从列表视图中删除项目并在另一个列表视图活动中添加已删除的项目?

时间:2014-10-05 11:50:19

标签: android android-listview

您能举例说明如何将数据从ListView传递到另一个ListView。

例如,我的MainActivity.javaListView,当我删除其中的项目时。我想要实现的是传递并将其添加到另一个ListView活动。

MainActivity.java | OnitemClick

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {

    String member_name = rowItems.get(position).getMember_name();
    int product_icons = rowItems.get(position).getProfile_pic_id();
    String status = rowItems.get(position).getStatus();

    // passing data
    Intent i = new Intent(getBaseContext(), CarActivity.class);
    i.putExtra("member_name", member_name);
    i.putExtra("product_icons", product_icons);
    i.putExtra("status", status);

    RowItem item = rowItems.get(position);

    rowItems.remove(item);

    newadapter = new CustomAdapter(this, rowItems);
    mylistview.setAdapter(newadapter);

    newadapter.notifyDataSetChanged();

    Toast.makeText(this.getApplicationContext(),
            "Added to Cart: " + member_name, Toast.LENGTH_SHORT).show();

}

CartActivity.java |的onCreate

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String member_names = extras.getString("member_name");
        int product_icons = extras.getInt("product_icons");
        String status = extras.getString("status");
    }

    rowItems = new ArrayList<RowItem_cart>();

    RowItem_cart item = new RowItem_cart(member_names, product_icons, status);
    rowItems.add(item);

    mylistview = (ListView) findViewById(R.id.list);

    mylistview.setItemsCanFocus(true);
    //
    final CustomerAdapter_Cart adapter = new CustomerAdapter_Cart(this,
            rowItems);
    mylistview.setAdapter(adapter);

    mylistview.setOnItemClickListener(this);

    mylistview.setScrollingCacheEnabled(false);

    mylistview.invalidateViews();

CustomAdapter_cart

public class CustomerAdapter_Cart extends BaseAdapter {

Context context;
List<RowItem_cart> rowItems;

public CustomerAdapter_Cart(Context context, List<RowItem_cart> rowItems) {
    this.context = context;
    this.rowItems = rowItems;

}

public CustomerAdapter_Cart() {
    // TODO Auto-generated constructor stub
}

@Override
public int getCount() {
    return rowItems.size();
}

@Override
public Object getItem(int position) {
    return rowItems.get(position);
}

@Override
public long getItemId(int position) {
    return rowItems.indexOf(getItem(position));
}

/* private view holder class */
private class ViewHolder {
    ImageView product_icons;
    TextView member_name;
    TextView status;
    Button addtocart;
    EditText txtQuantity;
    TextView lblQty;
}

ViewHolder holder = null;

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

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item_cart, null);
        holder = new ViewHolder();

        holder.member_name = (TextView) convertView
                .findViewById(R.id.member_name);
        holder.product_icons = (ImageView) convertView
                .findViewById(R.id.product_icons);
        holder.status = (TextView) convertView.findViewById(R.id.status);
        holder.addtocart = (Button) convertView
                .findViewById(R.id.btnaddtocart);

        holder.txtQuantity = (EditText) convertView
                .findViewById(R.id.txtQuantity);

        holder.lblQty = (TextView) convertView.findViewById(R.id.lblQty);

        convertView.setTag(holder);

    } else {

        holder = (ViewHolder) convertView.getTag();

    }
    RowItem_cart row_pos = rowItems.get(position);

    holder.product_icons.setImageResource(row_pos.getProfile_pic_id());
    holder.member_name.setText(row_pos.getMember_name());
    holder.status.setText(row_pos.getStatus());
    holder.addtocart.setText(" add to cart");

    return convertView;
    }

  }

RowItem_cart

public class RowItem_cart {

private String member_name;
private int profile_pic_id;
private String status;

public RowItem_cart(String member_name, int profile_pic_id, String status) {

    this.member_name = member_name;
    this.profile_pic_id = profile_pic_id;
    this.status = status;

}

public String getMember_name() {
    return member_name;
}

public void setMember_name(String member_name) {
    this.member_name = member_name;
}

public int getProfile_pic_id() {
    return profile_pic_id;
}

public void setProfile_pic_id(int profile_pic_id) {
    this.profile_pic_id = profile_pic_id;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

  }

1 个答案:

答案 0 :(得分:0)

MainActivity你可能有一个onItemSelected()听众。在那里,您可以获取从活动项目列表中删除的项目。假设您有一个已删除项目列表的字段,您只需使用刚刚删除的项目在该列表上调用add()

如果没有您的某些实际代码,我们可以将您带走;)