【问题标题】:repeated values are coming in listView in android重复值出现在 android 的 listView 中
【发布时间】:2023-03-12 10:01:01
【问题描述】:

我正在使用ListViewBaseAdapter。我正在显示从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 文件

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

【问题讨论】:

  • 首先检查是否将正确的列表传递给适配器。放置一个断点并检查您收到的列表。如果正确则它是适配器问题
  • 为什么在设置适配器之后调用 notifyDataSetChanged()?尝试删除它。
  • notifyDataSetChanged() 应该在您对列表进行任何更改并希望您的 listView 反映在该更改中时调用。
  • @swetabhsuman - 删除后仍然发生同样的事情
  • @ArpanSharma - 感谢您的宝贵意见..!!!

标签: android json listview


【解决方案1】:

您应该使用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);
}

【讨论】:

  • @JigarMakwana 你检查过传递给适配器的数据列表吗?对吗?
  • @JigarMakwana 请在getViewgetItem 中回复编辑
  • 你好,我遇到了问题..我发现在 fetchCustomers 方法中我只得到所有数组项中的最后一个值..所以迭代中一定有错误,你能帮我吗?
  • @JigarMakwana 多解释,我不懂你
  • @JigarMakwana 你的fetchCustomers 方法没有问题!顺便说一句,你可以添加loadJSONFromAsset的代码
【解决方案2】:

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

    }
        }

试试这个,这没有帮助。请评论我会帮助你

【讨论】:

  • @Jigar Makwana 希望对您有所帮助。我这没有帮助请回复我会给你更好的答案
  • 您在设置适配器之前获得了 customerList 中的所有值?
  • 在您的customerList 中填写完所有数据后,此列表中有重复值吗?还是上面的JSON不重复存储在customerList中?
  • 我知道。但是调试您的代码并在填写所有数据后检查您的 customerList 中是否存在此列表中的重复值?还是上面的JSON不重复存储在customerList中? customerList 是否具有重复值?
  • 你好,我遇到了问题..我发现在 fetchCustomers 方法中我只得到所有数组项中的最后一个值..所以迭代中一定有错误,你能帮我吗?
【解决方案3】:

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

从下面的线程得到解决方案。

Repeatable values in list in android

【讨论】:

  • 感谢您的时间和支持..(y) :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-29
  • 2021-06-19
  • 2023-02-09
  • 2020-12-27
相关资源
最近更新 更多