2
我有一個setOnItemClickListener方法的挑戰,在另一個活動中顯示適當的listItem。我使用Firebase執行此操作。我有3個變量(字符串):姓名,電話,電子郵件如何在另一個活動中顯示點擊的listview項目
我的代碼片段在MainActivity:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ChatMessage selectedList = adapter.getItem(position);
if(selectedList != null) {
Intent intent = new Intent(MainActivity.this, MainDetails.class);
long listId = adapter.getItemId(position);
intent.putExtra(Constants.KEY_LIST_ID, listId);
startActivity(intent);
}
}
});
KEY_LIST_ID在Constants.class:
public static final String KEY_LIST_ID = "LIST_ID";
這KEY_LIST_ID是否需要保存每個itemId?
ChatAdapter:
public class ChatAdapter extends ArrayAdapter<ChatMessage> {
public ChatAdapter(Context context, int resource) {
super(context, resource);
}
@Override
public ChatMessage getItem(int position) {
return super.getItem(getCount() - 1 - position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView==null){
LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
convertView = inflater.inflate(R.layout.list_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.username = (TextView) convertView.findViewById(R.id.list_item_username);
viewHolder.message = (TextView) convertView.findViewById(R.id.list_item_message);
viewHolder.email = (TextView) convertView.findViewById(R.id.list_item_email);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
ChatMessage item = getItem(position);
if(item != null) {
viewHolder.username.setText(item.getName());
viewHolder.message.setText(item.getPhone());
viewHolder.email.setText(item.getEmail());
}
return convertView;
}
private static class ViewHolder{
TextView username;
TextView message;
TextView email;
}
}
MainDetails:活動應該顯示相應的listItem:
private TextView detailsName, detailsPhone, detailsEmail;
private ChatMessage chatMessage;
private ListView listView;
private ChatAdapter chatAdapter;
private String listId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_details);
detailsName = (TextView) findViewById(R.id.detsName);
detailsPhone = (TextView) findViewById(R.id.detsText);
detailsEmail = (TextView) findViewById(R.id.detsEmail);
Intent intent = this.getIntent();
listId = intent.getStringExtra(Constants.KEY_LIST_ID);
}
現在,它顯示沒有在MainDetails。
有人可以幫助我如何顯示每個ListItem在這個活動?
它不工作。這意味着什麼都不顯示 – gryzek
你的問題不清楚。你得到空listId?或null selectedList?你是否嘗試打印listId,如果它是空的? – uguboz
我改變了listId,正如你所說的,在MainDetails中變長。但它沒有幫助解決問題。在這個MainDetails活動中,我想從listView中顯示特定的姓名,電子郵件和電話。 – gryzek