2013-11-27 50 views
0

我一直在試圖弄清楚這是一段時間了,也許我一直在盯着它太久......我覺得這很簡單。自定義ListAdapter從來沒有調用GetView

這裏是我的ListActivity類

public class ResultsListActivity extends ListActivity { 
    private ArrayList<Result> results = new ArrayList<Result>(); 
    private ListView list; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.sp_history); 
     //set up list 
     list = new ListView(this); 

    } 

    @Override 
    protected void onResume() 
    { 
     super.onResume(); 
     results = openAndQueryDatabase(); 
     displayResultList(); 
    } 
    private void displayResultList() { 


     list.setAdapter(new HistoryArrayAdapter(this.getBaseContext(), results)); 

    } 
    private ArrayList<Result> openAndQueryDatabase() { 
     DatabaseHandler dbh = new DatabaseHandler(getBaseContext()); 
     ArrayList<Result> toReturn = dbh.getAllResults(); 
     return toReturn; 

    }//open and query database 

    public int getCount() { 
     return null == results ? 0 : results.size(); 
    } 

    } 

這裏是我的ListAdapterClass

public class HistoryArrayAdapter extends ArrayAdapter<Result> { 


    private final Context context; 
    private final ArrayList<Result> values; 
    private int layoutId; 

    public HistoryArrayAdapter(Context context, ArrayList<Result> results) { 
    super(context, R.layout.history_row, results); 
    this.context = context; 
    this.layoutId = R.layout.history_row; 
    this.values = results; 
    } 

    @Override 
    public int getCount() { 
     return values.size();// more than zero 
    } 

    @Override 
    public Result getItem(int position) { 

     return values.get(position);// may be in your case 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if(convertView==null) 
     { 
         // inflate the layout 
         LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
         convertView = inflater.inflate(layoutId, parent, false); 
     }// if convertView == null 

    //set up our views 
    TextView uploadSpeed = (TextView) convertView.findViewById(R.id.upload_speed_history); 
    TextView downloadSpeed = (TextView) convertView.findViewById(R.id.download_speed_history); 
    TextView pingTime = (TextView) convertView.findViewById(R.id.ping_history_text); 
    TextView networkName = (TextView) convertView.findViewById(R.id.history_network_name); 
    TextView dateTime = (TextView) convertView.findViewById(R.id.history_dateTime); 
    ImageView imageView = (ImageView) convertView.findViewById(R.id.history_connection); 

    //set up our values 
    Result row = values.get(position); 
    uploadSpeed.setText(row.getUpload()); 
    downloadSpeed.setText(row.getDownload()); 
    pingTime.setText(row.getPingTime()); 
    networkName.setText(row.getNetworkName()); 
    dateTime.setText(row.getDateTime()); 
    if(row.getNetworkType() == "NETWORK_WIFI") 
     imageView.setImageResource(R.drawable.ico_wifi); 
    else 
     imageView.setImageResource(R.drawable.ico_cell); 

    return convertView; 
    }//getView 

    } 

這裏是爲ListView

 <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

<ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:divider="#b5b5b5" 
    android:dividerHeight="1dp" /> 

</LinearLayout> 

這裏的.XML是行佈局

 <?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

<RelativeLayout 
    android:id="@+id/realtive_line_1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:id="@+id/history_network_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="26dp" 
     android:layout_toRightOf="@+id/imageView1" 
     android:text="TextView" /> 

    <ImageView 
     android:id="@+id/history_connection" 
     android:layout_width="20dip" 
     android:layout_height="20dip" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft = "20dip" 
     android:layout_marginTop = "5dip" 
     android:src="@drawable/ico_wifi" /> 

    <TextView 
     android:id="@+id/history_dateTime" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="65dp" 
     android:layout_below="@+id/network_name" 
     android:text="2013-08-15 12:35:00 PM" 
     android:textSize="10sp" /> 

</RelativeLayout> 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/realtive_line_1" 
    > 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="15dip" 
     android:layout_height="15dip" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="23dp" 
     android:layout_marginTop = "15dip" 
     android:src="@drawable/ico_ping" /> 

    <TextView 
     android:id="@+id/ping_history_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="25dp" 
     android:layout_toRightOf="@+id/imageView2" 
     android:text="30 ms" 
     android:textSize = "15sp" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <ImageView 
     android:id="@+id/results_icon_up" 
     android:layout_width="20dip" 
     android:layout_height="20dip" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:src="@drawable/ico_up" 
     android:layout_marginRight = "40dip" /> 

    <ImageView 
     android:id="@+id/results_icon_down" 
     android:layout_width="20dip" 
     android:layout_height="20dip" 
     android:layout_alignParentRight="true" 
     android:layout_marginTop="5dip" 
     android:layout_below ="@+id/results_icon_up" 
     android:src="@drawable/ico_down" 
     android:layout_marginRight = "40dip" /> 

    <TextView 
     android:id="@+id/upload_speed_history" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/results_icon_down" 
     android:layout_marginRight="15dp" 
     android:layout_toLeftOf="@+id/results_icon_up" 
     android:text="4.26 Mbps" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <TextView 
     android:id="@+id/download_speed_history" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/upload_speed_history" 
     android:layout_alignTop="@+id/results_icon_down" 
     android:text="24.56 Mbps" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

</RelativeLayout> 

<ImageView 
    android:id="@+id/imageView3" 
    android:layout_width="20dip" 
    android:layout_height="20dip" 
    android:layout_alignParentRight ="true" 
    android:layout_centerVertical="true" 
    android:src="@drawable/arrow" /> 

</RelativeLayout> 

我知道的事情......我確實有結果。我可以在調試器中看到它們,並且它們填充良好。我知道我有一些東西看起來像不必要的方法,但有一些工作要做,以確保應用程序在填充列表之前不在進程的中間。

+0

是您的'DatabaseHandler'類進行異步調用以獲得結果? – d3m0li5h3r

+0

是的,我有結果回來。我可以在調試器中看到它們。 –

回答

2

更改onCreate()像下面,

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.sp_history); 
     //set up list 
     list = getListView(); 

    } 
+0

LayoutInflater inflater =(LayoutInflater)上下文 \t \t \t \t .getSystemService(Context.LAYOUT_INFLATER_SERVICE); <---這是你最重要的問題。完美謝謝! –

0

正如你說,你正在使用異步調用來獲取數據庫結果...即使在調試時,你所得到的結果,有機會,而實際運行時,結果可能會在適配器已設置在下面的行中,因此適配器中的results的長度爲0,因此getView()沒有得到回調。

可以有一個自定義Listener,當結果已被提取,然後你可以將適配器設置爲列表視圖時,它會得到回調,或者另一種解決方案可能是容納適配器的實例,然後添加獲得的結果使用方法Adapter.add()方法,然後調用Adapter.notifyDataSetChanged()方法。

編輯: - 同樣當您使用ListActivity時,不需要創建新的ListView實例。只要撥打getListView()方法即可獲得ListView實例,如上述回答者所建議。

相關問題