2014-10-31 34 views
0

問過多次,但我無法在自己的代碼中找到問題。android setAdapter不會觸發getView或不會生成ListView

作爲其他人,我有一個ArrayList,我需要顯示它。

問題是ListView在活動啓動時不可見,我不知道爲什麼。

這裏是我的代碼:

佈局:activity_dashboard_screen.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/ly_dashboard" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.ciroreed.war_horizon.ui.Dashboard_screen" > 

<TextView 
    android:id="@+id/tv_dashboard_display_username" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="username" 
    tools:ignore="HardcodedText" /> 

<TableRow 
    android:id="@+id/row_dashboard_1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="end" > 

</TableRow> 

<TableRow 
    android:id="@+id/row_dashboard_2" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" > 

    <ListView 
     android:id="@+id/lv_dashboard_matchlist" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" /> 

</TableRow> 

自定義類,它擴展BaseAdapter:MatchAdapter.java

package com.ciroreed.war_horizon; 

import java.util.ArrayList; 

import android.content.Context; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.TextView; 
import android.widget.TwoLineListItem; 

import com.ciroreed.war_horizon.data.Match; 

public class MatchAdapter extends BaseAdapter { 

    private Context con; 
    private ArrayList<Match> MATCHLIST; 


    public MatchAdapter(Context appcontext, ArrayList<Match> matchlist){ 
     con = appcontext; 
     MATCHLIST = matchlist; 

    } 

.............. 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     TwoLineListItem tll; 

     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) con 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      tll = (TwoLineListItem) inflater.inflate(
        android.R.layout.simple_list_item_2, parent); 
     } else { 
      tll = (TwoLineListItem) convertView; 
     } 

     TextView text1 = tll.getText1(); 
     TextView text2 = tll.getText2(); 

     text1.setText("VS "+MATCHLIST.get(position).getEnemy_name()); 
     text2.setText("turno: "+MATCHLIST.get(position).isPlayerTurn()); 

     return tll; 
    } 

} 

這裏是活動何時可以運行它:

package com.ciroreed.war_horizon.ui; 

import java.util.concurrent.ExecutionException; 

import org.json.JSONException; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.ciroreed.war_horizon.Controller; 
import com.ciroreed.war_horizon.MatchAdapter; 
import com.ciroreed.war_horizon.R; 

public class Dashboard_screen extends Activity implements OnClickListener{ 

    private TextView tv_dashboard_username; 
    private ListView lv_dashboard_matchlist; 

    private MatchAdapter MATCHLIST_adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_dashboard_screen); 

     init(); 
    } 

    private void init() { 
     // TODO Auto-generated method stub 
     tv_dashboard_username = (TextView)findViewById(R.id.tv_dashboard_display_username); 
     lv_dashboard_matchlist = (ListView)findViewById(R.id.lv_dashboard_matchlist); 


     try { 
      if(Controller.getCurrentMatches()){ 

       MATCHLIST_adapter = new MatchAdapter(getApplicationContext(), Controller.MATCHLIST);  

       Toast.makeText(getApplicationContext(), "Partidas cargadas correctamente", Toast.LENGTH_SHORT).show(); 
      }else{ 
       Toast.makeText(getApplicationContext(), "Error al cargar las partidas", Toast.LENGTH_SHORT).show(); 
      } 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ExecutionException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     MATCHLIST_adapter.notifyDataSetChanged(); 
     lv_dashboard_matchlist.setAdapter(MATCHLIST_adapter); 
    } 

當我執行,..好吧,這不是主要活動。它運行正常,但和ArrayList<Match>包含一個元素(至少)..

請不要問有關以前的代碼是如何工作的,如「Controller.getCurrentMatches()」 IE這個方法得到一個JSON從數據遠程服務器,除了相對於ListAdapter的代碼之前,其他所有代碼都已經過測試並正常工作。

+1

您可以將一些日誌行添加到您的自定義適配器以查看是否調用了這些方法。 – 2014-10-31 22:19:59

+0

嗨,謝謝。適配器的構造函數被調用,但getView()不會被執行:\ – k1r0s 2014-10-31 22:30:59

+1

getCount方法如何實現? – 2014-10-31 22:32:10

回答

0

http://www.vogella.com/tutorials/AndroidListView/article.html

章:13教程:如何在ListView

package de.vogella.android.listactivity; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.Map; 

import android.app.ListActivity; 
import android.os.Bundle; 
import android.widget.SimpleAdapter; 

public class MyTwoListItemsActivity extends ListActivity { 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    ArrayList<Map<String, String>> list = buildData(); 
    String[] from = { "name", "purpose" }; 
    int[] to = { android.R.id.text1, android.R.id.text2 }; 

    SimpleAdapter adapter = new SimpleAdapter(this, list, 
     android.R.layout.simple_list_item_2, from, to); 
    setListAdapter(adapter); 
    } 

    private ArrayList<Map<String, String>> buildData() { 
    ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>(); 
    list.add(putData("Android", "Mobile")); 
    list.add(putData("Windows7", "Windows7")); 
    list.add(putData("iPhone", "iPhone")); 
    return list; 
    } 

    private HashMap<String, String> putData(String name, String purpose) { 
    HashMap<String, String> item = new HashMap<String, String>(); 
    item.put("name", name); 
    item.put("purpose", purpose); 
    return item; 
    } 

} 

這不是對這個問題的準確的反應顯示兩個項目,但它解決了我的問題。

1

你在你的適配器的getCount()中做了什麼? 您應該返回一個大於0的值getCount()

+0

恕我直言,但這已經解決了這麼久,但我沒有公佈答案。 – k1r0s 2015-03-18 15:50:12