Android - 奇怪的ListView错误

时间:2014-11-10 06:15:58

标签: android listview

ArrayList按以下顺序保存字符串值:“GREEN”,“GREEN”,“RED”,“RED”,“GREEN”,“GREEN”。这些值正确显示在ListActivity的ListView中。

我已将其设置为当单击ListView中的任何项目时,将从列表中删除字符串值为“RED”的所有项目。然而,问题是ListView,尽管它的值容器现在只包含“GREEN”值,但在视觉上显示删除了两个“GREEN”值,所以现在ListView显示“GREEN”,“GREEN”,“RED”,“ RED“,即使适配器包含值”GREEN“,”GREEN“,”GREEN“,”GREEN。“

如果适配器包含值R,R,G,G,R,R,G,G并且单击了一个项目,它现在包含R,R,G,G。

我的代码中是否有错误?显示所有“GREEN”字符串值的调试器真的很令人困惑,因为ListView显示的内容不同。

public class MyActivity extends ListActivity implements AdapterView.OnItemClickListener {
    ArrayList<String> listValues;
    bugtestadapter adapter;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        listValues = new ArrayList<String>();
        listValues.add("GREEN");
        listValues.add("GREEN");
        listValues.add("RED");
        listValues.add("RED");
        listValues.add("GREEN");
        listValues.add("GREEN");

        adapter = new bugtestadapter(this, R.layout.adapter, listValues);
        setListAdapter(adapter);

        getListView().setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        deleteRedValues();
        adapter.notifyDataSetChanged();
    }

    public void deleteRedValues() {
        for (Iterator<String> iterator = listValues.iterator(); iterator.hasNext();) {
            String c = iterator.next();
            if (c.equals("RED")) {
                iterator.remove();
            }
        }
    }
}

bugtestadapter.java

public class bugtestadapter extends ArrayAdapter<String> {

    Context mContext;
    ArrayList<String> adapterList;
    LayoutInflater inflater;

    public bugtestadapter(Context context, int resource, ArrayList<String> objects) {
        super(context, resource, objects);
        mContext = context;
        adapterList = (ArrayList)objects;
        inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if ( convertView == null ) {
            convertView = inflater.inflate( R.layout.adapter, null );
            TextView tv = (TextView) convertView.findViewById(R.id.adapterItem);
            tv.setText(adapterList.get(position));

            if (tv.getText().equals("GREEN"))
                tv.setTextColor(Color.GREEN);
            else if (tv.getText().equals("RED"))
                tv.setTextColor(Color.RED);
        }

        return convertView;
    }
}

2 个答案:

答案 0 :(得分:2)

在getView方法中初始化If条件中的视图,即if(convertView == null)并在循环外设置数据。

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
        if ( convertView == null ) {
holder=new Holder();
                convertView = inflater.inflate( R.layout.adapter, null );
                holder.tvName = (TextView) convertView.findViewById(R.id.adapterItem);
            }else{
holder=convertView.getTag();
}

tv.setText(adapterList.get(position));
            if (tv.getText().equals("GREEN"))
                tv.setTextColor(Color.GREEN);
            else if (tv.getText().equals("RED"))
                tv.setTextColor(Color.RED);
        return convertView;
    }

并添加一个Holder。

private class Holder{
TextView tvName;
}

答案 1 :(得分:1)

你必须使用 Holder 类来完成,如下所示:

public class bugtestadapter extends ArrayAdapter<String> {

    Context mContext;
    ArrayList<String> adapterList;
    LayoutInflater inflater;

    public bugtestadapter(Context context, int resource, ArrayList<String> objects) {
        super(context, resource, objects);
        mContext = context;
        adapterList = (ArrayList)objects;
        inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if ( convertView == null ) {
            holder = new ViewHolder();
            convertView = inflater.inflate( R.layout.adapter, null );
            holder.tv = (TextView) convertView.findViewById(R.id.adapterItem);
            holder.tv.setText(adapterList.get(position));

            if (holder.tv.getText().equals("GREEN"))
                holder.tv.setTextColor(Color.GREEN);
            else if (tv.getText().equals("RED"))
                holder.tv.setTextColor(Color.RED);
        }else {
                holder = (ViewHolder) convertView.getTag();
            }


        return convertView;
    }

class ViewHolder {
            TextView tv;

        }
}