user4247042
user4247042

Reputation:

Listview Duplicating in Android

"Items" json array is Duplicating. In listview the "items" showing same items in every list. how to sort "items" json array.

Json Output

Two Json array bills and items. In this "items" is duplicating I want to display

 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);
 }

Adapter:

    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;
    }
}

}

Upvotes: 2

Views: 104

Answers (1)

Gavriel
Gavriel

Reputation: 19237

Here you return position as item:

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

but in getView you don't use it, instead you use another "definition" of item:

bill_list.get(position)

You should use getItem(position) inside getView, and fix your getItem:

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

And the same goes for the other adapter.

Upvotes: 1

Related Questions