I did listview tutorial. I can't see how to integrate the list making program into another program I have.
I want to fill an array with sensor input values in one method of my public class, and then display the array as a list after that.
Is it possible to call the list making code as a function, in response to some user activity within the 'sensor' method? How do I do this?
Excuse me if this is stupid, I am a beginner to Java.
Any advice appreciated.
AFAIK, the way you construct a listview is via List<? extends Map<String, ?>>.
if you do want to construct it this way, here's how to do it.
fill the data inside a List<Map<String,?>>, then use a SimpleAdapter (or other adapter) to concatenate the list to ListView. suppose you have a List<Map<String,?>> named mList,
ListView mListView = (ListView)findViewById( --listView id-- );
String[] mFrom = { -key1-, -key2- };
int[] mTo = {android.R.id.text1, android.R.id.text2 };
SimpleAdapter mAdapter = new SimpleAdapter(getApplicationContext(), mList, android.R.layout.simple_list_item_2, mFrom, mTo);
mListView.setAdapter(mAdapter);
listView id is the id of the listview on the xml, key1 and key2 respectively are the data you want to input inside the listview. however this one used android's default feature of list.
if you want to use your own listview template (i.e. you have more than 2 data inside),
define the xml of your custom listview (customlist.xml) then change the mTo variable to match your textviews
int[] mTo = {R.id.-listTextView1-,R.id.-listTextView2-,R.id.-listTextView3-};
and point to that xml on this line
SimpleAdapter mAdapter = new SimpleAdapter(getApplicationContext(), mList, R.layout.--customlistname--, mFrom, mTo);
Related
I am writing code for an mp3 player I am trying to create. I am only at the start of the project and want to be able to read and show all of the mp3 files that is on my sd card. I don't want to use the direct path method. Now this code I have written gathers all of the mp3 files for me but the only problem is that it does not view them on screen for me. The app shows a blank screen, but doesn't crash.
I got help and advice off of a tutor and I was told to use an ArrayAdapter to view the results but I can't find any help to show it. If anyone could please help that would be great.
Here is my code in the onCreate method;
ListView list;
Cursor cursor;
int columnIndex;
int count;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//list = (ListView) findViewById(R.id.list);
//A array is created that display the 3 fields
String[] displayMusic = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.TITLE};
//the cursor displays all of the audio in the sd card, just to limit to .mp3 files now
cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, displayMusic, null, null, null);
//this is the loop that gather all of the .mp3 files
int pos = 1;
ArrayList<String> listOfMp3s = new ArrayList<String>();
while (cursor.moveToNext())
{
if(cursor.getString(pos).endsWith("mp3"))
{
listOfMp3s.add(cursor.getString(pos));
}
}
String[] displayFields = new String[] {MediaStore.Audio.Media.DISPLAY_NAME};
int[] displayViews = new int[] {android.R.id.text1};
//setListAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, displayFields, displayViews);
ArrayAdapter<String> musicAdapter = new ArrayAdapter<String>(getBaseContext(), R.layout.list_songs, listOfMp3s);
//list.setAdapter(listOfMp3s);
}
Have a look a the simple and straight forward tutorial How to create a ListView using ArrayAdapter in Android.
You need to generate a String array and attach the array to an Array adapter
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, YourStringArray);
and then plug this adapter to your ListView
yourListView.setAdapter(adapter);
Or you could create your own custom adapter by extending ArrayAdapter<YourItem> and attaching YourItem array to it.
I would suggest you to create a custom ArrayAdapter to populate a ListView from your objects the way you want.
The advantage of this technic is that you gain a Views recycle mechanism that will recycle the Views inside you ListView in order to spend less memory.
In Short you would have to:
1. Create an object that represents your data for a single row.
2. Create an ArrayList of those objects.
3. Create a layout that contains a ListView or add a ListView to you main layout using code.
4. Create a layout of a single row.
5. Create a ViewHolder that will represent the visual aspect of you data row from the stand point of Views.
6. Create a custom ArrayAdapter that will populate the rows according to you needs, in it you will override the getView method to specify exactly what the row data would be.
7. Finally assign this ArrayAdapter to your ListView in onCreate.
You can get an idea of how to implement this by reading this blog post I wrote:
Create a Custom ArrayAdapter
I am still unclear about which adapter to use in a situation that doesn't require the basic, SimpleAdapter. There are BaseAdapters, ArrayAdapters, CustomAdapters, etc.
I would like to make a ListView with a simple layout like seen in the comment section in the Google PlayStore. A TextView on one side, and an image that pops up a context menu of some sort.
What adapter would I use for this that would work best?
In your case it is easier to use a SimpleAdapter. Just provide your custom layout and connect the data with the widgets' id. Something similar to this:
List<HashMap<String,String>> datalist = new ArrayList<HashMap<String,String>>();
HashMap<String, String> map = new HashMap<String,String>();
map.put("text", "some text");
map.put("image",Integer.toString( R.drawable.your_image_to_popup_a_menu ));
datalist.add( map );
String[] from = { "text","image" };
int[] to = { R.id.txt,R.id.img };
SimpleAdapter adapter = new SimpleAdapter( this, datalist, R.layout.your_layout, from, to);
Do not forget to identify the layout's TextView and ImageView as "#+id/txt" and "#+id/img" respectively.
(If your image is always the same, just set it in the layout and forget linking the R.id.img part)
If I have a hashmap containing the following:
Hashmap contains (String, String)
How can I instantiate a custom adapter? The custom adapter should extend baseadapter.
I need to combine both key and value so it looks something like "KEY+VALUE", "KEY+VALUE"... and assign this to an array VALUES. The array VALUES is used later on when I insantiate my custom adpter.
Instantiation should look something like this:
MyCustomAdapter adapter = new MyCustomAdapter(this, android.R.layout.simple_list_item_1, VALUES);
setListAdapter(adapter)
I am lost here so code would be a big help.
THANKS
graham
The following list is using as its datasource a string array called items.
public ArrayList<String> myList = new ArrayList<String>(Arrays.asList(items));
however items is a string array which I would like to stop using and instead start using concatenated key+value pairs from my hashmap
so instead of the user being presented a list of items he will be presented a list of key+value pairs taken from the hashmap hm
I don't think you need to use a custom adapter. Your layout is quite simple, you need only a textView, so you can use ArrayAdapter.
For you example you can do:
HashMap<Integer,String>hm=new HashMap<Integer,String>();
Vector<String>elements=new Vector<String>();
for(int i=0; i<=10;i){
hm.put(i,("num"+i));
}
for (Entry<Integer, String> e : hm.entrySet()) {
String newString=e.toString();
elements.add(newString);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, elements);
list.setAdapter(adapter);
I have an array of apps(PInfo) and I am wondering how do I add that array to a listview?
ArrayList<PInfo> info = appsGetter.listPackages();
int number = 0;
PInfo appInArray;
while(number < info.size()){
appInArray = info.get(number);
}
This is what I have at the moment, the listPackages() is a method that is getting the names of the apps from the device.
At the moment I am trying to get the information out of the array one by one and add it to the listview like that. Is that how I should do it our should I add the array straight to the listview? And how do you do that?
You can use an ArrayAdapter and initialize it like this:
ArrayAdapter<PInfo> adapter = new ArrayAdapter(context,
android.R.layout.simple_list_item_multiple_choice,
info);
Then you can you use ListView.setAdapter(adapter).
I'm not sure if this is what you're asking though. So please clarify further if this is not what you're asking
Try using an Adapter. For example (using just the String value of an object) you could do the following:
ListView listView = (ListView)findViewById( R.id.myListView );
final ArrayList<String> listItems = new ArrayList<String>();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, listItems );
listView.setAdapter( adapter );
Just a quick example, but I hope it gives you a starting place. Just make sure if you add values to your data source later (in this case the ArrayList) to call the adapter's "notifyDataSetChanged()" method so that it can be properly reflected in whatever has been bound to the adapter (in this case the ListView).
You need to use an ArrayAdapter. Just search for a ListView and ArrayAdapter sample online. It's quite simple once you see it done.
Is it possible to update a SimpleAdapter? I have a list of data and a footer that says "See Next Results" When that list item is clicked I capture the event and get new data. I then want to replace the data in the ListView with this new data but I can't figure out how to do it. Any Ideas? I don't want to use an ArrayAdapter, cause as far as I can see the items can only hold one string where I need it to hold multiple strings and ints.
Update: According to del116, you can indeed give SimpleAdapter a mutable map and then manually call the adapter's notifyDataSetChanged method when you need the list to update. However, my point below stands about the documentation of SimpleAdapter specifying that it is for static data; using it for mutable data is going counter to its design, so if you use this technique I would be sure to check on whether it continues to work in new Android releases as they emerge.
(Original commentary follows:)
If you look at the SimpleAdapter description it says it is "An easy adapter to map static data to views defined in an XML file." I've added the emphasis -- put simply, SimpleAdapater isn't built for use with data that changes; it handles static data only. If you can't use an ArrayAdapter because your data has more than a single bit of text, then you will either have to build your own custom ListAdapter, or put your data in a DB and use one of the CursorAdapters.
As a last resort, if you don't need much performance, you could update a ListView backed by a SimpleAdapter by building a whole new SimpleAdapter instance any time your data changes and telling the list view to use it via setListAdapter.
ListView lv= (ListView) findViewById(R.id.loglist);
ArrayList<Map<String, String>> list = buildData();
String[] from = { "time", "message" };
int[] to = { R.id.logtime, R.id.logmessage };
adapter = new SimpleAdapter(getApplicationContext(), list,R.layout.log_list_row, from,to);
lv.setAdapter(adapter);
Call this function each time to update the ListView. Keep in Mind that You have to update the list variable with new Data..
Hope this Helps..:)
SimpleAdapter is meant for static data, so your performance may vary. The best solution is probably to switch to a different type of adapter, such as ArrayAdapter, or make a new SimpleAdapter every time you change the dataset.
I was not able to get notifyDataSetChanged() to work on updating my SimpleAdapter, so instead I tried first removing all views that were attached to the parent layout using removeAllViews(), then adding the ListView, and that worked, allowing me to update the UI:
LinearLayout results = (LinearLayout)findViewById(R.id.results);
ListView lv = new ListView(this);
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
SimpleAdapter adapter = new SimpleAdapter( this, list, R.layout.directory_row,
new String[] { "name", "dept" }, new int[] { R.id.name, R.id.dept } );
for (...) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", name);
map.put("dept", dept);
list.add(map);
}
lv.setAdapter(adapter);
results.removeAllViews();
results.addView(lv);