0

I'm working on a ListView with a BaseAdapter. I am showing values from a JSON file to a ListView. My problem is that I am only getting the last value in every list Items,

Activity:

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

    }
        }

Adapter class

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 Answers3

0

You should Use View holder pattern edit your getView method and also add class ViewHolder to you adapter as follows:

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

        }

Edit: try to do that also

@Override
public Customer getItem(int position) {
    // TODO Auto-generated method stub
    return customer.get(position);
}
Atef Hares
  • 4,715
  • 3
  • 29
  • 61
0

Set your adapter in your fetchCustomers() fuction

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

    }
        }

try this and this doesn't help. Please comment me will help you

Zaki Pathan
  • 1,762
  • 1
  • 13
  • 26
  • @Jigar Makwana hope it helps you. I this doesn't help please reply me will give you better answer – Zaki Pathan Feb 17 '17 at 08:26
  • you getting all values in your customerList before adapter set? – Zaki Pathan Feb 17 '17 at 09:33
  • In your customerList after filling all data there is repeated values in this list? Or the above JSON is stored in customerList without repeat? – Zaki Pathan Feb 17 '17 at 09:56
  • I know that. but debug your code and check In your customerList after filling all data there is repeated values in this list? Or the above JSON is stored in customerList without repeat? customerList having repeated value or not? – Zaki Pathan Feb 17 '17 at 10:16
  • hello,I got the issue..I found that in fetchCustomers method i am getting only last values in all the arrayitems..So there must be error in iteration,can u help me for the same? – concentrate H2SO4 Feb 17 '17 at 10:23
  • JSONArray customerArray = dataObj.getJSONArray("result"); what u get in customerArray? you getting correct JSON over here? – Zaki Pathan Feb 17 '17 at 10:26
  • yes i am helping you bro. but you need to debug your code according to my suggestions – Zaki Pathan Feb 17 '17 at 10:26
  • your code is correct according to this. From which URL you getting JSON? Can you share URL? So i will try myself – Zaki Pathan Feb 17 '17 at 11:24
  • remone adapter.notifyDataSetChanged(); from my above answer and try – Zaki Pathan Feb 17 '17 at 11:25
0

Finally...I found the solution to my problem that i have defined the string as "static" in my model classes,that is the main problem.I removed "static" and fixed the issue.

Got solution from below thread.

Repeatable values in list in android

Community
  • 1
  • 1