Android 用另一个类的数据填充一个类的textview
我已经开发了一些短信应用程序,我不知道如何解决这个问题。。。在一个布局中,我显示了两个文本视图来显示短信的号码和日期。。。这很好用 但现在我正在实现onListItemClick,我希望当单击列表项时,数据库中的所有消息都应该加载到新视图中。。。。我怎样才能做到这一点Android 用另一个类的数据填充一个类的textview,android,Android,我已经开发了一些短信应用程序,我不知道如何解决这个问题。。。在一个布局中,我显示了两个文本视图来显示短信的号码和日期。。。这很好用 但现在我正在实现onListItemClick,我希望当单击列表项时,数据库中的所有消息都应该加载到新视图中。。。。我怎样才能做到这一点 protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method s
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
dba.open();
Cursor c=dba.executeQuery("select rowid _id,* from messages where rowid="+position+";");
Log.v(tag, "value count"+ c.getCount());
if(c.getCount()>0)
{
Log.v(tag, "value count"+c.getCount());
c.moveToPosition(position);
Intent intent = new Intent();
String mesage = c.getString(c.getColumnIndex("msg"));
Log.v("Value passed to class", mesage);
intent.putExtra(name, mesage);
intent.setClass(this, NewInterface.class);
startActivity(intent);
}
}
这是我要传递数据的另一个类的代码
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.messages);
tv4 = (TextView) findViewById(R.id.tvMessages);
Bundle bundle=getIntent().getExtras();
if(bundle!=null)
{
Log.v("Value got from class", bundle.getString("name"));
tv4.setText(bundle.getString("name"));
}
列表视图通过此方法从包含电话号码的数据库中填充
私有void openAndQueryDatabase(){
单击列表项时,您将打开一个数据库,根据单击的位置获取数据。与其尝试在第一个活动中读取整个数据,不如使用Intent将int位置传递给下一个活动,然后在那里进行数据库查询。这将更简单、更有意义。如果您仍然希望传递整个数据,使用与下面类似的代码(如果(c.getCount>0)块,则替换
if):
在OnlistItem中添加这些行单击,然后在查询中使用此号码。您遇到的问题是什么?我希望当用户单击某个列表项时,它会获取显示在特定列表项上的电话号码,然后对该号码执行数据库查询,获取消息并将该数据传递给另一个类用于在不同的布局中显示它…可以添加getview()吗或者listview项到底包含什么,我是指数据,但问题是…当我得到位置变量时,它返回一个int…当我需要列表上显示的电话号码时…然后根据该电话号码执行数据库查询…希望你能得到我..listview显示的是号码而不是textview….否t正在单击的文本视图是正在单击的listviewitem,在该listviewclick的基础上,我想用数据库中的数据填充一个文本视图…您添加了上面的两行吗?我得到的listitem内容如下….字符串s=(字符串)((光标)l.getItemAtPosition(位置)).getString();
newDB=new DBAdapter(this);
try {
newDB.open();
Cursor c = newDB.executeQuery("select distinct nm,rowid _id from messages");
if (c != null ) {
if (c.moveToFirst())
Log.v(tag, "Stage 1");{
do {
String firstName = c.getString(c.getColumnIndex("nm"));
Log.v(tag, firstName);
// String date = c.getString(c.getColumnIndex("dt"));
results.add(firstName);
}while (c.moveToNext());
Log.v(tag, "Stage 1");
}
}
ArrayList<String> arrayList ;
Cursor c=null;
if(c.moveToFirst()){
int x= c.getCount();
int y =c.getColumnIndex("msg");
arrayList = new ArrayList<String>(x);
for(int a=0;a<x;a++){
c.moveToPosition(a);
arrayList.add(c.getString(y));
}
Intent intent = new Intent(this,NewInterface.class);
intent.putStringArrayListExtra("msgs",arrayList );
startActivity(intent);
}
TextView tv1=(TextView)v.findViewbyId(R.id.number)//Replace this with id of tv displaying numbers
String number = tv1.getText().toString();