在列表中的活动之间传递值
我正在使用 SimpleCursoradapter 来填充我在活动中拥有的列表。
现在我需要将一个字符串值从这个活动传递到另一个活动,并且该值将从本地数据库中获取。
我通过在列表视图中创建一个新的文本视图并使用 SimpleCursorAdapter 映射字符串值来执行相同的操作。
这是最好的做法吗?
public class InboxAdapter extends SimpleCursorAdapter {
public InboxAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
Button msgReply = (Button) view.findViewById(R.id.msgReply);
final TextView msgId = (TextView) view.findViewById(R.id.msgId);
msgReply.setOnClickListener(new View.OnClickListener() {
private Intent intent;
@Override
public void onClick(View v) {
intent = new Intent(getApplicationContext(), ComposeMessage.class);
Bundle b = new Bundle();
b.putCharSequence("id", msgId.getText());
intent.putExtras(b);
startActivity(intent);
}
});
}
}
I am using SimpleCursoradapter to populate a List that i'm having in an activity.
Now i need to pass a String value from this activity to another and the value is to be fetched from a local database.
I did the same by creating a new textview in the listview and mapping the string value using SimpleCursorAdapter.
Is this the best pratice to do so??
public class InboxAdapter extends SimpleCursorAdapter {
public InboxAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
Button msgReply = (Button) view.findViewById(R.id.msgReply);
final TextView msgId = (TextView) view.findViewById(R.id.msgId);
msgReply.setOnClickListener(new View.OnClickListener() {
private Intent intent;
@Override
public void onClick(View v) {
intent = new Intent(getApplicationContext(), ComposeMessage.class);
Bundle b = new Bundle();
b.putCharSequence("id", msgId.getText());
intent.putExtras(b);
startActivity(intent);
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有点仓促,但是 this 是我如何用正常的方式做到这一点的一个例子
SimpleCursorAdapter
。内容从数据库中获取并显示在
ListActivity
中。这也适用于 ListView。当用户单击列表中的某个项目时, onClick - 方法从数据库作为参数。在我的示例中,它然后打开一个新的活动,该活动查询数据库中的特定信息。
I'm a little on short notice, but this is an example how I managed to do this with a normal
SimpleCursorAdapter
.The Content is fetched from the Database and shown in the
ListActivity
. This works forListView
s, too.When the user clicks on an item in the list, the onClick-method gets the items ID from the Database as a parameter. In my example it then opens a new Activity which query's the particular informations from the Database.
我是这样做的。
i did it this way.