2014-10-09 53 views
0

我有一個表在sql數據庫中的值:id,名稱,年齡,體重,並可以讓他們進入List值。 清單應該是這樣的: 名稱 年齡:體重:Android的SQL元素到列表視圖

我該如何做到這一點?我試圖將列表傳遞給ArrayAdapter,但我不知道具體如何。當我使用這個: ArrayAdapter<Person> adapter = new ArrayAdapter<Person>(this, R.layout.list_example_entry, values);
我得到android.widget.FrameLayout cannot be cast to android.widget.TextView錯誤。

回答

0

要實現這一點,你必須建立一個自定義的適配器,並充氣自定義行佈局。使用ArrayAdapter將無法正常工作

所以,您的自定義適配器類可能是財產以後這樣的:

public class CustomAdapter extends BaseAdapter { 
     private final Activity activity; 
     private final List list; 

     public CustomAdapter(Activity activity, ArrayList<Person> list) { 
      this.activity = activity; 
      this.list = list; 
     } 

    @Override 
    public int getCount() { 
     return list.size(); 
    } 

    @Override 
    public Object getItem(int arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public long getItemId(int arg0) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View rowView = convertView; 
      ViewHolder view; 

      if(rowView == null) 
      { 
       // Get a new instance of the row layout view 
       LayoutInflater inflater = activity.getLayoutInflater(); 
       rowView = inflater.inflate(R.layout.rowlayout, null); 

       // Hold the view objects in an object, that way the don't need to be "re- finded" 
       view = new ViewHolder(); 
       view.person_name= (TextView) rowView.findViewById(R.id.name); 
       view.person_address= (TextView) rowView.findViewById(R.id.address); 

       rowView.setTag(view); 
      } else { 
       view = (ViewHolder) rowView.getTag(); 
      } 

      /** Set data to your Views. */ 
      Person item = list.get(position); 
      view.person_name.setText(item.getTickerSymbol()); 
      view.person_address.setText(item.getQuote().toString()); 

      return rowView; 
     } 

     protected static class ViewHolder{ 
      protected TextView person_name; 
      protected TextView person_address; 
     } 
    } 

而且你Person.java類可以簡單到我在下面說明:

public class Person { 
    private String name; 
    private String address; 

    public Person(String name, String address) { 
     this.name = name; 
     this.address = address; 
    } 
    public void setName(String name) { 
     this.name= name; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setAddress(String address) { 
     this.address= address; 
    } 
    public String getAddress() { 
     return address; 
    } 
} 

現在,在你的主要活動只是綁定你的列表與一些數據,如;

/** Declare and initialize list of Person. */ 
ArrayList<Person> list = new ArrayList<Person>(); 

/** Add some restaurants to the list. */ 
list.add(new Person("name1", "address1")); 
list.add(new Person("name2", "address2")); 
list.add(new Person("name3", "address3")); 
list.add(new Person("name4", "address4")); 
list.add(new Person("name5", "address5")); 
list.add(new Person("name6", "address6")); 
At this point you're able to set the custom adapter to your list 

ListView lv = (ListView) findViewById(R.id.mylist); 

CustomAdapter adapter = new CustomAdapter(YourMainActivityName.this, list); 
lv.setAdapter(adapter); 
+0

也許一個愚蠢的錯誤,但我不能甚至創建'公共CustomAdapter(活動活動,ArrayList的名單){ this.activity =活動; this.list = list;}它說在ArrayAdaptor中沒有默認的構造函數 – user2542809 2014-10-09 19:18:17

+0

錯誤說的是 – 2014-10-09 19:20:34

+0

錯誤:(23,73)錯誤:找不到適合ArrayAdapter的構造函數 – user2542809 2014-10-09 19:23:52