多列listview错误视图

时间:2016-03-14 07:40:47

标签: android listview view

我遇到了multioloumn listview的问题。问题是,当我在列表中向上或向下滚动时,主题会发生变化。 这是我的活动:

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;
}

最后是coloumn行:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/name"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:textStyle="bold"
    android:textSize="22sp"/>

<TextView
    android:id="@+id/gender"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:textSize="22sp"/>

<TextView
    android:id="@+id/age"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:layout_weight="0.5"
    android:textSize="22sp"/>

<TextView
    android:id="@+id/status"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:textSize="22sp"/>

结果是这样的:

1 Marc Thomson 12345678

2 Jhon Smith 54654879

3 Maria Trump 45869958

向上/向下滚动后:

3 Maria Trump 45869958

1 Mark Thomson 12345678

2 Jhon Smith 54654879

4 个答案:

答案 0 :(得分:1)

您的if(convertView == null)区块应更改如下,因为您的其他情况为空。

    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

答案 1 :(得分:1)

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

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;
}

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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;

    }