Listview在Android中复制

时间:2016-02-09 10:20:01

标签: android json listview

"项目" json数组是复制。在列表视图中"项目"在每个列表中显示相同的项目。如何排序"项目" json数组。

Json Output

两个Json阵列账单和物品。 在这"项目"正在重复 我想要显示

 JSONArray jArray = json.getJSONArray("bills");
 for (int i = 0; i < jArray.length(); i++) {
     JSONObject jsonObJ_bill = jArray.getJSONObject(i);
     billno = jsonObJ_bill.getString(TAG_BILL_NO);
     amount = jsonObJ_bill.getString(TAG_AMOUNT);
     HashMap<String, String> cus_name_bill = new HashMap<String, String>();
     cus_name_bill.put(TAG_BILL_NAME, billname);
     cus_name_bill.put(TAG_BILL_NO, billno);
     cus_name_bill.put(TAG_AMOUNT, amount);
     JSONArray jsonarray_details = jsonObJ_bill.getJSONArray("items");
     item_lists  = new ArrayList<HashMap<String, String>>();
     for (int j = 0; j < jsonarray_details.length(); j++) {
         JSONObject c = jsonarray_details.getJSONObject(j);
         String itemname = c.getString("item_name");
         String itemprice = c.getString("rate");
         String qty = c.getString("qty");
         String itemamount = c.getString("amount");
         HashMap<String, String> food_item_lists = new HashMap<String, String>();
         food_item_lists.put("item_name", itemname);
         food_item_lists.put("rate", itemprice);
         food_item_lists.put("qty", qty);
         food_item_lists.put("amount", itemamount);
         item_lists.add(food_item_lists);
     }
     all_bill_details.add(cus_name_bill);
 }

适配器:

    Context con;
ArrayList<HashMap<String, String>> bill;
String strdte;
String recorder_no;
ArrayList<HashMap<String, String>> item_lists;
Itemlist itl;
public AllBillinvoiceAdapter(Context con, ArrayList<HashMap<String, String>> bill,ArrayList<HashMap<String, String>> item_lists, String recorder_no) {
    super();
    this.con = con;
    this.bill = bill;
    this.recorder_no = recorder_no;
    this.item_lists = item_lists;
}
@Override
public int getCount() {
    return bill.size();
}
@Override
public Object getItem(int position) {
    return position;
}
@Override
public long getItemId(int position) {
    return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Holder holder = new Holder();
    LayoutInflater layoutInflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowview;
    Calendar c = Calendar.getInstance();
    SimpleDateFormat sdfdate = new SimpleDateFormat("yyyy-MM-dd");
    strdte = sdfdate.format(c.getTime());
    rowview = layoutInflater.inflate(R.layout.allcustomer_bill_item, null);
    holder.billno_txt = (TextView) rowview.findViewById(R.id.bill_no);
    holder.net_amount = (TextView) rowview.findViewById(R.id.amounttobepaid);
    holder.item_Listview=(ListView) rowview.findViewById(R.id.item_listview);
    holder.billno_txt.setText("Bill No : $ " +bill.get(position).get(Splitbill_OrderSummaryFragment.TAG_BILL_NAME));
    holder.tableamount.setText("Bill Amount : $ " + bill.get(position).get(Splitbill_OrderSummaryFragment.TAG_AMOUNT));
    holder.net_amount.setText("Amount to be paid : $ " + bill.get(position).get(Splitbill_OrderSummaryFragment.TAG_NET_AMOUNT));

    if(item_lists != null && item_lists.size() > 0) {
        itl = new Itemlist(con, item_lists);
        holder.item_Listview.setAdapter(itl);
    }
    return rowview;

}

public class Holder {
    TextView billno_txt, net_amount;
    ListView item_Listview;
}

private class Itemlist extends BaseAdapter{
    Context context;
    ArrayList<HashMap<String, String>> bill_list;

    public Itemlist(Context context, ArrayList<HashMap<String, String>> bill_list) {
        super();
        this.context=context;
        this.bill_list=bill_list;
    }
    @Override
    public int getCount() {
        return bill_list.size();
    }
    @Override
    public Object getItem(int position) {
        return position;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = new Holder();
        View rowview;
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowview = layoutInflater.inflate(R.layout.itemlistview, null);
        holder.item_name=(TextView) rowview.findViewById(R.id.item_listview_text);
        holder.item_price=(TextView) rowview.findViewById(R.id.price);
        holder.item_qty=(TextView) rowview.findViewById(R.id.qty);
        holder.item_total=(TextView) rowview.findViewById(R.id.total);
        holder.item_name.setText(bill_list.get(position).get("item_name"));
        holder.item_price.setText(bill_list.get(position).get("rate"));
        holder.item_qty.setText(bill_list.get(position).get("qty"));
        holder.item_total.setText(bill_list.get(position).get("amount"));
        return rowview;
    }
    private class Holder {
        TextView item_name,item_price,item_qty,item_total;
    }
}

}

1 个答案:

答案 0 :(得分:1)

在这里你将位置作为项目返回:

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

但是在getView中你不能使用它,而是使用另一个&#34;定义&#34;项目:

bill_list.get(position)

你应该在getView中使用getItem(position),并修复你的getItem:

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

另一个适配器也是如此。