我正在尝试将我的用户名值转移到新活动。但是,当我执行此操作时,它显示我添加的第一个数组项,例如,即使我单击列表中的其他项,下面的代码也会在“对话”活动中显示“Bob Jones”用户名。
公共类MainActivity扩展了Activity {
TextView user;
TextView threadId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//messaging_string = (TextView) findViewById(R.id.messaging_string);
//this.GetActivityFeedData();
final ListView listView = (ListView) this.findViewById(R.id.messagingListView);
final ActivityAdapter itemAdapter = new ActivityAdapter(getApplicationContext(), this.GetActivityFeedData());
listView.setAdapter(itemAdapter);
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
user = (TextView) findViewById(R.id.messaging_username);
threadId = (TextView) findViewById(R.id.messaging_threadId);
listView.getAdapter().getItem(position);
Intent intent = new Intent(MainActivity.this, ConversationView.class);
intent.putExtra("username", user.getText().toString());
intent.putExtra("pos", position);
intent.putExtra("threadId", threadId.getText().toString());
startActivity(intent);
}
});
}
public ArrayList<MessageItem> GetActivityFeedData() {
ArrayList<MessageItem> items = new ArrayList<MessageItem>();
items.add(new MessageItem(6, "Lorna Smith", "image", DateTime.now(), "Hello what are you doing"));
return items;
}
class ActivityFeedTask extends AsyncTask<Integer, Void, Void> {
ArrayList<MessageItem> recentTracks;
//remove to not loading more data
}
public class ActivityAdapter extends ArrayAdapter<MessageItem> {
private final Context context;
private final ArrayList<MessageItem> items;
private int currentPage = 0;
public ActivityAdapter(Context context, ArrayList<MessageItem> recentTrackArrayList) {
super(context, 0, recentTrackArrayList);
this.context = context;
this.items = recentTrackArrayList;
}
}
答案 0 :(得分:0)
您必须获取所点击行的内容。您可以使用parent.getItemAtPosition(position);
,
MessageItem itemAtPostion = (MessageItem) parent.getItemAtPosition(position);
Intent intent = new Intent(MainActivity.this, ConversationView.class);
intent.putExtra("username", itemAtPostion.Username);
intent.putExtra("pos", position);
intent.putExtra("threadId", itemAtPostion.ThreadId);