2010-10-29 125 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