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