如何在另一个活动中显示单击的列表视图项

时间:2016-08-19 17:09:37

标签: java android firebase firebase-realtime-database onitemclicklistener

我对setOnItemClickListener方法提出了挑战,要在另一个活动中显示相应的listItem。我使用Firebase执行此操作。我有3个变量(字符串):姓名,电话,电子邮件

我在 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);
                }
            }
        });
Constants.class中的

KEY_LIST_ID

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吗?

1 个答案:

答案 0 :(得分:1)

您将listId设为long。所以使用:

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

修改 要获取用户名,电话和电子邮件:

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);
                    }
            }
        });
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);