如何为ListView中的项目设置onClickListener(通过从Firebase检索数据来动态生成Listview的项目)

时间:2019-05-28 07:52:30

标签: java android listview onclicklistener

您好Iam尝试制作chatApp。Iam通过诸如用户身份验证,注册之类的初始步骤成功完成。成功登录后,用户将获得如下图所示的屏幕:

User Interface for a user 现在,此处的用户列表是通过以下代码生成的:

ListView listView;
View view;
ArrayList<String> usernames = new ArrayList<>();
String name;
FirebaseDatabase db = FirebaseDatabase.getInstance();
DatabaseReference root = db.getReference();
DatabaseReference userRef = root.child("users");

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
   Bundle savedInstanceState) {
   view = inflater.inflate(R.layout.fragment_users,container,false);
   listView =(ListView) view.findViewById(R.id.listView);
   final ArrayAdapter <String> arrayAdapter = new ArrayAdapter<String> 
     (getContext(),android.R.layout.simple_list_item_1,usernames);
   listView.setAdapter(arrayAdapter);

   userRef.addChildEventListener(new ChildEventListener() {
     @Override
     public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
       String value = dataSnapshot.getValue().toString();
       try
       {
         JSONObject object = new JSONObject(value);
         name = object.getString("username");
       }
       catch(JSONException e)
       {
         e.printStackTrace();
       }

       usernames.add(name);
       arrayAdapter.notifyDataSetChanged();
     }

     @Override
     public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {}

     @Override
     public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {}

     @Override
     public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {}

     @Override
     public void onCancelled(@NonNull DatabaseError databaseError) {}
  });

  return view;
}

现在,每当用户单击特定名称时,我都必须将其带到messageActivity(更确切地说是另一项活动)。请告诉我如何添加onClickListeners或任何其他可能的方法来做到这一点?

3 个答案:

答案 0 :(得分:2)

尝试一下

在您的片段或活动中声明这一点

private OnItemClickListener listPairedClickItem = new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView < ? > arg0, View arg1, int arg2, long arg3) {
        String personName = usernames.get(position);
        Toast.makeText( context, "listview clicked ", Toast.LENGTH_LONG ).show();
        Intent intent = new Intent(currenactivityname.this, activitytolaunchname.class);
        intent.putExtra("username",personName ); //get this name in next activity and show in title  bar
        startActivity(intent);
    }
};

用法

listView.setOnItemClickListener( listPairedClickItem );

答案 1 :(得分:1)

之后

listView.setAdapter(arrayAdapter); 

像这样创建您的侦听器:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        // Do what you want here
   }
});

答案 2 :(得分:1)

使用onItemClickListener:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
      String selectedName = usernames.get(position);
      // Do what you want with username here

   }
});