android - Android 中的 ListView 复制

标签 android json listview

“项目”json 数组正在复制。在 ListView 中,“项目”在每个列表中显示相同的项目。如何对“项目”json 数组进行排序。

Json Output

两个 Json 数组 bills 和 items。 在这个“项目”中是重复的 我要展示

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

最佳答案

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

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

但在 getView 中你不使用它,而是使用项目的另一个“定义”:

bill_list.get(position)

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

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

另一个适配器也是如此。

关于android - Android 中的 ListView 复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35289430/

相关文章:

java - 在 Android 中从 SQLite 填充 TextViews 和按钮

android - 如何避免屏幕键盘覆盖android中的EditText

android - 仅当版本为 11+ 时如何使用函数

android - 在用户键入主题标签时更改文本的颜色

javascript - 在不知道深度的情况下移除另一个对象内的对象

ruby-on-rails - 如何在 ActionController::TestCase 请求中设置内容类型

android - Activity 中的全屏背景图像

mysql - 在 MySQL 中提取 JSON 提取的第一个键

android - 在 ListView 之间添加布局

android - notifyDataSetChanged()事件