gryzek gryzek - 3 months ago 25
Java Question

How to display clicked listview item in another activity

I have a challenge with setOnItemClickListener method to display appropriate listItem in another activity. I do this with a Firebase. I have 3 variables(String): Name,Phone,Email

Snippet of my code in 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 in Constants.class:

public static final String KEY_LIST_ID = "LIST_ID";


This KEY_LIST_ID is needed to save each 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: Activity which should display appropriate 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);



}


Now, it shows nothing in MainDetails.

Could somebody help me how to display every each ListItem in this Activity?

Answer

You put listId as long. So use:

long listId = intent.getLongExtra(Constants.KEY_LIST_ID, -1L);

EDIT: To get user name, phone and email:

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_NAME, selectedList.getName());
                        intent.putExtra(Constants.KEY_LIST_PHONE, selectedList.getPhone());
                        intent.putExtra(Constants.KEY_LIST_EMAIL, selectedList.getEmail());
                        startActivity(intent);
                    }
            }
        });

in MainDetails

        Intent intent = this.getIntent();
        name= intent.getStringExtra(Constants.KEY_LIST_NAME);
        phone= intent.getStringExtra(Constants.KEY_LIST_PHONE);
        email= intent.getStringExtra(Constants.KEY_LIST_EMAIL);