多列listview错误视图

Posted

技术标签:

【中文标题】多列listview错误视图【英文标题】:Multicolumn listview wrong view 【发布时间】:2016-03-14 07:40:46 【问题描述】:

我遇到了多列列表视图的问题。问题是当我向上或向下滚动列表时,主题会更改行。 这是我的活动:

ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
    HashMap<String,String> temp = null; //new HashMap<String, String>();

    //TRASFERISCO I DATI DAGLI ARRAY ALLA MAPPA
    for(int i = 0 ; i < 24 ; i++) 
        temp = new HashMap<String, String>();
        temp.put(FIRST_COLUMN, i + " " + Array_Nome[i]);
        temp.put(SECOND_COLUMN, Array_Cognome[i]);
        temp.put(THIRD_COLUMN, Array_Cellulare[i]);
        temp.put(FOURTH_COLUMN, Array_UUID[i]);
        list.add(temp);
    

    ListView listView=(ListView)findViewById(R.id.listView1);
    MultiColoumnAdapter adapter=new MultiColoumnAdapter(this, list);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    
        @Override
        public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
        
            int pos=position+1;
            Toast.makeText(MainActivity.this, Integer.toString(pos)+" Clicked", Toast.LENGTH_SHORT).show();
        

    );

这是适配器:

public class MultiColoumnAdapter extends BaseAdapter

public ArrayList<HashMap<String, String>> list;
Activity activity;
TextView txtFirst;
TextView txtSecond;
TextView txtThird;
TextView txtFourth;
public MultiColoumnAdapter(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



    LayoutInflater inflater=activity.getLayoutInflater();

    if(convertView == null)

        convertView=inflater.inflate(R.layout.colmn_row, null);

        txtFirst=(TextView) convertView.findViewById(R.id.name);
        txtSecond=(TextView) convertView.findViewById(R.id.gender);
        txtThird=(TextView) convertView.findViewById(R.id.age);
        txtFourth=(TextView) convertView.findViewById(R.id.status);

    else

    

    HashMap<String, String> map=list.get(position);
    txtFirst.setText(map.get(FIRST_COLUMN));
    txtSecond.setText(map.get(SECOND_COLUMN));
    txtThird.setText(map.get(THIRD_COLUMN));
    txtFourth.setText(map.get(FOURTH_COLUMN));

    return convertView;

最后是列行:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_>

<TextView
    android:id="@+id/name"
    android:layout_
    android:layout_
    android:layout_weight="1"
    android:textStyle="bold"
    android:textSize="22sp"/>

<TextView
    android:id="@+id/gender"
    android:layout_
    android:layout_
    android:layout_weight="1"
    android:textSize="22sp"/>

<TextView
    android:id="@+id/age"
    android:layout_
    android:layout_
    android:layout_weight="0.5"
    android:textSize="22sp"/>

<TextView
    android:id="@+id/status"
    android:layout_
    android:layout_
    android:layout_weight="1"
    android:textSize="22sp"/>

结果是这样的:

1 马克·汤姆森 12345678

2 约翰·史密斯 54654879

3 玛丽亚·特朗普 45869958

向上/向下滚动后:

3 玛丽亚·特朗普 45869958

1 马克汤姆森 12345678

2 约翰·史密斯 54654879

【问题讨论】:

尝试更改getItemId:返回位置而不是0 【参考方案1】:

您的 if(convertView == null) 块应更改如下,因为您的 else 案例为空。

    if(convertView == null)
    ViewHolder viewHolder = new ViewHolder();
    convertView=inflater.inflate(R.layout.colmn_row, null);

    viewHolder.txtFirst=(TextView) convertView.findViewById(R.id.name);
    viewHolder.txtSecond=(TextView) convertView.findViewById(R.id.gender);
    viewHolder.txtThird=(TextView) convertView.findViewById(R.id.age);
    viewHolder.txtFourth=(TextView) convertView.findViewById(R.id.status);
    convertView.setTag(viewHolder)
else
    ViewHolder viewHolder = convertView.getTag(viewHolder);

ViewHolder 是适配器中的内部类,代码如下

public class ViewHolder
    TextView txtFirst,txtSecond,txtThird,txtFourth;

试试看,如果它有效,请告诉我。

这称为视图模式。在此处阅读。https://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder

【讨论】:

【参考方案2】:

感谢大家的建议。 这对我很有用

private class ViewHolder 
    TextView txtFirst;
    TextView txtThird;
    TextView txtSecond;
    TextView txtFourth;


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

    ViewHolder holder;
    LayoutInflater inflater=activity.getLayoutInflater();

    if(convertView == null)

        holder = new ViewHolder();
        convertView=inflater.inflate(R.layout.colmn_row, null);

        holder.txtFirst=(TextView) convertView.findViewById(R.id.name);
        holder.txtSecond=(TextView) convertView.findViewById(R.id.gender);
        holder.txtThird=(TextView) convertView.findViewById(R.id.age);
        holder.txtFourth=(TextView) convertView.findViewById(R.id.status);
        convertView.setTag(holder);

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

    HashMap<String, String> map=list.get(position);
    holder.txtFirst.setText(map.get(FIRST_COLUMN));
    holder.txtSecond.setText(map.get(SECOND_COLUMN));
    holder.txtThird.setText(map.get(THIRD_COLUMN));
    holder.txtFourth.setText(map.get(FOURTH_COLUMN));

    return convertView;

【讨论】:

【参考方案3】:

在 else 情况下,您的 getView 函数没有设置您的 textView 变量。如果你不这样做,你就会搞砸回收利用。

【讨论】:

【参考方案4】:

在你的getView

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


        ViewHolder viewHolder;
        LayoutInflater inflater=activity.getLayoutInflater();

        if(convertView == null)

            convertView=inflater.inflate(R.layout.colmn_row, null);

            txtFirst=(TextView) convertView.findViewById(R.id.name);
            txtSecond=(TextView) convertView.findViewById(R.id.gender);
            txtThird=(TextView) convertView.findViewById(R.id.age);
            txtFourth=(TextView) convertView.findViewById(R.id.status);
           convertView .setTag(viewHolder);

        else
            viewHolder = (ViewHolder) convertView .getTag();
        


         //And if you want to manipulate them u must use ViewHolder variable
        HashMap<String, String> map=list.get(position);
        viewHolder.txtFirst.setText(map.get(FIRST_COLUMN));
        viewHolder.txtSecond.setText(map.get(SECOND_COLUMN));
        viewHolder.txtThird.setText(map.get(THIRD_COLUMN));
        viewHolder.txtFourth.setText(map.get(FOURTH_COLUMN));

        return convertView;
    

当然,如果你还没有 ViewHolder 创建一个这样的

 /**
     * Creating ViewHolder , witch create adapter item elements
     */
    static class ViewHolder 
        public TextView txtFirst;
        public TextView txtThird;
       public TextView txtSecond;
       public TextView txtFourth;

    

【讨论】:

以上是关于多列listview错误视图的主要内容,如果未能解决你的问题,请参考以下文章

多列 ListView 显示零而不是数据库值

用于多列使用的 ListView 与 ListBox

多列 ListView 上的图标

ListView,更改错误的项目视图

多列listview UWP

如何在 Visual Basic 6 ListView 上进行多列排序?