2017-02-17 47 views
0

我在製作小應用程序。它在MainActivity上有2個listview。 DB是SQLLite,具有樹cloumns id(int),person(text),status(text)。 Firt列表視圖會從數據庫顯示的信息與此查詢Android如何從Listview中點擊itemid並更新該行的另一列值

select * from DB where status=B 

而旁邊的ListView將顯示信息,其中狀態= A。

lv1.status = b | lv2.status = a


人1 | Person 2


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

}

+0

請顯示完整的代碼示例。請記住,在Java中,所有方法都必須在類中。您還應該顯示您創建任何「OnClickListener」的位置。 –

回答

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()。然而,你沒有展示你目前怎麼稱呼它,所以我無法提供任何建議如何改變這一點。

相關問題