1

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();
    }
    }
1
  • your getItem method is still incorrect, this code will not work Commented Oct 29, 2010 at 12:10

2 Answers 2

1

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());
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

I try with "return historyArrayList.get(position);" but problem is steel :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.