android在动作模式复制点击中复制listview中的一个动态子视图

时间:2014-05-26 12:18:41

标签: android listview duplicate-data

在listview中,我希望创建一个动作(例如COPY),它将在对象列表中复制所选视图(带有新ID)。必须在所选视图旁边创建视图。有什么想法吗?

这是我的适配器

public class Test扩展了BaseAdapter {

Context context;
ArrayList<ListItemVO> itemList;
List<String> editList;

static class ViewHolder {
    public TextView text;
    public EditText editText;
    public ViewSwitcher vs;
}

public Test(Context context, ArrayList<ListItemVO> arr) {
    this.context = context;
    this.itemList = arr;
}

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

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

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

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

    View rowView = convertView;

    ViewHolder holder;
    if (convertView == null) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.listview_item, parent, false);

        holder = new ViewHolder();

        holder.text = (TextView) rowView.findViewById(R.id.textView111);

        holder.editText = (EditText) rowView.findViewById(R.id.editText111);
        holder.vs = (ViewSwitcher) rowView.findViewById(R.id.viewSwitcher111);

        rowView.setTag(holder);

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

    holder.text.setText(itemList.get(position).getName());
    holder.editText.setText(itemList.get(position).getName());

    holder.editText.setTag(R.id.TAG_POSITION_ID, position);
    holder.editText.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                final int position = Integer.parseInt(v.getTag(R.id.TAG_POSITION_ID).toString());
                final EditText editText = (EditText) v;
                String val = editText.getText().toString(); // you have the value here

                if (val.compareTo("") != 0) {
                    itemList.get(position).setName(val);
                }
            }
        }
    });

    holder.vs.setDisplayedChild(0);
    if(itemList.get(position).isEdit()) {
        holder.vs.setDisplayedChild(1);
    }

    return rowView;
}

}

1 个答案:

答案 0 :(得分:0)

为适配器实现适配器onItemClick()并更新项目列表

要删除,请从列表中删除该项目。 要添加,请在该位置添加项目。 编辑,编辑列表。

最后致电adapter.notifyDataSetChanged()