Android 如何通过单击表行启动新活动,并在EditText中将数据传递给新活动

Android 如何通过单击表行启动新活动,并在EditText中将数据传递给新活动,android,Android,下面是我的代码,我已经为每个表行设置了OnClickListener,它正在工作。现在,我想在用户单击任何行时启动一个新活动,并且我想在文本编辑中将行的数据传递给新活动 Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+docno.getText()+"'", null); int rows = c.getCount(); tab

下面是我的代码,我已经为每个表行设置了OnClickListener,它正在工作。现在,我想在用户单击任何行时启动一个新活动,并且我想在文本编辑中将行的数据传递给新活动

            Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+docno.getText()+"'", null);

                  int rows = c.getCount();
                  table_layout.removeAllViews();
                  c.moveToFirst();
                  for (int i = 0; i < rows; i++) {

                      String rollno  = c.getString(0);
                      String name    = c.getString(1);
                      String marks   = c.getString(2);
                   TableRow row = new TableRow(this);
                   row.setClickable(true); 
                    row.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                            v.setBackgroundColor(Color.GRAY);
                        }
                    });

                   row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                   LayoutParams.WRAP_CONTENT));

                    TextView et = new TextView(this);
                    TextView name1 = new TextView(this);
                    TextView marks1 = new TextView(this);

                    name1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));

                    et.setGravity(Gravity.CENTER);
                    et.setTextSize(10);
                    et.setText(rollno);

                    name1.setGravity(Gravity.CENTER);
                    name1.setTextSize(10);
                    name1.setText(name);
                    name1.setBackgroundColor(0xFF00FF00);
                    name1.setHeight(20);
                    name1.setWidth(180);

                    marks1.setGravity(Gravity.CENTER);
                    marks1.setTextSize(10);
                    marks1.setText(marks);

                    row.addView(et);
                    row.addView(name1);
                    row.addView(marks1);

                   table_layout.addView(row);
                   c.moveToNext();

                  }
Cursor c=db.rawQuery(“从学生中选择*,其中rollno='”+docno.getText()+”,null);
int rows=c.getCount();
表_layout.removeallview();
c、 moveToFirst();
对于(int i=0;i


特别是“开始第二项活动”部分似乎正是您所寻找的。从所选行获取相关数据,然后使用intent.putExtra(key,value)将其发送到下一个活动。

有多种方法可以实现此目的,下面介绍了几种方法

您可以使用putExtra方法来实现新的意图。您可以通过以文本形式传递数据来实现这一点,也可以公开光标并只传递行号。要以文本形式传递数据,请在onClick中:

Intent intt = new Intent(this,NewActivity.class);
intt.putExtra("rollno",rollno);
intt.putExtra("name",name);
intt.putExtra("marks",marks);
startActivity(intt);
然后在你的新活动中

Bundle extras = getIntent().getExtras();
if(extras != null) {
    // Use extras.getString("rollno") in your setText method in your new activity
    // Use extras.getString("name") in your setText method in your new activity
    // Use extras.getString("marks") in your setText method in your new activity
}
您还可以通过在类内、但在onCreate方法的外部/之前声明游标来公开游标

public static Cursor c;
然后在onCreate中,在开始处删除“Cursor”关键字。然后,您可以在onClick中将数据作为行号传入,如下所示:

Intent intt = new Intent(this,NewActivity.class);
intt.putExtra("rownum",i);
startActivity(intt);
然后在您的新活动中:

Bundle extras = getIntent().getExtras();
int rownum;
if(extras != null) {
    c.moveToPosition(extras.getInt("rownum"));
    et.setText(MainActivity.c.getString(0));
    name1.setText(MainActivity.c.getString(1));
    marks1.setText(MainActivity.c.getString(2));
}
您甚至可以在onClick中跳过通过intent传递数据:

c.moveToPosition(i);
Intent intt = new Intent(this,NewActivity.class);
startActivity(intt);
然后,当您在NewActivity中使用getString()引用MainActivity.c(您的游标对象)时,将已经为您设置了正确的行。但是,这要求始终调用moveToPosition

我还想提出一些其他的建议。您应该为布局对象使用不同的命名约定。使用诸如name、name1、marks、marks1等变量会导致混淆。而是使用name、nameText或marksString、marksTextView

对于db查询,也应该使用query()方法。而不是:

Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+docno.getText()+"'", null);
使用:

这还假设您已将“c”声明为游标,如上所述。您也不能保证您的列将精确地位于您期望的位置,因此不要使用

c.getString(0)
若要检索列0,应改为使用:

c.getString(c.getColumnIndex("columnName");

其中“columnName”是表中列的名称

你建议我使用两种方法来传递数据,一种是以文本形式传递数据,另一种是公开光标,这是永远最好的方法,实际上我是android新手。这完全取决于你的最终目标是什么。理想情况下,新活动不应直接从主活动处理光标。您也可能希望以空值或其他游标开始,因此在bundle中传递字符串。当您得到更复杂的记录时,您可以考虑为每个记录创建一个类并按类引用它。(想象一个C风格的结构实现了它自己的私有方法)。亲爱的Dvaey,我也想消失所选的行,但如果数据成功地进入数据库。你能帮帮我吗?
c.getString(c.getColumnIndex("columnName");