2016-07-27 14 views
0

Ich habe eine ListView, die einen Adapter verwendet, der ein String-Array als Argument erhält. Wenn ich mir die Listenansicht anschaue, wird 1 Element angezeigt, dann wird dahinter viel Leerzeichen angezeigt, gefolgt vom nächsten Element, wenn ich nach unten blättere. Was könnte das Problem sein? HierAndroid ListView mit einem Element pro Seite

class CustomAdapter extends ArrayAdapter<String> { 

CustomAdapter(Context context, String[] camere) { 
    super(context, R.layout.custom_row, camere); 
} 
boolean parola = true; 
boolean intra = true; 
String player_id, room_id; 
String siteul = "some_site"; 
String site; 
HashMap<String, String> hash; 

static class ViewHolder { 
    TextView camera; 
    TextView players; 
    TextView max_players; 
    ImageView privata; 
    Button Buton; 
    String room_id, nume; 
} 
ViewHolder ceva; 
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    final ViewHolder holder; 
    String variabile[] = getItem(position).split("\\s+"); 

    if(convertView == null) 
    { 
     LayoutInflater linflater = LayoutInflater.from(getContext()); 
     convertView = linflater.inflate(R.layout.custom_row, parent, false); 
     holder = new ViewHolder(); 
     holder.camera = (TextView) convertView.findViewById(R.id.Nume); 
     holder.players = (TextView) convertView.findViewById(R.id.players); 
     holder.max_players = (TextView) convertView.findViewById(R.id.max_players); 
     holder.privata = (ImageView) convertView.findViewById(R.id.privata); 
     holder.Buton = (Button) convertView.findViewById(R.id.Buton); 
     holder.camera.setText(variabile[0]); 
     if (!variabile[1].equals("true")) { 
      parola = false; 
      holder.privata.setVisibility(View.INVISIBLE); 
     } 
     holder.players.setText(variabile[2]); 
     holder.max_players.setText(variabile[3]); 
     holder.room_id = variabile[4]; 
     holder.nume = variabile[5]; 

     holder.Buton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       int playeri = Integer.parseInt(holder.players.getText().toString()); 
       int maximi = Integer.parseInt(holder.max_players.getText().toString()); 
       if(playeri < maximi) 
       { 
        hash = new HashMap<String, String>(); 
        hash.put("name", holder.nume); 
        hash.put("room", holder.room_id); 
        if (intra) { 
         holder.Buton.setText("Iesi"); 
         site = siteul + "/join"; 
         intra = false; 
        } else { 
         holder.Buton.setText("Intra"); 
         site = siteul + "/leave"; 
         intra = true; 
        } 
        new ATask((ViewHolder) v.getTag()).execute(site); 
       } 
      } 
     }); 
     convertView.setTag(holder); 
     holder.Buton.setTag(holder); 
    } 
    else 
     holder = (ViewHolder) convertView.getTag(); 
    return convertView; 
} 

public void CheckStart() 
{ 
    site = siteul + "/checkstart"; 
    hash = new HashMap<String, String>(); 
    hash.put("room", room_id); 
    new ATask(ceva).execute(site); 
} 

public void CheckPlayers() 
{ 
    site = siteul + "/checkplayers"; 
    hash = new HashMap<String, String>(); 
    hash.put("room", room_id); 
    new ATask(ceva).execute(site); 
} 

public void openGame(String litera) 
{ 
    Intent incercare = new Intent(getContext(), Game.class); 
    incercare.putExtra("nume", player_id); 
    incercare.putExtra("room", room_id); 
    incercare.putExtra("litera", litera); 
    incercare.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    getContext().startActivity(incercare); 
} 

private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException { 
    StringBuilder result = new StringBuilder(); 
    boolean first = true; 
    for(Map.Entry<String, String> entry : params.entrySet()){ 
     if (first) 
      first = false; 
     else 
      result.append("&"); 

     result.append(URLEncoder.encode(entry.getKey(), "UTF-8")); 
     result.append("="); 
     result.append(URLEncoder.encode(entry.getValue(), "UTF-8")); 
    } 
    return result.toString(); 
} 

public class ATask extends AsyncTask<String, Void, String> { 
    String rez = ""; 
    ViewHolder myHolder; 
    public ATask(ViewHolder view) { 
     myHolder = view; 
     ceva = myHolder; 
     room_id = myHolder.room_id; 
    } 
    @Override 
    protected String doInBackground(String... urls) { 
     //try { 
     try { 
      Log.e("rasp", site); 
      URL obj = new URL(site); 
      try { 
       Log.e("rasp", obj.toString()); 
       HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 
       con.setRequestMethod("POST"); 
       con.setRequestProperty("Content-Type", 
         "application/x-www-form-urlencoded"); 
       //con.setRequestProperty("User-Agent", USER_AGENT); 
       // For POST only - START 
       con.setDoOutput(true); 
       OutputStream os = con.getOutputStream(); 
       os.write(getPostDataString(hash).getBytes()); 
       os.flush(); 
       os.close(); 
       // For POST only - END 
       int responseCode = con.getResponseCode(); 
       Log.e("rasp", "response code-ul e " + Integer.toString(responseCode)); 
       if (responseCode == HttpURLConnection.HTTP_OK) { //success 
        BufferedReader in = new BufferedReader(new InputStreamReader(
          con.getInputStream())); 
        String inputLine; 
        StringBuffer response = new StringBuffer(); 
        while ((inputLine = in.readLine()) != null) { 
         response.append(inputLine); 
        } 
        in.close(); 
        // print result 
        rez = response.toString(); 
       } 
       else { 
        Log.e("rasp", "POST request not worked"); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } catch (MalformedURLException e) { 
      Log.e("naspa", "E corupt!"); 
     } 
     //} catch (Exception e) { 
     // Log.e("rasp", "aia e"); 
     //} 
     return rez; 
    } 
    // onPostExecute displays the results of the AsyncTask. 
    @Override 
    protected void onPostExecute(String result) { 
     if (rez.charAt(0) == 'Y') 
      openGame(rez.substring(2)); 
     else if (rez.charAt(0) == 'V' && !intra) 
     { 
      TextView players_mare = myHolder.players; 
      players_mare.setText(rez.substring(2)); 
      final Handler handler = new Handler(); 
      handler.postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        // Do something after 1s = 1000ms 
        CheckStart(); 
       } 
      }, 1000); 
     } 
     else if(rez.charAt(0) == 'G') 
      CheckPlayers(); 
     else if(rez.charAt(0) == 'Z') 
     { 
      TextView players_mare = myHolder.players; 
      players_mare.setText(rez.substring(2)); 
     } 
     else if (rez.charAt(0) == 'D') 
     { 
      player_id = rez.substring(2); 
      final Handler handler = new Handler(); 
      handler.postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        // Do something after 1s = 1000ms 
        CheckStart(); 
       } 
      }, 400); 
     } 
    } 
} 
} 

ist die xml von "custom_row.xml":

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/background_joc"> 



<ScrollView 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_marginTop="5dp" 
    android:id="@+id/scrollView"> 

    <LinearLayout 
     android:background="@color/grey" 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <TextView 
      android:layout_marginTop="10dp" 
      android:layout_marginLeft="7dp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="Nume camera" 
      android:id="@+id/Nume" 
      android:textSize="25dp" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="Runde:" 
      android:layout_marginTop="-7dp" 
      android:layout_marginLeft="3dp" 
      android:id="@+id/textView15" /> 

     <TextView 
      android:layout_marginTop="15dp" 
      android:layout_marginLeft="-30dp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="5" 
      android:id="@+id/runde" 
      android:textSize="25dp" /> 

     <ImageView 
      android:layout_marginTop="18dp" 
      android:layout_marginLeft="20dp" 
      android:background="@drawable/buton_lock" 
      android:layout_width="15dp" 
      android:layout_height="20dp" 
      android:id="@+id/privata" /> 

     <TextView 
      android:layout_marginTop="10dp" 
      android:layout_marginLeft="7dp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="0" 
      android:id="@+id/players" 
      android:textSize="30dp" /> 

     <TextView 
      android:layout_marginTop="10dp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="? android:attr/textAppearanceMedium" 
      android:text="/" 
      android:id="@+id/nu_trebuia_sa_aiba_id" 
      android:textSize="30dp" /> 

     <TextView 
      android:layout_marginTop="10dp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="5" 
      android:id="@+id/max_players" 
      android:textSize="30dp" /> 

     <Button 
      android:layout_marginTop="10dp" 
      android:layout_width="90dp" 
      android:layout_height="40dp" 
      android:text="Intra" 
      android:id="@+id/Buton" 
      android:layout_marginLeft="5dp" /> 

     <TextView 
      android:layout_marginLeft="50dp" 
      android:layout_marginTop="50dp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="" 
      android:id="@+id/e_sa_pacaleasca_match_parentu_de_la_linear_layout" 
      /> 
    </LinearLayout> 
</ScrollView> 

+0

Fügen Sie die XML für 'R.layout.custom_row' hinzu –

Antwort

0

Vom R.layout.custom_row können wir sehen, dass Sie die Stammansicht machen (RelativeLayout) Eltern füllen und die Höhe machen von ScrollViewmatch_parent. So besetzen die Scrollview den gesamten Bildschirm. Das Leerzeichen gehört zu scrollview. Ich denke, Sie können die scrollview entfernen und die Root-Ansicht (RelativeLayout) Inhalt umbrechen.

Try-Codes unter:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="@drawable/background_joc"> 

    <ScrollView 
     android:id="@+id/scrollView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp"> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@color/grey" 
      android:orientation="horizontal"> 

      <TextView 
       android:id="@+id/Nume" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="7dp" 
       android:layout_marginTop="10dp" 
       android:text="Nume camera" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:textSize="25dp"/> 

      <TextView 
       android:id="@+id/textView15" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="3dp" 
       android:layout_marginTop="-7dp" 
       android:text="Runde:" 
       android:textAppearance="?android:attr/textAppearanceSmall"/> 

      <TextView 
       android:id="@+id/runde" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="-30dp" 
       android:layout_marginTop="15dp" 
       android:text="5" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:textSize="25dp"/> 

      <ImageView 
       android:id="@+id/privata" 
       android:layout_width="15dp" 
       android:layout_height="20dp" 
       android:layout_marginLeft="20dp" 
       android:layout_marginTop="18dp" 
       android:background="@drawable/buton_lock"/> 

      <TextView 
       android:id="@+id/players" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="7dp" 
       android:layout_marginTop="10dp" 
       android:text="0" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:textSize="30dp"/> 

      <TextView 
       android:id="@+id/nu_trebuia_sa_aiba_id" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="10dp" 
       android:text="/" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:textSize="30dp"/> 

      <TextView 
       android:id="@+id/max_players" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="10dp" 
       android:text="5" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:textSize="30dp"/> 

      <Button 
       android:id="@+id/Buton" 
       android:layout_width="90dp" 
       android:layout_height="40dp" 
       android:layout_marginLeft="5dp" 
       android:layout_marginTop="10dp" 
       android:text="Intra"/> 

      <TextView 
       android:id="@+id/e_sa_pacaleasca_match_parentu_de_la_linear_layout" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="50dp" 
       android:layout_marginTop="50dp" 
       android:text="" 
       android:textAppearance="?android:attr/textAppearanceSmall" /> 
     </LinearLayout> 
    </ScrollView> 
</RelativeLayout> 
+0

Ich brauche die Scrollview für den Fall, dass ich zu viele Elemente auf einer Seite angezeigt habe. – Vali79

+0

@ Vali79 Die 'ListView' kann auch scrollen, wenn Ihre Artikelansichten mehr als eine Seite belegen. Ich schlage vor, in dieser Demo nicht 'ScrollView' zu verwenden, denn wenn Sie' ListView' und 'ScrollView' gleichzeitig verwenden, müssen Sie die Gleitkonflikte behandeln. – DysaniazzZ

+0

Die scrollview wurde gelöscht, aber sie sieht immer noch gleich aus. – Vali79

0

In Ihrem custom_row.xml:

Änderung der layout_height des RelativeLayout, ScrollView und die LinearLayoutfill_parent-wrap_content.

+0

Es scheint nicht zu funktionieren. – Vali79

Verwandte Themen