2016-07-27 99 views
0

我有一個使用接收字符串數組作爲參數的適配器的列表視圖。當我查看列表視圖時,它顯示1個項目,然後顯示它後面的大量空白區域,如果向下滾動,則會顯示下一個項目。可能是什麼問題呢?Android ListView顯示每頁顯示一個項目

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); 
     } 
    } 
} 
} 

這裏是 「custom_row.xml」 的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

爲'R.layout.custom_row'添加xml –

回答

0

R.layout.custom_row我們可以看到,你弄根視圖(RelativeLayout)填寫父母,使高度ScrollViewmatch_parent。所以Scrollview佔據全屏。空白區域屬於scrollview。我認爲您可以刪除scrollview並使根視圖(RelativeLayout)包裝內容。

試試下面的代碼:

<?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

我需要滾動視圖以防萬一我有太多項目要顯示在一個頁面上。 – Vali79

+0

@ Vali79當你的項目視圖佔據多個頁面時,ListView也可以滾動。我建議不要在這個演示原因中使用'ScrollView',如果你同時使用'ListView'和'ScrollView',你必須處理滑動衝突。 – DysaniazzZ

+0

刪除了滾動視圖,但它看起來仍然相同。 – Vali79

0

在你custom_row.xml

變化RelativeLayoutScrollViewlayout_heightLinearLayoutfill_parentwrap_content

+0

它似乎沒有工作。 – Vali79