在android动态列表视图中发送之前添加数据

时间:2015-05-24 10:11:58

标签: android json listview dynamic android-listview

我有动态列表视图活动,它将项目转移到另一个具有编辑文本(名称,电话,表格)和另一个动态列表视图的活动。

每个项目都有编辑文本,应该与每个项目一起发送。

public class OrderAdapter extends BaseAdapter {

private Context ctx;
private LayoutInflater mInflater;
private List<Item> orderItems;
private DatabaseHelper helper;

public OrderAdapter(Context context, List<Item> items) {
    // TODO Auto-generated constructor stub

    ctx = context;
    helper = new DatabaseHelper(ctx);
    mInflater = LayoutInflater.from(ctx);
    orderItems = items;
}

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

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

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


public class ViewHolder {

    TextView name, desc, price, total;
    EditText etQty;
    Button btnDelete, btnAdd, btnMinus;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    final ViewHolder holder;

    if (convertView == null) {

        convertView = mInflater.inflate(R.layout.order_preview_row, parent,
                false);

        holder = new ViewHolder();
        holder.name = (TextView) convertView.findViewById(R.id.c_table);
        holder.desc = (TextView) convertView.findViewById(R.id.order_item_name);
        holder.price = (TextView) convertView.findViewById(R.id.tvOpPrice);
        holder.etQty = (EditText) convertView.findViewById(R.id.etQty);

        holder.btnAdd = (Button) convertView
                .findViewById(R.id.btnIncreaseQty);

        holder.btnAdd.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                float total_qty = (Float.parseFloat(holder.etQty.getText()
                        .toString()) + 1);

                Item item = (Item) holder.etQty.getTag();
                item.setNoItems(String.valueOf(total_qty));

                holder.etQty.setText(String.valueOf(total_qty));

                holder.total.setText(" = "
                        + (Float.parseFloat(orderItems.get(position)
                                .getItemPrice()) * Float
                                .parseFloat(holder.etQty.getText().toString())));
            }
        });

        holder.btnMinus = (Button) convertView
                .findViewById(R.id.btnDecreaseQty);

        holder.btnMinus.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Float total_qty = (Float.parseFloat(holder.etQty.getText()
                        .toString()) - 1);

                if (total_qty != 0) {

                    Item item = (Item) holder.etQty.getTag();
                    item.setNoItems(String.valueOf(total_qty));

                    holder.etQty.setText(String.valueOf(total_qty));

                    holder.total.setText(" = "
                            + (Float.parseFloat(orderItems.get(position)
                                    .getItemPrice()) * Float
                                    .parseFloat(holder.etQty.getText()
                                            .toString())));

                }
            }
        });

        holder.btnDelete = (Button) convertView
                .findViewById(R.id.btnDeleteItem);

        holder.btnDelete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                helper.deleteItem(orderItems.get(position).getItemID());
                orderItems.remove(position);
                notifyDataSetChanged();
            }
        });

        holder.total = (TextView) convertView.findViewById(R.id.tvTotal);
        holder.etQty.setTag(orderItems.get(position));
        convertView.setTag(holder);
    } else {

        holder = (ViewHolder) convertView.getTag();
        holder.etQty.setTag(orderItems.get(position));
    }

    holder.name.setText(orderItems.get(position).getItemName());
    holder.desc.setText(orderItems.get(position).getItemDescription());
    holder.price.setText(orderItems.get(position).getItemPrice() + " x ");

    holder.etQty.setText(orderItems.get(position).getNItems());

    holder.total
            .setText(" = "
                    + (Float.parseFloat(orderItems.get(position)
                            .getItemPrice()) * Float
                            .parseFloat(holder.etQty.getText().toString())));

    return convertView;
}

}

我的问题是如何从编辑文本中调用数据以将其与每个项目一起添加

例如项目名称是:&#34; Cheese Pizza&#34;我想要它&#34; 2芝士披萨&#34;

@Override
        protected Boolean doInBackground(Void... params) {
            // TODO Auto-generated method stub

            item_ids = new StringBuilder();

            for (int i = 0; i < orderItems.size(); i++) {
                item_ids.append(orderItems.get(i).getItemID()).append(",");
            }

            if (item_ids.length() == 0)
                return false;
            else
                item_ids.deleteCharAt(item_ids.length() - 1);

            Log.d("OrderPreview", item_ids.toString());

            item_names = new StringBuilder();

            for (int i = 0; i < orderItems.size(); i++) {
                item_names.append(orderItems.get(i).getItemName()).append(",");
            }

            if (item_names.length() == 0)
                return false;
            else
                item_names.deleteCharAt(item_names.length() - 1);

            Log.d("OrderPreview", item_names.toString());



            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(Utils.WS_ADD_ORDER);

            List<NameValuePair> mParams = new ArrayList<NameValuePair>();

            mParams.add(new BasicNameValuePair("name", etName.getText()
                    .toString()));
            mParams.add(new BasicNameValuePair("phone", etPhone.getText()
                    .toString()));
            mParams.add(new BasicNameValuePair("table", etTable.getText()
                    .toString()));
            mParams.add(new BasicNameValuePair("order_status", "Processing"));
            mParams.add(new BasicNameValuePair("id", item_ids.toString()));
            mParams.add(new BasicNameValuePair("items", item_names.toString()));

1 个答案:

答案 0 :(得分:0)

在适配器中编写一个方法,以在现有项目列表中添加新项目。 当您从服务/ API获取新数据集时,在异步任务中调用该方法。不要忘记将notifyDataSetChanged()放在适配器内的该方法中。