使用getText()。toString();作为Database Query Filter

时间:2015-02-23 11:29:41

标签: android sqlite

我刚学习Android和数据库,并希望得到一些帮助,希望我能说清楚。

我有一个Activity类,我在其中获取getText()。toString()值,当我记录返回的值时它工作正常,我想取这个值并在我的DatabaseHelper类中使用它来过滤我的查询来自用户点击的价值。

活动类:

    @Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
    // TODO Auto-generated method stub

    LinearLayout ll = (LinearLayout) viewClicked;                            
    TextView tv = (TextView) ll.findViewById(R.id.textViewArtistsSingle); 
    String tvName = tv.getText().toString()     

    // tv.getText().toString() will return JOHN if the user clicked on John

    startActivity(new Intent(CURRENT_ACTIVITY.this, NEXT_ACTIVITY.class));

DatabaseHelper类:

    public List<String> getDBNameSingle(){

    List<String> listRows = new ArrayList<String>();
    SQLiteDatabase db = this.getWritableDatabase();

    Cursor c;

    try {

        c = db.rawQuery("SELECT name FROM " + TABLE_LM + " WHERE name = 'JOHN'", null); //Get Specific Name HARD)

我想要这样:(注意来自getText()的tvName。toString()

c = db.rawQuery("SELECT name FROM " + TABLE_LM + " WHERE name = '" + tvName +"'", null); //Get Specific Name VARIABLE)

INSTEAD of this:

c = db.rawQuery("SELECT name FROM " + TABLE_LM + " WHERE name = 'JOHN'", null); //Get Specific Name HARD)

但我的DatabaseHelper类不知道tvName,我如何将此变量从CURRENT_ACTIVITY传递给DatabaseHelper,以便我可以过滤name列以返回行WHERE name = tvName(来自getText()。toString();)

1 个答案:

答案 0 :(得分:1)

  

如何将此变量从CURRENT_ACTIVITY传递到DatabaseHelper

在将tvName传递给DatabaseHelper之前,需要在tvName课程中获取NEXT_ACTIVITY

1。使用Intent.putExtratvName字符串值从NEXT_ACTIVITYCURRENT_ACTIVITY发送到ListView

2。tvName方法中,使用onCreate中的getIntent()获取NEXT_ACTIVITY方法中的tvName字符串值。

3。要在DatabaseHelper中获取getDBNameSingle变量值,请将String参数添加到 public List<String> getDBNameSingle(String strName){ // Use strName for passing selected name in query .... } 方法:

{{1}}