2010-08-29 36 views
1

我正在將項目添加到我的數據源並希望它出現在我的ListView中。出於某種原因,沒有什麼是出現:添加項目到數據源不會出現在ListView中

適配器:

private class STCompanyAdapter extends ArrayAdapter<STCompany> { 

     private ArrayList<STCompany> items; 

     public STCompanyAdapter(Context context, int textViewResourceId, 
       ArrayList<STCompany> items) { 
      super(context, textViewResourceId, items); 
      this.items = items; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View v = convertView; 
      if (v == null) { 
       LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       v = vi.inflate(R.layout.addstocksrow, null); 
      } 
      STCompany q = items.get(position); 
      if (q != null) { 
       TextView symbolText = (TextView) v.findViewById(R.id.toptext); 
       TextView nameText = (TextView) v.findViewById(R.id.bottomtext); 

       if (nameText != null) { 
        nameText.setText(q.getSymbol()); 
       } 
       if (symbolText != null) { 
        symbolText.setText(q.getName()); 
       } 
      } 
      return v; 
     } 
    } 

添加項目中的onCreate:

listView = (ListView) findViewById(R.id.symbolsListView); 
      this.companiesAdapter = new STCompanyAdapter(this, R.layout.addstocksrow, companies); 
      listView.setAdapter(this.companiesAdapter); 

    STCompany myCompany = new STCompany(); 
        myCompany.setName("La La"); 
        myCompany.setSymbol("BP"); 

        companiesAdapter.add(myCompany); 
        companiesAdapter.notifyDataSetChanged(); 

這裏是我的佈局:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout"> 
<EditText android:id="@+id/addStocksEditText" android:text="Search company or stock" android:layout_width="200dp" android:layout_height="50dp"></EditText><Button android:layout_height="wrap_content" android:text="Search" android:id="@+id/searchButton" android:layout_width="fill_parent"></Button><ListView android:id="@+id/symbolsListView" android:layout_width="fill_parent" android:layout_height="fill_parent"></ListView> 


</LinearLayout> 

回答

0

問題是我的佈局。 listview沒有出現。代碼是可以的。

1

嘗試companies.add(myCompany的)而不是companiesAdapter.add(myCompany)。

+0

我嘗試過,沒有運氣。 – 2010-08-29 22:12:48