列表视图中的一行在滚动时会更改(Android Studio)

时间:2016-04-08 12:15:33

标签: android listview baseadapter

我尝试按照本教程创建多列列表视图:

Datetime vs Date and Time Mysql

当记录数量太大而整个视图超出手机屏幕时,出现了问题。现在,当我滚动时,其中一行中的所有记录都在变化(它们的值已更改)。所有其他行都很好。可能是什么问题?

 public class ListViewAdapters extends BaseAdapter{

    public ArrayList<HashMap<String, String>> list;
    Activity activity;
    TextView txtFirst;
    TextView txtSecond;
    TextView txtFourth;

    public ListViewAdapters(Activity activity, ArrayList<HashMap<String, String>> list){
        super();
        this.activity=activity;
        this.list=list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        HashMap<String, String> map=list.get(position);
        LayoutInflater inflater=activity.getLayoutInflater();

        if(convertView == null)
        {
            if(map.get(MARKED)=="no")
             {
            convertView=inflater.inflate(R.layout.colmn_row, null);
             }
            else if (map.get(MARKED)=="yes")
            {
            convertView=inflater.inflate(R.layout.colmn_row_clicked, null);
            }
            txtFirst=(TextView) convertView.findViewById(R.id.name);
            txtSecond=(TextView) convertView.findViewById(R.id.gender);
            txtFourth=(TextView) convertView.findViewById(R.id.status);
        }

        txtFirst.setText(map.get(FIRST_COLUMN));
        txtSecond.setText(map.get(SECOND_COLUMN));
        txtFourth.setText(map.get(FOURTH_COLUMN));

        return convertView;
    }

}

1 个答案:

答案 0 :(得分:3)

请尝试以下代码:

public class ListViewAdapters extends BaseAdapter {

public ArrayList<HashMap<String, String>> list;
Activity activity;
TextView txtFirst;
TextView txtSecond;
TextView txtFourth;

public ListViewAdapters(Activity activity, ArrayList<HashMap<String, String>> list){
    super();
    this.activity=activity;
    this.list=list;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return list.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}



@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder;
    HashMap<String, String> map=list.get(position);
    LayoutInflater inflater=activity.getLayoutInflater();

    if(convertView == null) {
        holder = new ViewHolder();
        if (map.get(MARKED) == "no") {
            convertView = inflater.inflate(R.layout.colmn_row, null);
        } else if (map.get(MARKED) == "yes") {
            convertView = inflater.inflate(R.layout.colmn_row_clicked, null);
        }
        holder.txtFirst = (TextView) convertView.findViewById(R.id.name);
        holder.txtSecond = (TextView) convertView.findViewById(R.id.gender);
        holder.txtFourth = (TextView) convertView.findViewById(R.id.status);

        holder.txtFirst.setText(map.get(FIRST_COLUMN));
        holder.txtSecond.setText(map.get(SECOND_COLUMN));
        holder.txtFourth.setText(map.get(FOURTH_COLUMN));
    }else {
        holder = (ViewHolder) convertView.getTag();
    }
    return convertView;
}

@Override
public int getItemViewType(int position) {
    return position;
}

@Override
public int getViewTypeCount() {
    return getCount();
}

public class ViewHolder {
    TextView txtFirst, txtSecond,txtFourth;

}

}