android的listView中出现了重复的值

android的listView中出现了重复的值,android,json,listview,Android,Json,Listview,我正在使用BaseAdapter开发一个ListView。我正在显示从JSON文件到ListView的值。我的问题是我只得到每个列表项的最后一个值 活动: public class CustomerList extends Activity { ImageView ic_back; ListView lv_cust; LoadJson loadcustomers; String customers; ArrayList<Customer> cu

我正在使用
BaseAdapter
开发一个
ListView
。我正在显示从
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();
        }

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

您应该使用
ViewHolder模式
编辑您的
getView
方法,并将class
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);
}

在fetchCustomers()功能中设置适配器

公共类CustomerList扩展活动{
ImageView集成电路背面;
ListView lv_cust;
LoadJson loadcustomers;
串客户;
ArrayList客户列表;
客户适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u customer\u list);
lv_cust=(ListView)findViewById(R.id.lv_客户);
ic_back=(ImageView)findViewById(R.id.ic_back);
loadcustomers=newloadjson(CustomerList.this,“customers”);
customers=loadcustomers.loadJSONFromAsset();
ic_back.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
完成();
}
});
//将静态数据设置为列表。。
获取客户();
//结束
}
//空白填充行数据。。
void fetchCustomers(){
customerList=新的ArrayList();
customerList.clear();
试一试{
JSONObject mainObj=新的JSONObject(客户);
JSONObject dataObj=mainObj.getJSONObject(“数据”);
JSONArray customerArray=dataObj.getJSONArray(“结果”);
对于(int i=0;i

试试这个,这个没用。请评论我将帮助您

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

从下面的线程获得解决方案


首先检查是否将正确的列表传递给适配器。放置一个断点并检查您收到的列表。如果正确,则是适配器问题设置适配器后,为什么要调用notifyDataSetChanged()?尝试删除它。当您对列表进行了任何更改,并希望列表视图反映该更改时,应调用notifyDataSetChanged()。@swetabhsuman-已删除的仍然是相同的事情on@ArpanSharma-谢谢你的宝贵意见@JigarMakwana您是否检查了传递给适配器的数据列表?是否正确?@JigarMakwana请在
getView
getItem
中编辑答案,您好,我遇到了问题。我发现在fetchCustomers方法中,我只得到所有数组项中的最后一个值。所以迭代中一定有错误,您能帮我解决同样的问题吗?@JigarMakwana进一步解释,我不明白you@JigarMakwana您的
fetchCustomers
方法没有问题!顺便说一句,你能添加
loadJSONFromAsset
@Jigar Makwana的代码吗?希望对你有所帮助。我这没有帮助,请回答我会给你更好的答案你在设置适配器之前在customerList中获取所有值?在填充所有数据后在customerList中有重复的值吗?或者上面的JSON存储在customerList中,没有重复?我知道。但在填充所有数据后,调试代码并签入customerList此列表中是否有重复的值?或者上述JSON存储在customerList中而不重复?customerList是否有重复值?您好,我遇到了问题..我发现在fetchCustomers方法中,我只获取所有arrayitems中的最后一个值..因此在迭代中一定有错误,您能帮我做同样的事情吗?感谢B其他人的时间和支持..(y):)
        @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);
}
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();
        }

    }
        }