首页 新闻 会员 周边 捐助

AS将SQLite中数据使用ViewHolder显示在ListView中出现数据重复问题

0
[已解决问题] 解决于 2020-04-10 17:00
public class DataStoreArrayAdapter extends ArrayAdapter<User> {
    private ArrayList<User> users;
    private Context context;

    public DataStoreArrayAdapter(@NonNull Context context, int resource, @NonNull ArrayList<User> users) {
        super(context, resource, users);
        this.context = context;
        this.users = users;
    }

    @Override
    public int getCount() {
        return users.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        User user = users.get(position);
        ViewHolder viewHolder;
        String number = user.getNumber(), name = user.getName(), password = user.getPassword();
        if(convertView == null){
            convertView = LayoutInflater.from(this.context).inflate(R.layout.user_page, null);
            viewHolder= new ViewHolder();
            viewHolder.textView = convertView.findViewById(R.id.tv_show);
            convertView.setTag(viewHolder);
        } else{
            viewHolder = (ViewHolder)convertView.getTag();
        }
        viewHolder.textView.setText("编号:" + number + " 用户名:" + name + " 密码:" + password);
        return convertView;
    }

    private class ViewHolder{
        private TextView textView;
    }
}
public class DataStoreActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btnAdd, btnUpdate, btnDelete, btnDeleteAll, btnRefresh;
    private EditText edTName, edTPassword, edTNumber;
    private ListView listView;
    private DataStoreDatabaseHelper helper;
    private User user = new User();
    private ArrayList<User> users = new ArrayList<>();
    private DataStoreArrayAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_store);
        btnAdd = findViewById(R.id.btn_add);
        btnUpdate = findViewById(R.id.btn_update);
        btnDelete = findViewById(R.id.btn_delete);
        btnDeleteAll = findViewById(R.id.btn_delete_all);
        btnRefresh = findViewById(R.id.btn_refresh);
        edTName = findViewById(R.id.edt_name);
        edTPassword = findViewById(R.id.edt_password);
        edTNumber = findViewById(R.id.edt_number);
        listView = findViewById(R.id.lv_show);
        btnAdd.setOnClickListener(this);
        btnUpdate.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnDeleteAll.setOnClickListener(this);
        btnRefresh.setOnClickListener(this);
        helper = new DataStoreDatabaseHelper(DataStoreActivity.this, "user.db", null, 1);
    }


    @Override
    public void onClick(View v) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("number", edTNumber.getText().toString());
        cv.put("name", edTName.getText().toString());
        cv.put("password", edTPassword.getText().toString());
        switch (v.getId())
        {
            case R.id.btn_add:
                Cursor cursor = db.query("user",new String[]{"number"},"number=?",new String[]{cv.get("number").toString()},null,null,null);
                if(cursor.getCount() == 0){
                    db.insert("user", null, cv);
                    Toast.makeText(DataStoreActivity.this, "添加成功", Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(DataStoreActivity.this, "编号已存在", Toast.LENGTH_LONG).show();
                }
                cursor.close();
                break;
            case R.id.btn_update:
                db.update("user", cv, "number=?", new String[]{cv.get("number").toString()});
                Toast.makeText(DataStoreActivity.this, "修改成功", Toast.LENGTH_LONG).show();
                break;
            case R.id.btn_delete:
                db.delete("user", "number=?", new String[]{cv.get("number").toString()});
                Toast.makeText(DataStoreActivity.this, "删除成功", Toast.LENGTH_LONG).show();
                break;
            case R.id.btn_delete_all:
                db.execSQL("delete from user");
                Toast.makeText(DataStoreActivity.this, "全部删除", Toast.LENGTH_LONG).show();break;
            case R.id.btn_refresh:
                users.clear();
                cursor = db.query("user",null,null,null,null,null,null);
                //cursor = db.rawQuery("select * from user", null);
                if(cursor != null && cursor.getCount() >=1){
                    while(cursor.moveToNext()){
                        user.setNumber(cursor.getString(1));
                        user.setName(cursor.getString(2));
                        user.setPassword(cursor.getString(3));
                        users.add(user);
                    }
                }
                cursor.close();
                /*for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
                    user.setNumber(cursor.getString(1));
                    user.setName(cursor.getString(2));
                    user.setPassword(cursor.getString(3));
                    users.add(user);
                }*/
                adapter = new DataStoreArrayAdapter(DataStoreActivity.this, R.layout.user_page, users);
                listView.setAdapter(adapter);
                Toast.makeText(DataStoreActivity.this,"刷新成功",Toast.LENGTH_LONG).show();
                break;
        }
        db.close();
    }
}


第一条应该显示:编号:1用户名:1密码:1,但是这里重复显示了

這是記憶中的一本書!的主页 這是記憶中的一本書! | 菜鸟二级 | 园豆:222
提问于:2020-04-10 15:46
< >
分享
最佳答案
1
while(cursor.moveToNext()){
  user.setNumber(cursor.getString(1));//这里循环设置的同一个对象的值。
  user.setName(cursor.getString(2));
  user.setPassword(cursor.getString(3));
  users.add(user);//同一个对象add了多次。
}

修改一下,在循环内部定义user变量,即每条记录,需要new一个User对象,加到users集合中。

while(cursor.moveToNext()){
  User user = new User();//这里定义user对象
  user.setNumber(cursor.getString(1));
  user.setName(cursor.getString(2));
  user.setPassword(cursor.getString(3));
  users.add(user);
}

另外删除上面的user定义,定义在类级别没有用,且容易出错,在使用的时候声明为局部变量即可。

public class DataStoreActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btnAdd, btnUpdate, btnDelete, btnDeleteAll, btnRefresh;
    private EditText edTName, edTPassword, edTNumber;
    private ListView listView;
    private DataStoreDatabaseHelper helper;
    private User user = new User();//删除这一行,不要定义成对象或类级别的变量。
奖励园豆:5
。淑女范erり | 小虾三级 |园豆:961 | 2020-04-10 16:31

非常感谢!!!

這是記憶中的一本書! | 园豆:222 (菜鸟二级) | 2020-04-10 16:59
其他回答(1)
0

convertView=layoutInflater.inflate(R.layout.user_page,null);
改成这样看看,没发现其他地方问题

s_p | 园豆:140 (初学一级) | 2020-04-10 16:24

感谢回答!!!

清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册