Confused by use of `from` and `to` params in Android SimpleAdapter class - android

The official Android docs say the following:
from A list of column names that will be added to the Map associated with each item.
to The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.
I've accomplished this as follows:
final String[] from = new String[] {"TEXT1", "TEXT2"};
final int[] to = new int[] {android.R.id.text1, android.R.id.text2};
a = new SimpleAdapter(this, list, resource, from, to)
I'm able to set data in the view just fine, but im extremely confused as to why the from parameter is necessary. Why not just supply a list of xml resources? Why do a list of (column names?) need to be passed in as well?
Please help me understand.

The first N views in this list are given the values of the first N columns in the from parameter.
This means the values in the from parameter will be set in the corresponding views in the to parameter. According to the SimpleAdapter Android docs, if the views are TextViews, then a list of Strings is expected, and will be bound to the TextViews by calling setViewText(TextView, String).

Related

Creating a Favorites List using expandable list view that can create groups? Android

I've been searching for a way to create a favorites list. Something like the expandable list view but instead of having fixed groups it should allow the user to add groups he desires. I use a HashMap<String, List<String>> for storing the subgroups and their Group's name. I also use a text file to load stored groups and subgroups.
Is it possible to reuse a List<String> to enter values into the HashMap. My multiple attempts have failed so I was wondering if there is a possible work around. This is basically what I did for my last attempt:
myList.add("Carrot");
myList.add("Cabbage");
myHash.put("Group1", myList);
myList.clear();
myList.add("Orange");
myList.add("Apple);
myHash.put("Group2", myList);
When I view the results on the emulator it displays Orange and Apple for both groups. Any suggestions would be appreciated since I don't program in java often.
myList.add("Carrot"); myList.add("Cabbage");
myHash.put("Group1", myList);
//replace clear to new object.
myList = new ArrayList();
myList.add("Orange"); myList.add("Apple);
myHash.put("Group2", myList);

Checking the contents of the record before it is shown to listview using SimpleCursorAdapter

I would like to display a gray star image if column "status"==0 and display a golden star if column "status"==1. My code can display the record to listview, but without checking the "status" column first:
public void loadPertanyaan(){
Cursor soal = DBAdapter.fetchSemuaSoal();
String[] from = new String[]{DBAdapter.KEY_SOAL_PERTANYAAN};
int[] to = new int[]{R.id.txt_PilihSoal_Pertanyaan};
SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(this, R.layout.list_row, soal, from, to);
setListAdapter(dataAdapter);
}
Then I add code to check status column using moveToNext() after setListAdapter(dataAdapter). But the application always force stopped, code to check "answered" column below -
ImageView img = (ImageView) findViewById(R.id.imgBintang);
while(soal.moveToNext()){
if(soal.getInt(soal.getColumnIndex("status"))==1)
img.setImageResource(R.drawable.bintang_mas);
else
img.setImageResource(R.drawable.bintang_abu);
}
How can I check "status" column before displaying to ListView. Thanks in advance.
Agi, You need a good tutorial like link Populating a ListView with a CursorAdapter . Look at code bindView similar to getView method, mentioned in a previous answer. getView is less intensive so it may be good enough for you.
Look at LinearLayout resource XML. In it, there are 2 TextView elements. Instead, I think you need more columns in your ListView, like TextView, popular Checkbox, and an image for the star (bintang).
The code is different than your thoughts in design. It gets and displays data in virtual mode, and you can check the data by the position or Cursor parameter.
Good luck and tell me how it goes for you...

How to switch database string to drawable resource in Android?

I have this codesnippet that gives me the fetched KEY_COLOR from database, and puts it at R.id.text_color for attachment in xml..
KEY_COLOR equals atm "Red" and "Blue".
I dont want "red" and "blue" i want images instead, a little dot.
private void fillData() {
Cursor notesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(notesCursor);
// Create an array to specify the fields we want to display in the list (only TITLE)
String[] from = new String[]{NotesDbAdapter.KEY_TITLE, NotesDbAdapter.KEY_COLOR};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.text1, R.id.text_color};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this,
R.layout.notes_row, notesCursor, from, to);
setListAdapter(notes);
}
I simply want to change the outcome of NotesDbAdapter.KEY_COLOR to R.drawable.red_dot or blue_dot... But i cant figure ot how...
Do you understand? :/
Use the same fillData that you have.
However, you need to have the URI to the drawables in the database with column name NotesDbAdapter.KEY_COLOR when inserting them into the database.
For the sample code below change image to your drawables, if its blue or red.
String nameToInsert = "android.resource://my.package.name/"+R.drawable.image;
Of course change my.package.name to the correct packagename.
Just let R.id.text_color be the id of the imageview and not a textview.
From the docs for SimpleCursorAdapter:
Binding occurs in two phases. First, if a SimpleCursorAdapter.ViewBinder is available, setViewValue(android.view.View, android.database.Cursor, int) is invoked. If the returned value is true, binding has occured. If the returned value is false and the view to bind is a TextView, setViewText(TextView, String) is invoked. If the returned value is false and the view to bind is an ImageView, setViewImage(ImageView, String) is invoked.
From the docs for SimpleCursorAdapter.setViewImage (ImageView v, String value):
By default, the value will be treated as an image resource. If the value cannot be used as an image resource, the value is used as an image Uri.
So basically if you make it so the KEY_COLOR column of your DB contains Uris to your images and replace R.id.text_color in your to array with the resource id of an ImageView, in theory your SimpleCursorAdapter will automatically fill the ImageView with the image resolved from the Uri.
Never tried it myself but it seems like how it should work.
See this example of how to use an image with a textview in list rows, Fancy ListViews Part One. It's written by Mark Murphy aka CommonsWare here on SO. It actually uses a different method to what I was suggesting with a view binder and uses the getView(...) method to set images and text for each row.

listview in android

i need to display four names continues in listview
for example
aravind
bose
guru
moorthy
aravind
bose
guru
moorthy
the set of values to be displayed in listview for "n" number of times
if i have mension 10times.the four names should be display for 10 times in listview.help me
for this you need to pass "n" as a parameter while calling your custom adapter.
Since its your custom adapter create your arrayList in the adapter to be used for the list view using the parameters of ArrayList(data to be displayed) and the "n". Using these create another arrayList which is used to display the listView data..
the call would be somewhat like this:
listAdapter = new CustomListAdapter(this, R.layout.list_view,
countryStringList, n);

Custom AutoComplete in Android

I want to display List of Contact Names with the respective phone numbers
like
Vikas Patidar <9999999999>
Rahul Patidar <9999999999>
using AutoCompleteTextView when a user type text in the mobile number field.
In default style I can only display the list of names.
Can anyone please tell me how can I implement this so that when a user select any item in list and I can display number of that in Mobile number field.
You need use adapter for this task. For example, with SimpleAdapter:
SimpleAdapter adapter = new SimpleAdapter(context, list, R.id.row_layout, new String[] { "Name", "Phone" }, new int[] { R.id.name, R.id.phone });
autoCompleteField.setAdapter(adapter);
For this you need make a layout XML file and list must be of type List<? extends Map<String, ?>. Strings in 4th parameter are keys for maps in list. Ints in 5th parameter are identifiers of components in layout file.
Or you can extend any adapter and use it. See this link for reference.

Categories

Resources