I have been trying to get a listview inside of SherlockListFragment for about a week (total of like 12 hours) and I can't figure it out. I'm out of options here. So, I have three tabs, which all need to have listviews, with images. I managed to get the listviews working, but I just cannot seem to find a way to add images. I am a HUGE beginner at this as well.
Here is one of the three tab classes that extend SherlockListFragment:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] commands = this.getResources().getStringArray(R.array.currentmobs_selections);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
R.layout.main_list_item, commands);
setListAdapter(adapter);
}
Now I did make a custom array for the listview in attempt to get it working, but I didn't even know how to implement the custom array adapter into the code. If anyone knows any way to get a image/text listview inside of a ListFragment I'd be soooo happy. Thanks!!
You need to use a Custom adapter instead of an array adapter.
Good tutorial here: http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
Use if listview holds only one string
ListAdapter listAdapter =
new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,command);
setListAdapter(listAdapter);
Related
I am trying to understand what other alternative I have if I can't write this code:
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,bluetoothDevices);
recyclerView.setAdapter(arrayAdapter);
I have started to learn Java and I am taking a freshman class that requires us to write a mobile app, but there is no instruction on how to do this. This class is not a programming class, it's a project based course so everyone has to learn everything on their own. I have literally been introduced to android a week ago.
My other question is how does android store text files?
ArrayAdapter isn't meant to be used with RecyclerView. If you want to keep your ArrayAdapter, you can change your RecyclerView to a ListView:
// Change to a ListView
// (Remember to change it in your layout as well)
ListView myListView = findViewById(R.id.my_list_view);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, myStringItems);
myListView.setAdapter(adapter);
This will work fine for simple data but, if you need to display more complex data or if you require more customization or performance, using RecyclerView is the recommended approach.
I have just started learning android using Big Nerd Ranch Guide. In a project, we were to implement a listView and get the data from an array using an ArrayAdapter, as the crime objects were stored in some list.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTitle(R.string.crimes_title);
mCrimes = CrimeLab.get(getActivity()).getCrimes();
ArrayAdapter<Crime> adapter =
new ArrayAdapter<Crime>(getActivity(),
android.R.layout.simple_list_item_1,
mCrimes);
setListAdapter(adapter);
}
After that, we were to create custom list to display the title as well as the checkbox and date so we created a custom layout. Now, we used a different adapter for that purpose.
private class CrimeAdapter extends ArrayAdapter<Crime> {
public CrimeAdapter(ArrayList<Crime> crimes) {
super(getActivity(), 0, crimes);
}
My question is why cant we use the same adapter that we used earlier?I mean we can just give it new values or create a new object.I searched the internet and found that we can create multiple adapters with some restrictions. why we extend it to ArrayAdapter? cant we just do it like we did previously by creating the adapter?
Please explain in detail, as I have just started.
Thanks.
Let us talk about the list view adapter implementation in android first of all in your first example you used the default implementation of listview adapter in android which provide you with list view of text
but most of the time this is not enough for your user interface you want to add an image a checkbox like in your second example so android OS gives you the ability to make your own custom cell (each row in list) that's why you would find yourself in a need to extend array adapter
in your first example you used
android.R.layout.simple_list_item_1
which is a layout containing only textview
and you provide a data source mCrimes
so when you extend array adapter you do the same you provide your own layout which represent the cell and a data source which will populate your list
hope that clarify it for you and welcome aboard :)
I am using customized listview with listadaper in listactivity.I want to add the option for single & multiple items selecting in listview.I searched about this but all are using check box with adapter class.I dont have any adapter class in my app.I am using listactivity with no adapter class.So how can i get the view of checkbox for single /multiple select?
My adapter:
ListAdapter adapter = new SimpleAdapter(getApplicationContext(), entriesList,R.layout.new_todo_singleentry, new String[]
{"``id","time","date","message","status"},new int[] { R.id.todo_id,R.id.todo_time,R.id.todo_date,R.id.todo_entry});
I'm not aware of an option to do this without a custom adapter class.
I would advice you to read this and use the example in chapter 14 to create the functionality that you are looking for.
When my application loads, I need some(not necessarily all) of the items in my list view to change their backgrounds without clicking on the ones I need. I know the method to change background, but I don't know how to extract the views from the ListView to do that.
I looked at this question and the answer did not work:
https://stackoverflow.com/questions/10650692/how-to-change-background-color-list-view-item-without-click-on-any-of-them
Here is some of my code:
mApprovalList = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.approvals_array)));
mAdapter = new ArrayAdapter<String>(this,
R.layout.approvals_list,
immApprovalList);
setListAdapter(mAdapter);
ListView lv = getListView();
Log.v(String.valueOf(lv.getChildCount()), "count");
Also, that "log" returns 0.
Thanks in advance!
You should write a custom adapter and have it set the backgrounds to whatever color you'd like. See https://stackoverflow.com/a/5561813/458968
Then use
MyCustomAdapter customAdapter = new MyCustomAdapter(this, mApprovalList);
setListAdapter(customAdapter);
in your onCreate method.
I've searched far and wide for this answer and can't seem to find it.
I'm looking to populate a very simple 3 line listview, no more then 5-6 words per line at the most inside of my android app.
I'm currently using a base adapter and a string array to enable the actual text to show up on the screen.
I want to have the ability to update the information inside of my listview remotely using
some sort of means whether that's xml, SQLite, plain text, etc and then have that hosted file populate my listview.
Can anyone here help me to figure out how to do this? I'm still pretty new to android development so please go easy on me. Hopefully this question wont be too hard answer and also not too difficult to enable for a newbie like myself.
If the most you're going to ever have in there is just 3 lines of text, I think a SQLite DB may be a bit much for your situation. I'd look into using a Typed Array.
Here's a link to the Android Dev Guide on this subject:
http://developer.android.com/guide/topics/resources/more-resources.html#TypedArray
Here's a code sample:
public class YourListActivity extends ListActivity {
String[] mTestArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an ArrayAdapter to contain your items
ArrayAdapter<String> adapter;
mTestArray = getResources().getStringArray(R.array.yourArray);
// Assign your array to an adapter with your layout file
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mTestArray);
// Assign the adapter to this ListActivity
setListAdapter(adapter);
}
}
EDIT
Just realized that your data will be on a remote server, so this approach may not work for you, but it can still give you an idea of how to take your data once received from your remote server and place it into a ListView.