crystalWing
crystalWing

Reputation: 69

I got a weird situation about android listView and baseAdapter?

Here is my code:

public class MyAdapter extends BaseAdapter{
    private LayoutInflater mInflater;
    ViewHolder holder = null;

    public MyAdapter(Context context){
        this.mInflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mData.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) {
        /**Original holder
         * 
         * 
         * */
        //ViewHolder holder = null;
        if (convertView == null) {

            holder=new ViewHolder();  
            convertView = mInflater.inflate(R.layout.country_list_row, null);
            holder.countryName=(TextView)convertView.findViewById(R.id.country_text);
            holder.check = (CheckBox)convertView.findViewById(R.id.country_check);
            convertView.setTag(holder);

        }else {                 
            holder = (ViewHolder)convertView.getTag();

        }
        holder.check.setTag(new Integer(position));

        Log.v("In CountryList","mData.get("+position+").get(country) = "+(String)mData.get(position).get("country"));       

        holder.countryName.setText((String)mData.get(position).get("country"));
        if (position == countryIndex){
            Log.v("In CountryList onCreate","before setChecked(true), countryIndex=="+countryIndex);       
            Log.v("In CountryList onCreate","before setChecked(true), position=="+position);       
            holder.check.setChecked(true);
        }else{
            Log.v("In CountryList onCreate","setChecked(false), countryIndex=="+countryIndex);       
            Log.v("In CountryList onCreate","setChecked(false), position=="+position);       
            holder.check.setChecked(false);
        }

        holder.check.setOnCheckedChangeListener(new myCheckBoxListener(position));

        return convertView;
    }



}

I found that the position kept looping from 0-8. However, I can also get the correct object from mData using "(String)mData.get(position).get("country")" without repetition. Could someone tell me why.

Upvotes: 1

Views: 225

Answers (1)

user1659006
user1659006

Reputation: 373

Take a look a this: Absolute position in BaseAdapter of GrivView

BTW, is your listview shows around 7-9 rows at a time?

Upvotes: 1

Related Questions