Ad
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?
Ad
Answer
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 );
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad