Using custom layout rows producing unexpected errors - java

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()

Related

Execute code from Spinner's onItemSelected that's in custom listView

I have a custom listView with 3 spinners. I want to execute a code in onItemSelcted but I cannot do this in adapter because some part of the code that I need to execute won't work in a adapter.
I had this code pinned to a button before but sometimes there are over 20-30 elements in the list and I need do this "automaticly" right after user picks a value from spinner to save some time.
For a button I had this code:
((ListView) parent).performItemClick(v, position, 0);
in setOnClickListener that I created inside adapter. After using this code in adapter I could exetue any code outside adapter by writing something like this:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
long viewId = view.getId();
if(viewId == R.id.button){
myCode(); }
I found no way to do something similar because there was no "link" between inside adapter onItemSelcted and outside adapter onItemSelcted. Of course writing same line of code inside adapter's onItemSelcted won't work because spinner "doesn't click".
Any idea how to solve this?
In a fewer words: I need to execute onItemSelected for a spinner that's inside custom listView element but need to do this outside the adapter in onCreate method.
You can create an CallbackInterface and create an instance of it "outside" and pass it down to the "inside", where you call the callback function with the needed arguments.
public interface Callback {
void onAction (int a, int b, String c);
}

error while selecting a list item when list view is large in android?

Currently I am making an app in Android which is a todo list, it has a edit text, button and a listview, when button is clicked the text is added to listview, everything works fine but when i add too many items to list view and try to select a list item my app stops working and I get a error saying
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
Does anyone know a fix?
this is my view data method whcih displays items in listview
public void viewdata(String tablename)
{
db = new mydbhandler(this);
lvitemslist = findViewById(R.id.lvitemlist);
ArrayList mylist = new ArrayList();
Cursor c = db.getdata(tablename);
while(c.moveToNext())
{
mylist.add(c.getString(1));
}
ListAdapter mylistadapter = new ArrayAdapter(this,R.layout.custom_item_row,R.id.ctv,mylist);
lvitemslist.setAdapter(mylistadapter);
}
and this is the onitemclicklistener
lvitemslist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
View v = lvitemslist.getChildAt(i);
CheckedTextView ctv = v.findViewById(R.id.ctv);
ctv.setCheckMarkDrawable(android.R.drawable.checkbox_on_background);
db.deletedata(tablename,ctv.getText().toString());
viewdata(tablename);
Take a look at this line from your OnItemClickListener:
View v = lvitemslist.getChildAt(i);
I guess you want to assign the ListView row which was clicked to the variable v. But the ViewGroup method getChildAt(int) (ListView is a kind of ViewGroup) will give you
the view at the specified position or null if the position does not exist within the group
(quoted from the ViewGroup documentation)
Like you said, this works for the first few items but for higher positions the app crashes. This is because the ListView has a fixed number of child Views: about as many as fit on the screen simultaneously plus some more to enable smooth scrolling. For every clicked row beyond that fixed number, lvitemslist.getChildAt(i) will return null.
What can you do to fix your code?
Luckily you do not have to do much work to get the clicked View, it is handed to you as one of the parameters of onItemClick(): the second parameter is the clicked ListView row.
So you just need to write
lvitemslist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long rowId) {
CheckedTextView ctv = view.findViewById(R.id.ctv);
ctv.setCheckMarkDrawable(android.R.drawable.checkbox_on_background);
db.deletedata(tablename,ctv.getText().toString());
viewdata(tablename);
}
}
This will help you to avoid the NullPointerException.
Another thing: you change the appearance of a ListView row from outside of the Adapter by calling
ctv.setCheckMarkDrawable(android.R.drawable.checkbox_on_background);
This is dangerous (there are lots of questions on this site where people had weird behaviour when scrolling as a consequence). It may not be a problem in your case because you refresh the whole ListView soon afterwards. But generally one should provide the Adapter with all the data necessary to decide what each row should look like. Then one can change the data list and after that of course call notifyDatasetChanged() on the Adapter.

How to get information of an Item in RecyclerView CardView Android

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.

How to get the value of a TextView that is part of a ListView row in all checked items

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

Android: onItemClick listener of ListView - how to identify the items?

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

Categories