简体   繁体   中英

How to set onClickListener for items in a ListView (items of listview are generated dynamically by retreiving data from firebase)

Hi Iam trying to make a chatApp.Iam successful with initial steps like user authentication,registration.After a successfull login user gets a screen as shown in this image:

用户的用户界面 Now the list of users here are generated by following code:

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

Now whenever user clicks on particular name I've to take him to a messageActivity(more precisely another activity). Please tell me how I can add onClickListeners or any other possible method to do this?

Try this

Declare this in your fragment or activity

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

Usage

listView.setOnItemClickListener( listPairedClickItem );

After

listView.setAdapter(arrayAdapter); 

create your listener like this:

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

Use 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

   }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM