2010-10-29 106 views
0

我想在android中創建自定義列表視圖。Android自定義列表視圖

當我嘗試訪問我的ArrayList variale historyArrayListHistoryAdapter - >getViewhistoryArrayList總是返回我最後添加的元素數組列表。

public class HistoryDetails extends Activity { 

    List<HistoryInfoClass> historyArrayList = new ArrayList<HistoryInfoClass>() ; 
    DBAdapter db = new DBAdapter(this); 


    private class HistoryAdapter extends BaseAdapter { 
     private LayoutInflater mInflater; 

     public HistoryAdapter(Context context) { 

      mInflater = LayoutInflater.from(context); 

     } 

     public int getCount() { 

      return historyArrayList.size(); 

     } 

     public Object getItem(int position) { 

      return position; 
     } 

     public long getItemId(int position) { 

      return position; 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 

     ViewHolder holder; 

     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.history_listview, null); 
      holder = new ViewHolder(); 
      holder.text = (TextView) convertView.findViewById(R.id.TextView01); 
      holder.text2 = (TextView) convertView.findViewById(R.id.TextView02); 
      convertView.setTag(holder); 

     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     //PROBLEM HERE " historyArrayList.get(position).Time " always give me last element in historyArrayList, and historyArrayList.get(0).Time give me last element too, and get(1) 
     holder.text.setText(Integer.toString(historyArrayList.get(position).Time)); 
     holder.text2.setText(Integer.toString(historyArrayList.get(position).Time1)); 

     return convertView; 

     } 

     private class ViewHolder { 
     TextView text; 
     TextView text2; 


     } 
    } 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     setContentView(R.layout.historydetails); 
     super.onCreate(savedInstanceState); 



     HistoryFromDBToArray(); 


     ListView l1 = (ListView) findViewById(R.id.ListView01); 
     l1.setAdapter(new HistoryAdapter(this)); 

     l1.setOnItemClickListener(new OnItemClickListener(){ 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
        long arg3) { 

       Toast.makeText(getBaseContext(), "You clciked ", Toast.LENGTH_LONG).show(); 

       } 
      }); 
     } 


    class HistoryInfoClass { 

     Integer Time = 0, 
       Time1 = 0; 

     } 


    private void HistoryFromDBToArray(){ 

     HistoryInfoClass History = new HistoryInfoClass();  
     historyArrayList.clear(); 


     db.open(); 
     int i =0; 
     Cursor c = db.getHistory("history"); 
     startManagingCursor(c); 
     if (c.moveToFirst()) 
     { 
      do {   

       History.Time = c.getInt(1); 
       History.Time1 = c.getInt(2); 


       historyArrayList.add(History); 
// Here "historyArrayList.get(i).Time" return true value (no last record) 
i++; 
      } while (c.moveToNext()); 
     } 

     db.close(); 
    } 
    } 
+0

您的getItem方法仍然不正確,該代碼將無法正常工作 – 2010-10-29 12:10:49

回答

1

當您填充historyArrayList時,您每次通過循環更新並添加相同的對象History。嘗試在循環的開始,重新初始化History

do { 
    // Initialize History 
    History = new HistoryInfoClass(); 

    History.Time = c.getInt(1); 
    History.Time1 = c.getInt(2); 

    historyArrayList.add(History); 
    i++; 
} while (c.moveToNext()); 
0

getItem看起來不正確

我也建議你清理和重構的代碼有人可以幫助你的休息,這是很難遵循

look at this tutorial ...向下滾動到WeatherDataListAdapter代碼

+0

我嘗試用「返回historyArrayList.get(位置);」但問題是鋼:( – Jovan 2010-10-29 01:25:35