Jovan
Jovan

Reputation: 4794

Android custom listview

I'm trying to create custom listview in android.

When I try to access my arraylist variale historyArrayList in HistoryAdapter -> getView, historyArrayList always return me last added element arraylist.

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();
    }
    }

Upvotes: 1

Views: 252

Answers (2)

Aaron Saunders
Aaron Saunders

Reputation: 33345

getItem looks incorrect

I would also suggest you clean up and restructure the code someone can help you with the rest, it is hard to follow

look at this tutorial... scroll down to the WeatherDataListAdapter code

Upvotes: 0

tinja
tinja

Reputation: 161

When you populate historyArrayList, you're updating and adding the same object History every time through the loop. Try reinitializing History at the start of the loop:

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

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

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

Upvotes: 1

Related Questions