如何传递以下代码中的参数?谁能告诉我怎么样?

时间:2014-01-18 06:34:24

标签: android

我想在搜索功能(在文本字段中) id 中给出一个参数,通过获取该ID,它应该检索单个记录并显示该ID详细信息。而不是给出来自emp的select *,其中id = 12345;

输出:

输入ID: 12345

在行视图中显示数据

UserActivity.java

public class UserActivity extends Activity {

SQLiteDatabase db;
TableRow tableRow;
TextView textview,textview1,textview2,textview3,textview4,textview5;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user);
    db=openOrCreateDatabase("EMP",MODE_PRIVATE,null);
}



public void Search(View view)
{   
Cursor c=db.rawQuery("SELECT * FROM EMP where id=225618",null);
int count=c.getCount();
c.moveToFirst();
TableLayout tableLayout= new TableLayout(getApplicationContext());
tableLayout.setVerticalScrollBarEnabled(true);
TableRow tableRow;
TextView textview,textview1,textview2,textview3,textview4,textview5;
tableRow=new TableRow(getApplicationContext());
textview=new TextView(getApplicationContext());
textview.setText("Empid");
textview.setTextColor(Color.RED);
textview.setTypeface(null,Typeface.BOLD);
textview.setPadding(10, 8, 3, 5);
tableRow.addView(textview);
textview4=new TextView(getApplicationContext());
textview4.setText("Ename");
textview4.setTextColor(Color.RED);
textview4.setTypeface(null,Typeface.BOLD);
textview4.setPadding(35, 8, 3, 5);
tableRow.addView(textview4);
textview5=new TextView(getApplicationContext());
textview5.setText("Email");
textview5.setTextColor(Color.RED);
textview5.setTypeface(null,Typeface.BOLD);
textview5.setPadding(60, 8, 3, 5);
tableRow.addView(textview5);
tableLayout.addView(tableRow);
for (Integer j=0; j< count; j++)
{
tableRow= new TableRow(getApplicationContext());
textview1=new TextView(getApplicationContext());
textview1.setText(c.getString(c.getColumnIndex("id")));
textview2=new TextView(getApplicationContext());
textview2.setText(c.getString(c.getColumnIndex("name")));
textview3=new TextView(getApplicationContext());
textview3.setText(c.getString(c.getColumnIndex("email")));
textview1.setPadding(10, 5, 3, 5);
textview2.setPadding(35, 5, 3, 5);
textview3.setPadding(60, 5, 3, 5);
tableRow.addView(textview1);
tableRow.addView(textview2);
tableRow.addView(textview3);
tableLayout.addView(tableRow);
c.moveToNext();
}
setContentView(tableLayout);
db.close();
}

2 个答案:

答案 0 :(得分:0)

您可以使用setTag()

传递包含视图的参数

答案 1 :(得分:0)

在search()方法中。首先获取edittext字段数据。然后在查询中提及:

类似的东西:

public void Search(View view)
{  
  EditText edittext=(EditText) findViewById(edit field id);
  String textfiled_id=edittext.getText().toString();
  Cursor c=db.rawQuery("SELECT * FROM EMP where id='"+textfiled_id+"'",null);
  ............
  ........... your code
}