How to set multiple id with ArrayAdapter? - java

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

Related

What does these three lines of codes mean?

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

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.

Spinner not a view that can be bounds(sp) by this SimpleCursorAdapter

I'm trying to adapt my query to my spinner object with some trouble, I get the error listed as the title. Here is the code portion where it crashes:
Spinner classDropDown = (Spinner) this.findViewById(R.id.classDropDown);
int[] to = new int[] { R.id.classDropDown };
String[] classFields = new String[] { "className" };
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.main, cursor, classFields, to);
cursorAdapter.setDropDownViewResource(R.id.classDropDown);
classDropDown.setAdapter(cursorAdapter);
I had a problem where the cursor wasn't being filled but fixed that now. Can someone give me help on debugging this issue?
Edit: I think my problem is the "to" field. What should this be?
Edit 2: Also, here is the XML for the spinner object:
<Spinner
android:id="#+id/classDropDown"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Edit 3: I've fixed the above to reflect fixing the code. This fixes this particular problem. I'm not getting the error, but I also have nothing displayed in the spinner.
To is a list of resource ids that you want to put your data into, such as R.id.textview1, and they meed to be contained in the layout you specify in the adapter. The number of elements should also match the number of elements in your from array ( you called it classfields).
So, you have two pieces of data and only specified one target resource id. Either remove one of the fields in your classfields array or add a widget to your layout and call.it in your to array and it should work.

android java sdk, loading bookmarks into spinner?

The spinner comes up with but with nothing in it
what am I doning wrong?
cbookmarks = Browser.getAllBookmarks(getContentResolver());
SimpleCursorAdapter ABM = new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item, cbookmarks,new String[] {android.provider.Browser.BookmarkColumns.URL},new int[]{R.id.Spinner});
ABM.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
SpinnerBM.setAdapter(ABM);
Thanks.
Is R.id.Spinner a TextView? Perhaps you can post the XML you have defined for the layout of the entries?
UPDATE
According to the documentation the array of id's that you pass into the SimpleCursorAdapter constructor need to be TextView. Most examples I have seen use the built-in id of android.R.id.text1, but I think you can define your own custom layouts too.

Categories