Populating listview from arraylist - java

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.

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

Add items to ListView Android

I have an app that gets all the .mp3 files off of a storage device and I want to add them to a ListView when the app is created. I am lost on adding items to the list. I have googled it and I cannot a good defintion of what the dev is going and why they are adding what they are adding. I would like someone to tell me how to add items to a ListView and also, if they can, explain what they are doing as they are doing it so I understand and learn from it. I am a new Android Dev and looking to learn as much as I can and not just fix my code.
So, my current code is...
XML
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ListView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
and I do not know what to do on my main activity. I so far have...
File storageDir = new File("/mnt/extSdCard/");
ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
walkdir(storageDir);
}
public void walkdir(File directory) {
TextView songList=(TextView)findViewById(R.id.textView);
String fileType = ".mp3";
File[] listFile = directory.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
walkdir(listFile[i]);
} else {
if (listFile[i].getName().endsWith(fileType)){
//I need to add the items to the ListView right here.
//listFile[i].toString() is the code to get the text, aka what i want to add
}
}
}
} }
To start with, in your activity, you should extend ListActivity instead of Activity. This will give you access to a method called setListAdapter (there are other ways, but this is probably easiest).
The setListAdapter method takes an adapter as its parameter, so we now need to create an adapter that we can pass it.
To do so, write the following:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listFile);
I'll break that down a little bit:
The first part (this) is where we pass it the context.
The second part is the layout file that represents each individual list item. To save us having to create one, we are using one that android has built in.
Finally, we need to pass it the words you want it to display (in your case, a string array, although it could also be an ArrayList of strings.
Once you've done that, we just need to pass the adapter to our setListAdapter method, like so
setListAdapter(adapter);
But before you run your code, we need to add one little line to the XML file. When you extend ListActivity, Android will be looking for a list whose id is android.R.id.list, so we need to set the id like so:
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
initialize your list adapter after fetching mp3 file names.
adapter = new ArrayAdapter(yourContext,android.R.layout.simple_list_item_1,yourFileNameList);
Looking at your code you are trying to convert the mp3 file to string and showing in listview. You can just show the filename.
You need to set adapter for the custom listview you have built.
Go to youtube and search for videos by Slidenerd. He has all the basic tutorials for android. The listview tutorials start from #77.

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

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