如何在列表视图中仅显示所选记录?

时间:2019-04-15 07:54:18

标签: android

我只想在列表视图中显示数据库中的一条记录。 (登录时输入的密码记录)。

数据库文件代码[DatabaseHelper.java]

public static final String TABLE_NAME = "Master";   
public static final String ColumnID = "user_id";    
public static final String Column_2 = "f_name";    
public static final String Column_3 = "l_name";  
public static final String Column_4 = "password";

在数据库文件中选择记录代码

public Cursor SelectRecord(String pass) {  

    SQLiteDatabase db = this.getReadableDatabase();

    Cursor c=db.rawQuery("SELECT * FROM TABLE_NAME WHERE password = ?",new String[]{pass});

    if (c != null)
        c.moveToFirst();
    return c;
 }

JAVA文件代码(要执行的代码)[Edit.java]

final DatabaseHelper db=new DatabaseHelper(getApplicationContext());

 Cursor c =db.SelectRecord(t1.setText(getIntent().getStringExtra("text")));

 String[] from={"_id","f_name","l_name","password"};

 int[] to={R.id.textView1,R.id.textView2,R.id.textView3,R.id.textView4};

 SimpleCursorAdapter adapter = new   SimpleCursorAdapter(Edit.this,R.layout.list_data,c,from,to);

 //ListView lv=(ListView)findViewById(R.id.listview1);
 //  lv.setAdapter(adapter);

登录文件代码[LogIn.java]

 if (database.checkdata(e1.getText().toString().trim()))
     {
          Intent i=new Intent(Login.this,Edit.class);

          i.putExtra("text",e1.getText().toString());

          startActivity(i);

          Toast.makeText(getApplicationContext(), "Login Successfully Done", Toast.LENGTH_LONG).show();

           e1.getText().clear();
     }

我只希望在列表视图中获得一条记录,该记录是我在登录时输入的密码。

修改

现在此代码正确

Cursor c =db.SelectRecord(getIntent().getStringExtra("text"));

1 个答案:

答案 0 :(得分:0)

如果只想根据密码获得一条记录,则需要将参数传递给SelectRecord方法。

假设用户密码之一是“ pass”

Cursor c = db.SelectRecord("pass");

选择记录代码

  public Cursor SelectRecord(String pass) {    

    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.rawQuery("SELECT * FROM YOUR_TABLE_NAME
    WHERE password = ? LIMIT 1", new String[]{pass});

    if (cursor != null)
         cursor.moveToFirst();

    return cursor;
}

修改

从我们的对话中:

  

错误在光标行中:光标c =   db.SelectRecord(t1.setText(getIntent()。getStringExtra(“ text”))));

     

错误显示DatabaseHelper中的SelectRecord(Java.lang.String)   不能应用于(void)

此行中有错误

Cursor c =db.SelectRecord(t1.setText(getIntent().getStringExtra("text")));

您需要将“文本”键从“登录”传递到Java File代码,因此请将此行修改为

Cursor c =db.SelectRecord(getIntent().getStringExtra("text"));

不需要setText