Android如何从Listview中获取单击的itemid并更新此行上的其他列值

时间:2017-02-17 13:27:06

标签: android sqlite listview

我正在制作小应用。它在MainActivity上有2个列表视图。 DB是SQLLite,具有树cloumns id(int),person(文本),status(文本)。 Firt listview将使用此查询显示来自DB的信息

    select * from DB where status=B

下一个ListView将显示status = A的信息。

lv1.status = b | lv2.status =一

人1 |人2

人3 |人4

当我点击项目上的lv2时,点击了lv2字段的值' status'必须改为' b'。 但我无法为db编写正确的查询。

public void changeUser(){
    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put(KEY_STATUS, "B");

    db.update(TABLE_ORDER, values, null, null);
    db.close();

}

由于

这是我的代码

 lvB = (ListView)findViewById(R.id.lvB);
    listClientB();

    lvA = (ListView)findViewById(R.id.lvA);
    listClientA();

    lvA.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            User user = (User)adapterView.getAdapter().getItem(i);

            int id = user.get_id();

            if (user.getStatus().contains("A")){
                dbHelper.changeUser();
            }

            Toast.makeText(getApplicationContext(), id + "-NUMBER id", Toast.LENGTH_LONG).show();
            Log.d(String.valueOf(user.get_id()), "-NUMBER id");

            listClientA();

            Log.d(user.getStatus(), "Pressed");
        }
    });

}

private void listClientA(){
    list = dbHelper.allUsersA();
    klientStatusAdapter = new KlientStatusAdapter(MainActivity.this, list);
    lvA.setAdapter(klientStatusAdapter);
    lvA.setTextFilterEnabled(true);
}

private void listClientB(){
    list = dbHelper.allUsersB();
    klientStatusAdapter = new KlientStatusAdapter(MainActivity.this, list);
    lvB.setAdapter(klientStatusAdapter);
    lvB.setTextFilterEnabled(true);
}

这是来自DB

public List<User> allUsersA(){
    db = this.getReadableDatabase();
    List<User> users = new ArrayList<User>();
    String s = "select * from " + TABLE_ORDER + " where status = 'A'";
    Cursor cursor = db.rawQuery(s, null);
    if (cursor.moveToFirst()){
        do {
            User user = new User();
            user.set_id(Integer.parseInt(cursor.getString(0)));
            user.setClientName(cursor.getString(1));
            user.setCleintOrderedFood(cursor.getString(2));
            user.setStatus(cursor.getString(3));

            users.add(user);
        }while (cursor.moveToNext());
    }
    db.close();
    return users;
}

public List<User> allUsersB(){
    db = this.getReadableDatabase();
    List<User> users = new ArrayList<User>();
    String s = "select * from " + TABLE_ORDER + " where status = 'B'";
    Cursor cursor = db.rawQuery(s, null);
    if (cursor.moveToFirst()){
        do {
            User user = new User();
            user.set_id(Integer.parseInt(cursor.getString(0)));
            user.setClientName(cursor.getString(1));
            user.setCleintOrderedFood(cursor.getString(2));
            user.setStatus(cursor.getString(3));

            users.add(user);
        }while (cursor.moveToNext());
    }
    db.close();
    return users;
}


public void changeUser(){
    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put(KEY_STATUS, "B");

    db.update(TABLE_ORDER, values, null, null);
    db.close();

}

这是适配器

public class ClientStatusAdapter extends BaseAdapter{
LayoutInflater inflater;
Context context;
List<User> wordsList;
DbHelper dbHelper;

public ClientStatusAdapter(Context context1, List<User> wordsList) {
    this.context = context1;
    this.wordsList = wordsList;
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    dbHelper = new DbHelper(context);
}

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

@Override
public Object getItem(int i) {
    return wordsList.get(i);
}

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

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    if (view == null){
        view = inflater.inflate(R.layout.kliyent_status_adapter, null);
    }

    TextView txtIsmAdapter = (TextView)view.findViewById(R.id.txtIsmAdapter);
    TextView txtOvqatAdapter = (TextView)view.findViewById(R.id.txtOvqatAdapter);

    final User user = wordsList.get(i);

    TextView txtCliyentNames = (TextView)view.findViewById(R.id.txtCliyentNames);
    txtCliyentNames.setText(user.getClientName());

    TextView txtCliyentOrderedFoood = (TextView)view.findViewById(R.id.txtCliyentOrderedFoood);
    txtCliyentOrderedFoood.setText(user.getCleintOrderedFood());

    TextView txtStatusAdapter = (TextView)view.findViewById(R.id.txtStatusAdapter);
    txtStatusAdapter.setText(user.getStatus());

    notifyDataSetChanged();
    ImageView imgOn = (ImageView) view.findViewById(R.id.imgOn);

    return view;
}

}

这是实体用户

public class User {

private int _id;
private String clientName;
private String cleintOrderedFood;
private String status = "A";

public User() {
}

public User(int _id, String clientName, String cleintOrderedFood) {
    this._id = _id;
    this.clientName = clientName;
    this.cleintOrderedFood = cleintOrderedFood;
}

public User(int _id, String clientName, String cleintOrderedFood, String status) {
    this._id = _id;
    this.clientName = clientName;
    this.cleintOrderedFood = cleintOrderedFood;
    this.status = status;
}

public int get_id() {
    return _id;
}

public void set_id(int _id) {
    this._id = _id;
}

public String getClientName() {
    return clientName;
}

public void setClientName(String clientName) {
    this.clientName = clientName;
}

public String getCleintOrderedFood() {
    return cleintOrderedFood;
}

public void setCleintOrderedFood(String cleintOrderedFood) {
    this.cleintOrderedFood = cleintOrderedFood;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

}

1 个答案:

答案 0 :(得分:0)

如果仔细查看SQLiteDatabase.update()方法,您会看到它被声明为

int update (String table, 
            ContentValues values, 
            String whereClause, 
            String[] whereArgs)

注意最后两个参数。这些是您选择要更新的行的方式。例如,您可以指定仅更新具有给定id的行:

public void changeUser(int userId){
    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put(KEY_STATUS, "B");

    String whereClause = "_id = ?";
    String where = new String[] {Integer.toString(userId)};

    db.update(TABLE_ORDER, values, whereClause, where);
    db.close();

}

这里我假设您使用传统的列名_id。当然,如果您有不同的列名,可以根据需要进行更改。

请注意,您现在需要将参数传递给changeUser()。但是,您还没有显示您当前如何调用它,因此我无法提供任何有关如何更改此内容的建议。