重复的值将在android中的listView中出现

时间:2017-02-17 07:52:25

标签: android json listview

我正在使用ListView处理BaseAdapter。我正在显示从JSON文件到ListView的值。我的问题是我只获得每个列表项中的最后一个值,

活动

public class CustomerList extends Activity {
    ImageView ic_back;
    ListView lv_cust;
    LoadJson loadcustomers;
    String customers;
    ArrayList<Customer> customerList;
    CustomerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer_list);
        lv_cust = (ListView) findViewById(R.id.lv_customers);
        ic_back = (ImageView) findViewById(R.id.ic_back);
        loadcustomers = new LoadJson(CustomerList.this, "customers");
        customers = loadcustomers.loadJSONFromAsset();

        ic_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //setting static data to list..
        fetchCustomers();
        adapter = new CustomerAdapter(CustomerList.this, customerList);
        lv_cust.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        //end


    }

    //void fill row data..
    void fetchCustomers() {
        customerList = new ArrayList<>();
        customerList.clear();
        try {
            JSONObject mainObj = new JSONObject(customers);
            JSONObject dataObj = mainObj.getJSONObject("data");
            JSONArray customerArray = dataObj.getJSONArray("result");

            for (int i = 0; i < customerArray.length(); i++) {
                JSONObject c = customerArray.getJSONObject(i);
                Customer customer = new Customer();
                customer.setAccountNumber(c.getString("accountNumber"));
                customer.setAccountStatus(c.getString("accountStatus"));
                customer.setAccountType(c.getString("accountType"));
                customer.setCustomerCategory(c.getString("customerCategory"));
                customerList.add(customer);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
        }

适配器类

public class CustomerAdapter extends BaseAdapter {
    private Context mContext;
    private final ArrayList<Customer> customer;
    Integer selected_position = -1;


    public CustomerAdapter(Context c, ArrayList<Customer> customer) {
        mContext = c;
        this.customer = customer;
    }

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


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

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


    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.raw_customer, parent, false);
            holder = new ViewHolder();
            holder.tv_account_no = (TextView) convertView.findViewById(R.id.tv_act_no);
            holder.tv_act_sts = (TextView) convertView.findViewById(R.id.tv_act_sts);
            holder.tv_act_name = (TextView) convertView.findViewById(R.id.tv_act_name);
            holder.tv_cust_cat = (TextView) convertView.findViewById(R.id.tv_cust_cat);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tv_account_no.setText(customer.get(position).getAccountNumber());
        holder.tv_act_sts.setText(customer.get(position).getAccountStatus());
        holder.tv_act_name.setText(customer.get(position).getAccountType());
        holder.tv_cust_cat.setText(customer.get(position).getCustomerCategory());


        return convertView;
    }

    public static class ViewHolder {
        TextView tv_account_no;
        TextView tv_act_sts;
        TextView tv_act_name;
        TextView tv_cust_cat;
    }
}

josn file

{
  "code": 600,
  "status": "success",
  "message": null,
  "data": {
    "result": [
      {
        "accountNumber": "0000160057",
        "accountStatus": "ACTIVE",
        "accountType": "ACT09",
        "address": "10jan2017wom1fttx5",
        "mobilePhone": "1235123412",
        "phone": null,
        "customerCategory": "VIP"
      },
      {
        "accountNumber": "0000160058",
        "accountStatus": "DEACTIVE",
        "accountType": "ACT11",
        "address": "10jan2017wom1fttx5",
        "mobilePhone": "33408805",
        "phone": null,
        "customerCategory": "VIP"
      },
      {
        "accountNumber": "0000160059",
        "accountStatus": "REGISTERED",
        "accountType": "ACT19",
        "address": "10jan2017wom1fttx5",
        "mobilePhone": "9976878756",
        "phone": null,
        "customerCategory": "Default"
      },
      {
        "accountNumber": "0000160064",
        "accountStatus": "ACTIVE",
        "accountType": "ACT23",
        "address": "10jan2017wom1fttx5",
        "mobilePhone": "3556456745",
        "phone": null,
        "customerCategory": "VIP"
      },
      {
        "accountNumber": "0000160044",
        "accountStatus": "ACTIVE",
        "accountType": "ACT67",
        "address": "10jan2017wom1fttx5",
        "mobilePhone": "3556456745",
        "phone": null,
        "customerCategory": "Default"
      },
      {
        "accountNumber": "0003460097",
        "accountStatus": "REGISTERED",
        "accountType": "ACT56",
        "address": "10jan2017wom1fttx5",
        "mobilePhone": "3556456745",
        "phone": null,
        "customerCategory": "Default"
      },
      {
        "accountNumber": "0000160099",
        "accountStatus": "ACTIVE",
        "accountType": "ACT46",
        "address": "10jan2017wom1fttx5",
        "mobilePhone": "3556456745",
        "phone": null,
        "customerCategory": "Default"
      },
      {
        "accountNumber": "0000160090",
        "accountStatus": "ACTIVE",
        "accountType": "ACT29",
        "address": "10jan2017wom1fttx5",
        "mobilePhone": "3556456745",
        "phone": null,
        "customerCategory": "VIP"
      },
      {
        "accountNumber": "0000160044",
        "accountStatus": "ACTIVE",
        "accountType": "ACT55",
        "address": "10jan2017wom1fttx5",
        "mobilePhone": "3556456745",
        "phone": null,
        "customerCategory": "VIP"
      },
      {
        "accountNumber": "0000160045",
        "accountStatus": "REGISTERED",
        "accountType": "ACT23",
        "address": "10jan2017wom1fttx5",
        "mobilePhone": "3556456745",
        "phone": null,
        "customerCategory": "VIP"
      },
      {
        "accountNumber": "0000160077",
        "accountStatus": "ACTIVE",
        "accountType": "ACT25",
        "address": "10jan2017wom1fttx5",
        "mobilePhone": "3556456745",
        "phone": null,
        "customerCategory": "Default"
      },
      {
        "accountNumber": "0000160075",
        "accountStatus": "REGISTERED",
        "accountType": "ACT99",
        "address": "10jan2017wom1fttx5",
        "mobilePhone": "3556456745",
        "phone": null,
        "customerCategory": "Default"
      }
    ],
    "totalRecords": 14
  }
}

fetchjson

public class LoadJson {
    private Context mContext;
    private final String filename;

    public LoadJson(Context c, String filename) {
        mContext = c;
        this.filename = filename;
    }

    public String loadJSONFromAsset() {
        String json = null;
        try {
            InputStream is = mContext.getAssets().open(filename);
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;
    }
}

3 个答案:

答案 0 :(得分:0)

您应该使用View holder pattern修改getView方法,并将类ViewHolder添加到您的适配器,如下所示:

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder mHolder;
            final Customer c = getItem(position);
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.raw_customer, null);
                mHolder = new ViewHolder();

                mHolder.tv_account_no = (TextView) v.findViewById(R.id.tv_act_no);
                mHolder.tv_act_sts = (TextView) v.findViewById(R.id.tv_act_sts);
                mHolder.tv_act_name = (TextView) v.findViewById(R.id.tv_act_name);
                mHolder.tv_cust_cat = (TextView) v.findViewById(R.id.tv_cust_cat);

                convertView.setTag(mHolder);

            } else {
                mHolder = (ViewHolder) convertView.getTag();
            }

            mHolder.tv_account_no.setText(c.getAccountNumber());
            mHolder.tv_act_sts.setText(c.getAccountStatus());
            mHolder.tv_act_name.setText(c.getAccountType());
            mHolder.tv_cust_cat.setText(c.getCustomerCategory());

            return convertView;
        }

        private class ViewHolder {
            private TextView tv_account_no, tv_act_sts, tv_act_name, tv_cust_cat ;

        }

编辑:尝试也这样做

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

答案 1 :(得分:0)

在fetchCustomers()函数中设置适配器

public class CustomerList extends Activity {
    ImageView ic_back;
    ListView lv_cust;
    LoadJson loadcustomers;
    String customers;
    ArrayList<Customer> customerList;
    CustomerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer_list);
        lv_cust = (ListView) findViewById(R.id.lv_customers);
        ic_back = (ImageView) findViewById(R.id.ic_back);
        loadcustomers = new LoadJson(CustomerList.this, "customers");
        customers = loadcustomers.loadJSONFromAsset();

        ic_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //setting static data to list..
        fetchCustomers();

        //end


    }

    //void fill row data..
    void fetchCustomers() {
        customerList = new ArrayList<>();
        customerList.clear();
        try {
            JSONObject mainObj = new JSONObject(customers);
            JSONObject dataObj = mainObj.getJSONObject("data");
            JSONArray customerArray = dataObj.getJSONArray("result");

            for (int i = 0; i < customerArray.length(); i++) {
                JSONObject c = customerArray.getJSONObject(i);
                Customer customer = new Customer();
                customer.setAccountNumber(c.getString("accountNumber"));
                customer.setAccountStatus(c.getString("accountStatus"));
                customer.setAccountType(c.getString("accountType"));
                customer.setCustomerCategory(c.getString("customerCategory"));
                customerList.add(customer);
            }

adapter = new CustomerAdapter(CustomerList.this, customerList);
        lv_cust.setAdapter(adapter);
        adapter.notifyDataSetChanged();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
        }

尝试这一点,但这没有任何帮助。请评论我会帮助你

答案 2 :(得分:0)

最后......我找到了我的问题的解决方案,我在我的模型类中将字符串定义为“静态”,这是主要问题。我删除了“静态”并解决了问题。

从下面的帖子中获得解决方案。

Repeatable values in list in android