如何从Android中的ListView中正确删除项目?

时间:2012-03-19 16:21:34

标签: android android-layout android-widget android-listview

从ListView中删除项目时遇到问题,

这是我的活动:

public class TestListItemsView1Activity extends ListActivity {
    /** Called when the activity is first created. */

    private EfficientAdapter efficientAdapter;
    private String[] data = new String[] { "BBC", "Yahoo", "CNN", "Eenadu", "Hindu" };
    private List<String> dataValues = Arrays.asList(data);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        efficientAdapter = new EfficientAdapter(this,dataValues);

        setListAdapter(efficientAdapter);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
        //dataValues.remove(position);
        //efficientAdapter.notifyDataSetChanged();
        Toast.makeText(this, "Click-" + String.valueOf(position), Toast.LENGTH_SHORT).show();
    }
}

和Adapter类:

public class EfficientAdapter extends BaseAdapter implements Filterable{

    private LayoutInflater mInflater;
    private static String TAG=EfficientAdapter.class.getSimpleName();
    private Context context;

    private HashMap<String,EfficientAdapter.ViewHolder> holders= new HashMap<String, EfficientAdapter.ViewHolder>();
    //private String[] dataArray = new String[] { "BBC", "Yahoo", "CNN", "Eenadu", "Hindu" };
    private List<String> data= new ArrayList<String>();//Arrays.asList(dataArray);

    public EfficientAdapter(Context context,List<String> data) {
        //super(context, R.layout.main, values);
        mInflater = LayoutInflater.from(context);
        this.context=context;
        this.data.addAll(data);
    }

    public EfficientAdapter(Context context) {
        //super(context, R.layout.main, values);
        mInflater = LayoutInflater.from(context);
        this.context=context;
        //data.addAll(data);
    }
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if(convertView == null){
            convertView = mInflater.inflate(R.layout.adaptor_content, null);

            holder = new ViewHolder();
            holder.textLine = (TextView) convertView.findViewById(R.id.textLine);
            holder.buttonLine =(Button) convertView.findViewById(R.id.buttonLine);

            holder.textLine.setText(this.data.get(position));
            convertView.setOnClickListener(new OnClickListener() {
                private int pos= position;
                public void onClick(View v) {
                    Toast.makeText(context, "Click-" + String.valueOf(pos), Toast.LENGTH_SHORT).show(); 

                }
            });

            holder.buttonLine.setOnClickListener(new OnClickListener() {
                private int pos= position;

                public void onClick(View v) {
                    /*ViewHolder deleteHolder = (ViewHolder)v.getTag();
                    int delPosition=deleteHolder.position;
                    holders.remove(delPosition);
                    ViewHolder currentHolder;
                    for(int i=pos+1;i<getCount();i++){
                        currentHolder = holders.get(delPosition);
                        currentHolder.position=i-1;
                    }
                    data.remove(delPosition);
                    notifyDataSetChanged();*/
                    //View convertView1 = mInflater.inflate(R.layout.adaptor_content, null);
//                  ViewHolder deleteHolder = (ViewHolder)v.getTag();

                    data.remove(getItem(pos));
                    notifyDataSetChanged();


                    //final ArrayAdapter adapter = ((ArrayAdapter)getListAdapter());


                    //ListView lv = v.findViewById(R.layout.)


                    //v.invalidate();

                    Log.i(TAG, "Delete button pressed at position" + pos);
                    Toast.makeText(context, "Delete-" + String.valueOf(pos), Toast.LENGTH_SHORT).show();

                }
            });
            //holder.position=position;
            //holders.put(Integer.toString(position), holder);
            convertView.setTag(holder);

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

        return convertView;
    }


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

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

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


    public Filter getFilter() {
        // TODO Auto-generated method stub
        return null;
    }


    static class ViewHolder {
        TextView textLine;
        ImageView iconLine;
        Button buttonLine;
        //int position;
        }

}

每次删除最后一项而不是点击的项目。

1 个答案:

答案 0 :(得分:2)

enter code here我认为你没有更新View的位置。

当您执行if (convertView == null) { inflate........}时,您说第一次创建视图时,必须执行此操作。但是在ListViews中的Android视图是可重用的,所以如果你需要更新视图的位置,你必须放弃这个条件。

尝试在其他地方的if (convertView = null) ...中设置de ClickLsiteners。 试试吧......告诉我

public class EfficientAdapter extends BaseAdapter implements Filterable{

private LayoutInflater mInflater;
private static String TAG=EfficientAdapter.class.getSimpleName();
private Context context;

//private String[] dataArray = new String[] { "BBC", "Yahoo", "CNN", "Eenadu", "Hindu" };
private List<String> data= new ArrayList<String>();

public EfficientAdapter(Context context,List<String> data) {
    super();
    mInflater = LayoutInflater.from(context);
    this.context=context;
    this.data.addAll(data);
}

public EfficientAdapter(Context context) {
    super();
    mInflater = LayoutInflater.from(context);
    this.context=context;
}
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if(convertView == null){
        convertView = mInflater.inflate(R.layout.adaptor_content, null);
        holder = new ViewHolder();
        holder.textLine = (TextView) convertView.findViewById(R.id.textLine);
        holder.buttonLine =(Button) convertView.findViewById(R.id.buttonLine);
        convertView.setTag(holder);

    }
    holder =(ViewHolder) convertView.getTag();
    holder.textLine.setText(getItem(position));
    convertView.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(context, "Click-" + String.valueOf(position), Toast.LENGTH_SHORT).show(); 

        }
    });
    holder.buttonLine.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            data.remove(position);
            notifyDataSetChanged();
            Log.i(TAG, "Delete button pressed at position" + position);
            Toast.makeText(context, "Delete-" + String.valueOf(position), Toast.LENGTH_SHORT).show();
        }
    });

    return convertView;
}


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

public String getItem(int position) {
    // TODO Auto-generated method stub
    return data.get(position);
}

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

static class ViewHolder {
    TextView textLine;
    ImageView iconLine;
    Button buttonLine;
    //int position;
}

@Override
public android.widget.Filter getFilter() {
    return null;
}

}

正在运作!测试它:))