What does these three lines of codes mean? - java

I'm struggling to understand certain lines of code. What do these three lines mean when using an Array Adapter?
WordAdapter itemsAdapter = new WordAdapter(this,words);
ListView listView = findViewById(R.id.numlist);
listView.setAdapter(itemsAdapter);

WordAdapter itemsAdapter = new WordAdapter(this,words);
A custom adapter class for adding your listArray(contains data which will be added to List view), Bind view holder class and rest of the things which will provide you customization for displaying data in list view.
ListView listView = findViewById(R.id.numlist);
ListView is a predefined component class in android sdk, using this class you will create a object reference to the xml declared component ie.. R.id.numlist into your java class file.
listView.setAdapter(itemsAdapter);
This line attaches the adapter to the listview so that the data in the listarray will be displayed on listview

Related

PagingDataAdapter item ui is not updating while submitting new data in paging 3

I have an RV inside an alertdialog. Adapter for the RV extends PagingDataAdapter with DiffUtil.ItemCallback.
The problem is, the list is updated and submitted to the adapter with the new data and but the list item view is not updating based on new data provided as shown below. I'm using data/view binding for updating the list item view.
The RV sometimes updates the item view when being scrolled.

How to set multiple id with ArrayAdapter?

Here is my declared ArrayAdapter :
itemsAdapter = new ArrayAdapter<String>(this,
R.layout.activity_task,
R.id.taskTitle,
items1);
I wanted to do that :
itemsAdapter = new ArrayAdapter<String>(this,
R.layout.activity_task,
R.id.taskTitle,
items1,
R.id.taskNews,
items2);
Because, my app is like :
Task 1
Task 2
And, I'm trying to do :
Task 1
Task's description 1
Task 2
Task's description 2
How it's possible to add multiple id ?
If you are using list view to show the text items, then you can only use one layout and one text view using standard ArrayAdapter.
To use two TextView's try making a CustomListAdapter
Use this link for reference on CustomListAdapter

Android: filter ListView without the adapter

I have a ListView that gets filled by SimpleAdapter, this adapter is local variable inside the procedure that fills the ListView.
Now, I want to filter the ListView, but I have no access to the adapter in the EditText events
is there a way to do that?
I tried MyListView.getAdapter().getFilter().Filter("some text");
but that gives error as getFilter does not exist for MyListView.getAdapter()
How can I fix this?
Declare adapter as global, setting adapter as global will let you filter it:
YourActivity.this.adapter.getFilter().filter("some text");
Did you try this?, it should works:
Adapter adapter= MyListView.getAdapter();
adapter.getFilter().filter("some text");
Too you can post your code to check what are you doing wrong, if you're using a custom adapter you have to implement the getFilter method
This post might answer your question. You have to create getFilter() in your adapter.

Populating listview from arraylist

I want to populate a list view with an array list. The array list is fetched from a soap web service response. I have used it as following:
mylistview = (ListView)findViewById(R.id.list);
listAdapter = new ArrayAdapter<Plant>(MainActivity.this,
android.R.layout.simple_list_item_1, array_books);
mylistview.setAdapter(listAdapter);
It display following in list view:
Book{id=1; name= C++; imagepath=https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GW3ixVIGHLrrXb2yN_6i;}
Book{id=2; name= Java; imagepath=https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GW3ixVyN_6i;}
Book{id=3; name=Android; imagepath=https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GW3HLrrXb2yN_6i;}
I want to display the image by retrieving it from its path in Image View and display only name of book like:
image ................ Java
When I change the layout in
listAdapter = new ArrayAdapter<Book>(MainActivity.this,
android.R.layout.row, array_books);
it gives error that row cannot resolved as a resource. But row.xml is present in the layout folder. How can I correct this error and display the items I want in the list view?
Try this
listAdapter = new ArrayAdapter<Plant>(MainActivity.this,
R.layout.row, array_books);// Change android.R.layout.row to R.layout.row
Your row is present in your layout so need to find using R.layout.row
Use R.layout.row instead of android.R.layout.row
As #Giru pointed out, if your xml file row.xml is in your layout folder, you can't reference it by android.R
The android.R object has references to the resources bundled with Android.
Your resources are simply under your.package.R so in your file just remove the "android." and have
listAdapter = new ArrayAdapter<Plant>(MainActivity.this,
R.layout.row, array_books);
and you should be good to go.

Android:How can i make an AutoCompleteTextView to pass the text from a website and show me the results

How can i make an AutoCompletTextView to pass the text from a website and show me the results in ListView
After parsing the webservice data you will get results into an array.
Let's say you are getting result into array named resultData then use below code.
arrayAdapter = new ArrayAdapter<String>(viewAutoComplete,
android.R.layout.simple_list_item_1, resultData);
autoCompleteText.setAdapter(arrayAdapter);

Categories