I have a RecyclerView which is constructed by inflating CardView Layout. This is acheived by onCreateViewHolder method. I am passing a ArrayList of data to the RecyclerView.
My requirement is, on clicking the list item, I have to load a new page which shows the details of the item clicked. For this, I need to get the id of the item clicked which is not showed in the view. In the view I have only Name and email of the item.
Let's say, the ArrayList I am passing is of type Person and Person has member variables id,name,email,education,location.
In the RecyclerView onBindViewHolderfunction, I am setting only name and email.
I need to get the id of the item, then only I can do a db command and get details of the particular person.
RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.recyclerList);
rv.setAdapter(new NGOViewHolder(ArrayList()));
rv.addOnItemTouchListener( // and the click is handled
new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
Intent intent = new Intent(getActivity(),NGODetails.class);
intent.putExtra("ID", (IDOfTheItemClicked));
}));
I requirement is IDOfTheItemClicked, that is passing in the above
Intent(getActivity(),NGODetails.class);
intent.putExtra("ID", (IDOfTheItemClicked));
should be the id of the item that is in the ArrayList that I have passed initially.
How can I get to send the id of the Person object which is currently showing in the view, to the next screen, on clicking the list item.
set position for each item using setTag inside onCreateViewHolder method after that when you'll click then inside onclick you get a view reference from there, get that tag and convert to int( this will give you position of view)
onCreateViewHolder(............){
View view;
view.setTag(id, position);
view.setOnTouch or Onclick(View v){
String position = v.getTag(),toString();
// get your id using this position
}
}
If you don't change the order of the items in your adapter, simply call yourArrayList.get(position).yourgetIDOfTheItemClickedMethod in the onClick.
Related
I am new to android and I am having a problem reloading (rebuilding) as recyclerview after the activity is active. The activity with the recyclerview is a second activity started as an intent on the main activity. I can load it up, and show a list of items. When one of the items is clicked a third activity is launched. The problem is sometimes there are too many items on the list. I want to add a way of selecting an item more easily. I added a spinner on the same activity as the recyclerview. When an item is selected on the spinner, I want to reload a shorter version of the list, based on the spinner selection. However, I can't find a way to achieve that. Any help or suggestions pointing me in the right direction will be greatly appreciated.
as #bink said without code it's hard to help, but the idea in general is that on your spinner.setOnItemSelectedListener() you populate your recyclerView, so everytime you change the selection you get a new list depending on the choosed value.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
ArrayList<> arrayList = new ArrayList<>();
// add your items based on item spinner.itemSelected
// for(---)
adapter = new MyAdapter(context, arrayList );
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
});
You can also add a filter to your recycler view (https://medium.com/android-news/filterable-recyclerview-in-android-the-how-to-a9ade9cd26)
If you dont want to recreate all the recycler view and the list itselft, use adapter.notifyDataSetChanged();
As illustrated in the photo, i have a list view that is made up of a custom layout which has two TextView. One TextView is for storing numbers which has a visibility of gone, the other is for storing the name, which is visible.
all i want is to be able to get a string of all numbers that are selected when the send button is clicked.
A good suggestion here is to use a recyclerview instead of list view.
To achieve want you want with a list view you just set an item click listener to your list view in your activity. The item click listener will pass the View which is the current cell in the list view. Then you can find textview by id to locate. The Textview ID is set in the layout xml.
Example
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View cell,int pos, long arg1)
{
TextView textView = (TextView)cell.findViewByID(R.id.myTextView);
String texViewContents = textView.getText().toString();
}
});
I am currently learning android and working on a few projects...in this one I have a list view and when I click it I pass the name of the row I click and a value from a database to my NEXT activity and that works 100%.
This is part of my main Activity on create code. where I populate the listview from my database here and other stuff.
lvUsers = (ListView)findViewById(id.lvUsers);
List<String> listUsers = dbHeplper.getAllUsers();
if(listUsers != null){
adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1, android.R.id.text1,listUsers);
// adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.test, id.movie_title,listUsers);
lvUsers.setAdapter(adapter);
lvUsers.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String product= ((TextView)view).getText().toString();
// String product= String.valueOf((TextView)findViewById(id.movie_title));
// Toast.makeText(getBaseContext(),product,Toast.LENGTH_LONG).show();
Intent x = new Intent(getApplicationContext(), SingleListItem.class);
// sending data to new activity
x.putExtra("product", product);
startActivity(x);
}
});
This code works fine, but I have attempted to style my row myself if you look at this line. (which is commented in the main view)
adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.test, id.movie_title,listUsers);.
Adding this threw the error of relativelayout cannot be cast to android widget textview on the line
String product= ((TextView)view).getText().toString();
So I took that to mean it does not know which textview I am referring to anymore. so I edited that line to
String product= String.valueOf((TextView)findViewById(id.movie_title));
because movie_title is the ID of the textview in the test layout.
The then disappeared but now when I click the row instead of getting that data I expected like row name and data from database. the textview in my second activity is displaying
"android.widget.TextView{176f155.v.Ed (more random numbers) app:id/movie_title}
(Some of the code from second activity
Intent i=getIntent();
String name = i.getStringExtra("product");
txtName.setText(name);
Like I said all these errors are occurring from me trying to implement my owned custom rows. So If anyone could point out where I went wrong while doing this I would be grateful .
Have you tried creating a custom adapter and making it extend ArrayAdapter ? You can do this easily and then inside your getView() of your custom adapter you can inflate your textview with your appropriate text . Check this : https://stackoverflow.com/a/8166802/1497188
Also String product= String.valueOf((TextView)findViewById(id.movie_title)); , this line is getting the ID of the view and not the text inside the view. The appropriate view to do it is that from inside your onItemClick() you would convert/cast your view into a textview and then do the getText().toString() on that view. Just Search online how to create custom adapters and onItemClick Listener for Adapter
EDIT:
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String data=(String)adapterView.getItemAtPosition(arg2);
This will atleast help you get the string from inside your onItemClick()
I have created a custom ArrayAdapter that fills a ListView. The data in the adapter is a list of POJOs which I get from a server. Each POJO has a unique id given by the server, I have no influence on that id. The id is only for internal use, means it isn't displayed to the user on the ListView.
My intention: if the user clicks an item in the list, I want to pass the id back to the server. The first step would be to create an onItemClickListener:
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void OnItemClick(AdapterView<?> parent, View view, int position, long id) {
// call the server in a new thread
// BUT: how do I get the POJO id from this position?
}
});
My problem is: I don't know how to obtain the id of the POJO. Since it is not displayed to the user, is is not in the TextView I get passed in the listener. The other parameters - position and id - do not seem to be what I need also. Description:
position: The position of the view in the adapter.
id: The row id of the item that was clicked.
I hope you got what I mean, otherwise tell me. Thank you!
the parent.getItemIdAtPosition(int position) will give you for pojo object at that index.
That would depend on how your adapter work. The id parameter will have a correspondence to one of the items the adapter is adapting. Based on that id, and how your adapter works, you can retrieve the original logical item that was represented by the view that was clicked.
#Override
public void OnItemClick(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getAdapter().getItem(position);
}
In the application I am writing, I have a main class which extends the ListActivity - it contains a list of elements. I want these elements to link to a different page (an xml one, or perhaps a View object). However, I realised one cannot use the method setContentView(int) on a ListActivity object.
What's to be done?
Thanks!
Looks like you are trying to launch a new activity.
You have to override the onListItemClick method of ListActivity.
Here is the code.
// ListView l points to list view whose item user clicked
// View v points to the item in the list view on which the user clicked
// int position is the position index of the item in the list
// long id is the id assigned to the item. This id is assigned using the ListAdapter, CursorAdapter etc.
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// I am using getApplicationContext() as it is more safe then just passing `this`
Intent i = new Intent(this.getApplicationContext(), ActivityToRun.class);
this.startActivity(i);
}
NOTE: You have to improve on this skeleton depending upon your needs.
Have you tried setting the XML page as a ListView and give it the ID #android:id/list?