问题
I tried to create a multicolumn listview by following this tutorial:
http://techlovejump.com/android-multicolumn-listview/ .
The problem occured, when the amount of records was so big that the whole view exceeded the screen of a phone. Now when I'm scrolling, all of the records in one of the rows are changing (their values are changed). All the other rows are perfectly fine. What could be the problem?
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:
Please try this code:
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;
}
}
来源:https://stackoverflow.com/questions/36499325/a-row-in-list-view-changes-when-scrolled-android-studio