Set contentDescription dynamically when populating list of TextViews - android

I'm trying to update Android's sample code "API Demos" application so that TextView items in the list of the default activity have the "content description" needed for accessibility. This is what the initialization looks like for the list in this activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String path = intent.getStringExtra("com.example.android.apis.Path");
if (path == null) {
path = "";
}
setListAdapter(new SimpleAdapter(this, getData(path),
android.R.layout.simple_list_item_1, new String[] { "title" },
new int[] { android.R.id.text1 }));
getListView().setTextFilterEnabled(true);
}
And the getData function creates a Map with the appropriate intentions specified for when someone taps on one of the TextView list elements. Internally, it uses this function:
protected void addItem(List<Map<String, Object>> data, String name, Intent intent) {
Map<String, Object> temp = new HashMap<String, Object>();
temp.put("title", name);
temp.put("intent", intent);
data.add(temp);
}
This is how the Map is populated with title, which as you can see above is mapped to #android/text1 in the layout XML.
I want to set the content description of each of the TextViews with the same value as name. Things I've tried:
(1): Looping through child items of the ListView that is generated and calling item.setContentDescription(xxx) on each of them. This didn't work; apparently at the point I'm looping through the items they're not visible/accessible/existent and either way trying to call setContentDescription blows up.
(2): Creating a new layout XML for this modified TextView, that includes android:contentDescription. The layout XML looked like this
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:contentDescription="#android:id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight">
</TextView>
I then made sure that "contentDescription" was added to the Map returned by addItem, and changed the initialization code to look like this:
setListAdapter(new SimpleAdapter(this, getData(path),
R.layout.simple_list_item_with_desc, new String[] { "title", "contentDescription" },
new int[] { android.R.id.text1, android.R.id.text2 }));
In other words, I was trying to get "contentDescription" from my static data map to populate the field in the layout XML defined by android.R.id.text2.
This also didn't work--no failure, but when I examine the list in uiautomatorviewer, no content description is available. It could be because I don't understand what android.R.id.text2 means, other than being a placeholder for a value of some kind. I also tried android.R.id.content with the same result.
So my question is, how do I set the contentDescription of these TextViews dynamically?

I want to set the content description of each of the TextViews with the same value as name. Things I've tried:
(1): Looping through child items of the ListView that is generated and calling item.setContentDescription(xxx) on each of them. This didn't work; apparently at the point I'm looping through the items they're not visible/accessible/existent and either way trying to call setContentDescription blows up.
You are correct, your first method failed because none of the TextViews have been created yet. The problem is they are never visible all at the same time (unless you only have a handful of rows and they fit on the screen without scrolling.)
Solution: Extend SimpleAdapter and override getView(). This method is called each time a row is displayed. It gives you access to the TextView so you can call setContentDescription() here.

#Sam provided a solution, and I wanted to also contribute back the code to my extension class in case it's useful to anyone else:
package com.example.android.apis;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class SimpleAdapterWithDesc extends SimpleAdapter {
public SimpleAdapterWithDesc(Context context,
List<? extends Map<String, ?>> data, int resource, String[] from,
int[] to) {
super(context, data, resource, from, to);
}
public View getView(int position, View convertView, ViewGroup parent) {
TextView myView = (TextView)super.getView(position, convertView, parent);
myView.setContentDescription(myView.getText());
return myView;
}
}

Related

Retrieve array from SQLite in listview with more than a colum (ANDROID)

So I want to create a listview that consists of more than a column. I already succeed called the database, but the layouting still not work. I think this is because the all my data are put in one array and its difficult to make them in three columns. anyone can find solution for me?
My program result is like this in listview:
John Doe 12 Argentina
Marilyn Rose 32 Russia
Annabella 19 United States
However what I want is more like this:
John Doe 12 Argentina
Marilyn Rose 32 Russia
Annabella 19 United States
From what I read, we will need 2 XMLs. One for listview, and another is for layouting (give space between text). And One .JAVA called adapter to connect my MainActivity.java and layouting XML.
add:
I already tried using two XMLs. one XML, lets call it main.XML is for calling ListView. And grid.XML, is where i put android:layout_alignParentLeft="true" (to create spaces).
I used MyAdapter.JAVA to convertview in grid.XML. and in MainActivity.JAVA i called MyAdapter. However my code in MainActivity became error when its connected it MyAdapter.
this was my code that gave error java.lang.RuntimeException. So I had to delete it.. more information about it, please check two last code...
MainActivity.java (error)
public static ArrayList<String> arraydealer = new ArrayList<String>();
MyAdapter bohwat = new MyAdapter(MainActivity.this, arraydealer);
lvcustom.setAdapter(bohwat);
And here is the code that is working well. It uses class MainActivity, AstraDB, and MySetGet, and main.XML. Other class thats not working is MyAdapter and grid.xml
This is how I called my database:
public class MainActivity extends Activity
{
public static ArrayList<String> arraydealer = new ArrayList<String>();
AstraDB astrahandler = new AstraDB(this);
Spinner spDealer;
ListView lvcustom;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lvcustom = (ListView)findViewById(R.id.custom_lv);
ShowListView();
}
private void ShowListView()
{
astrahandler.getAllDealer();
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, arraydealer);
lvcustom.setAdapter(adapt);
}
}
This code of ArrayList in Activity and String name in AstraDB are very important to connect my MainActivity with the database but it seems this create trouble in layouting. because they are contained in ONE array
And this is function to get all data in my DB. its on AstraDB.java:
public List<MySetGet> getAllDealer()
{
List<MySetGet> info = new ArrayList<MySetGet>();
db = DBHelper.getReadableDatabase();
// Select All Query
String selectQuery = "SELECT * FROM Dealer";
cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
MySetGet lets = new MySetGet();
lets.setDealerID(Integer.parseInt(cursor.getString(0)));
lets.setDealerName(cursor.getString(1));
lets.setDealerOwner(cursor.getString(2));
lets.setDealerWil(cursor.getString(3));
String name = cursor.getInt(0) +
"\u00A0 "+ cursor.getString(1)+
"\u00A0 "+ cursor.getString(2)+
"\u00A0 "+ cursor.getString(3);
MainActivity.arraydealer.add(name);
//add
info.add(lets);
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
//return contentdealer;
return info;
}
The MySetGet in getAllDealer() connects with MySetGet.java where I put setter and getter so the data can become object. which is more like this:
public int getDealerID(){ return DealerID;}
public void setDealerID(int DealerID) { this.DealerID = DealerID; }
Code to connect other XML with Java but still gave error:
grid_dealer.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_height="wrap_content"
android:layout_width="50dp"
android:id="#+id/col1"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text=""/>
<TextView
android:layout_height="wrap_content"
android:layout_width="100dp"
android:id="#+id/col2"
android:layout_toRightOf="#id/col1"
android:layout_centerVertical="true"
android:text=""/>
important code in MyAdapter.java:
public class MyAdapter extends BaseAdapter
{
Context context;
ArrayList<MySetGet> dealerlist;
public MyAdapter(Context context, ArrayList<MySetGet>list)
{
this.context = context;
dealerlist = list;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
MySetGet yay = dealerlist.get(position);
//this is to customize the layout of the listview
if(convertView == null)
{
LayoutInflater inflater = null;
inflater = (LayoutInflater)context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.grid_dealer, null);
}
TextView tvID = (TextView)convertView.findViewById(R.id.col1);
tvID.setText(yay.getDealerID());
TextView tvName = (TextView)convertView.findViewById(R.id.col2);
tvName.setText(yay.getDealerName());
TextView tvOwner = (TextView)convertView.findViewById(R.id.col3);
tvOwner.setText(yay.getDealerOwner());
return convertView;
}
}
Please help me. I am very new to this. Is there a way to modify my code without changing too much on how I called my database? The class and XML below works fine in showing database, but didnt create a neat layout space between columns
Working class : AstraDB, MainActivity, MySetGet
Working XML : main.xml
Im sorry, if the post becomes longer. I want to clarify several things so that there is no misunderstanding.
you can use android:layout_weight="" for better arrangement of views
Using SimpleCursorAdapter is the ideal solution in your case. Please checkout a tutorial here that will take you through the steps in achieving what you want.
In simple_list_item_1 layout if you arrange items relative to each other you will get the result what you are getting now. If you want proper formatting, relate the elements to left, center and right using android:layout_alignParentLeft="true", android:centerHorizontal="true" and android:layout_alignParentRight="true" respectively

Can't get custom ListAdapter to work

I have followed various "how to" examples to the letter (or so I thought), but I still can't get my custom ListAdapter to work. I have a dialog with a list view containing strings which are references to an array of objects (of class "Notam"). I want to set the colour of each list item according to an attribute of the referenced object.
(Before you read my code, I have a quirk that the braces must line up or I can't see where the blocks are. I don't like the convention of putting an opening brace at the end of the same line.)
This is the code for the custom class (just as a test I am trying to set the text colour of each item to magenta):
private class GotoAdapter extends ArrayAdapter<String>
{
private ArrayList<String> items;
public GotoAdapter(Context context, int textViewResourceId, ArrayList<String> items)
{
super(context, textViewResourceId, items);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null)
{
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.goto_row, null);
}
String s = items.get(position);
if (s != null)
{
TextView tt = (TextView) v.findViewById(R.id.text1);
if (tt != null)
{
String s1 = (String)tt.getText(); // this is always an empty string!
tt.setTextColor(0xFF00FF); // this has no effect!
}
}
return v;
}
}
String s has the displayed text as expected (except you can't see it on the screen) when using this derived class), but the text in the returned TextView is always an empty string, and setting the colour has no effect.
This is the code that displays the dialog when a "Goto" button is clicked in my main view:
mGotoButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// The pre-loaded array gets round a problem which I read about somewhere else
// (the ArrayList gets cleared again below)
String[] array = {"one", "two", "three"};
ArrayList<String> lst = new ArrayList<String>();
lst.addAll(Arrays.asList(array));
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.goto_dialog);
dialog.setTitle("Choose Notam");
// Create the list view and adapter
final ListView list = (ListView) dialog.findViewById(R.id.goto_list);
// If I replace this reference to my custom adapter...
final GotoAdapter adapter = new GotoAdapter
(mContext, R.layout.goto_row, lst);
// ... with this normal one, everything works!
// (but of course now I can't get access to the objects.)
// final ArrayAdapter<String> adapter = new ArrayAdapter<String>
// (mContext, R.layout.goto_row, lst);
list.setAdapter(adapter);
// Populate the adapter
adapter.clear(); // first clear the silly preset strings
// Notam is my object class.
// Spine.mNotamsDisplayed is a public static NotamArray.
// class NotamArray extends ArrayList<Notam>
// Spine is my main activity where I keep my global (app-wide) stuff.
for (Notam notam : Spine.mNotamsDisplayed)
{
// This gets the reference string from the Notam object.
// This is what goes into the list.
String s = notam.getReference();
adapter.add(s);
}
// Sort into alphabetical order
adapter.sort(new Comparator<String>()
{
public int compare(String arg0, String arg1)
{
return arg0.compareTo(arg1);
}
});
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> a, View v, int pos, long id)
{
String s;
int i;
s = (String)a.getItemAtPosition(pos);
// This static function returns the index in Spine.mNotamsDisplayed
// which is referenced by the reference string s.
// I have to do this because I lost the one-for-one correlation of page
// indexes with list view entries when I did the sort.
i = NotamArray.findNotamIndexByReference(Spine.mNotamsDisplayed, s);
if (i >= 0)
{
// This is what the Goto button and dialog is all about: this
// just moves my main view's pager to the page that was selected.
mPager.setCurrentItem(i);
}
dialog.dismiss();
}
});
dialog.show();
}
});
This is my xml for the dialog (goto_dialog.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#+id/goto_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
And this is my xml for the list view row (goto_row.xml):
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#00FF00"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:padding="2dp"
android:textSize="20dp"
/>
(I set the text colour to green so I could see that this bit was working if I used the standard list view adapter. (Sure enough the text of every entry was green. However no text could be seen if I used my custom adapter, although it was there - I assume black on black.)
There must be someone out there who can spot what must be a trivial error I have made - please!
From what I read, it seems like you want to set the text color of every list item to match the color you have in your array.
I want to set the colour of each list item according to an attribute of the referenced object.
However, your initial array is set as
String[] array = {"one", "two", "three"};
So this will lead to problems later when you are dynamically setting the text color based on your array. But I'm sure you meant to change that later.
When you use a standard array adapter, it just shows the items in the array as a text, that's why:
if I used the standard list view adapter. (Sure enough the text of every entry was green. However no text could be seen if I used my custom adapter
To see if your custom adapter is working (changing color), you can just start off by adding one line to your TextView of goto_row.xml file:
android:text="Test String"
Now it will show "Test String" with different colors, and the
String s1 = (String)tt.getText();
above line will get "Test String"
I found the trivial error that I hinted at at the end of my question. It was this line in the custom adapter:
tt.setTextColor(0xFF00FF);
It seems that 0xFF00FF is not a valid colour value, which is why I saw nothing on the screen.
Changing that to:
tt.setTextColor(Color.rgb(255, 0, 255);
fixes the problem, and the default green colour is changed to magenta, and I can set the text to the value I want. So I can now set the individual row colours to what they need to be.
Thanks to #LukasKnuth and #tigerpenguin for pointing me in the right direction.

Android - Show textview by clicking on listview item

I am creating my very first Android application, but i stuck unfortunately. The application would be very simple: On the starting page there is a ListView with items like:
1st group
2nd group
3rd group
...
By clicking on any of these items a new page would show up with a single textview element that would have some description. Like you click on '1st group' item, the listview gets hidden, and a new page appears with '1st group description' text.
So far I can show the listview with the items, but when I click on them, nothing happens (i guess I miss some basic stuff, but as a very newby, i cannot find it out easily).
import java.util.ArrayList;
import java.util.Arrays;
import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;
import android.view.*;
public class SimpleListViewActivity extends Activity {
LinearLayout.LayoutParams layoutParams;
LinearLayout ll;
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );
// populate the List of groups
String[] GROUP = getResources().getStringArray(R.array.group);
ArrayList<String> GrList = new ArrayList<String>();
GrList.addAll( Arrays.asList(GROUP) );
// Create ArrayAdapter using the list.
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, GrList);
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
ll = (LinearLayout)findViewById(R.id.LinearLayout);
layoutParams = new LinearLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mainListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView t = (TextView) findViewById(R.id.textView1);
String[] DESC = getResources().getStringArray(R.array.desc);
t.setText(DESC[position]);
ll.addView(t);
//This is the point that is wrong for sure (and others maybe also). I cannot get the textview shown
}
});
}
}
Thanks for your help.
Have you tried displaying a toast message or setting a breakpoint within your onItemClick() method to verify that its not being reached? My guess is that it is and you are running into one of the issues described here:
Refreshing a LinearLayout after adding a view
I am assuming your R.layout.main is holding a listview and a linear layout with ids R.id.mainListView, and R.id.LinearLayout respectively.
Example: I left out some of the obvious attributes you would need like height width etc..
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
>
<ListView android:id="#+id/mainListView" />
<LinearLayout android:id="#+id/LinearLayout" />
</RelativeLayout>
In your on item click, all you will do is add a textview as you have done, then set the mainListView.setVisibility(View.INVISIBLE) and ll.setVisibility(View.VISIBLE).
If your R.layout.main is not using a RelativeLayout as the root node, but is instead using a LinearLayout you should still be able to achieve the same effect by setting the Visibilities to View.GONE if you want it to hide, and View.VISIBLE if you want it to show.
To revert back to being able to see the list view I would override onBackPressed() in the activity, to invert the Visibilities on the two items. Also remember to remove all views from the linear layout so that the next time an item in the group is selected it will be the only item in the linear layout when it is added.
There are much easier ways to accomplish this, such as firing off a new activity for viewing the next item, but seems you are keeping everything within one activity. I would also think about using a ListActivity instead of base activity class.
Hope this helps.
First off stop using the word page. Call it an activity (gotta get you in the Android zone)
Once the click happens start a new activity like so:
mainListView.setOnItemClickListener(new OnItemClickListener() {
String textToPass = GrList.get(position)
Intent i = new Intent(getBaseContext(), SecondActivity.class);
i.putExtra("textToPass", textToPass);
startActivity(i);
}
You'll obviously need to have that second activity with its corresponding layout file defined. Also in the second activity look up how to get the bundle and extras from the first activity in order to get the textToPass String

Adapter for list view

please guide me with this program. Why do we need to use an array adapter to show the list? What is this "adapter", and can we display things directly in the ListView, without an adapter? Like, can we set setListAdapter(names) instead of setListAdapter(adapter);? Thanks.
Here is the code:
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class Episode7 extends ListActivity {
String[] names = {
"Elliot","Geoffrey","Samuel","Harvey","Ian","Nina","Jessica",
"John","Kathleen","Keith","Laura","Lloyd"
};
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an ArrayAdapter that will contain all list items
ArrayAdapter<String> adapter;
/* Assign the name array to that adapter and
also choose a simple layout for the list items */
adapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
names);
// Assign the adapter to this ListActivity
setListAdapter(adapter);
}
}
From the android API reference,
An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.
It basically a set of interfaces that determine how the data will be handled by the list. You can use different pre-made adapter classes in your lists or create your own if you want to present custom data.
Take a look at this page in the Dev Guide: http://developer.android.com/guide/topics/ui/binding.html
Lars Vogel has a nice tutorial also: http://www.vogella.de/articles/AndroidListView/article.html
The Adapter acts as both a container for the information you want to display, and allows you to change how it is displayed by over-riding the getView() method of the adapter. Normally, by default, the adapter will call the toString() method of the Object used to create the Adapter and set the text in the TextView that is referenced in the layout provided by android.R.layout.simple_list_item_1... but by over-riding the adapter's getView(), you can have a more complicated layout display for the list.
To answer the initial question... you must use an adapter with a ListView.
This is how I do it and it works for me:
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListViewDemo extends Activity {
// --------- Create your string array, adapter and ListView
String[] items = {"Cars", "Money","Vacation","Electronics",
"Shoes","Jewelry", "Buku bucks","Cash","Ham","Swag","Straight Cash","Homies","Roll Dawgs","Nate Dogg","Wiz Khalifa","Mac Miller","Chitty Bang",
"Sam Adams","Technine","Kanye West","Rims","Escalade","Spreewells","Chrome Rims","24's",
"Lebron James","Dwayne Wade","Andre Iguodala","Allen Iverson","Jodi Meeks",
"Levoy Allen","Mo Williams","Eric Snow","Alien Iverson","Laptop","Phone","Tablet"};
ArrayAdapter<String> adapter;
ListView cashList;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cashList = new ListView(this);
// create the array adapter<String>(context, layout, array)
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
// add the adapter to the list
cashList.setAdapter(adapter);
// set the list as the content view
setContentView(cashList);
}
}

Text Parsing in android

I am stuck with text parsing in android. I have to read a textfile from url and display the contents in a listview. The contents of textfile include images and texts which are seperated by delimeters (like ^, ~,|,~^~^~). How can i remove the delimeters and display in a listview with the help of array adapter class? Can anyone help with the code?
I think array adapter might be two simplistic to do this. I mean you could use it, but it will end up being more complicated than the overall problem. So I think you can figure out how to parse the text file into let's say Text and Image objects (for you to define). Then you need an adapter. There are few places out there with some code snippets. I think SimpleAdapter might work well for this scenario.
For simplistic reasons let's say you have a layout for a list row with both an ImageView and a TextView
layout.xml
<LinearLayout xmlns:android="..."
android:layout_height="wrap_content"
android:layout_width="fill_parent"
>
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
and now we need to use an adapter to fill that
So the constructor is the meat of the class for clients to use.
SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
So if going with the assumption that the file is now parsed and we have a list of Objects
final Context context = ...;
final ListView listView = ...;
List<Object> objects = ...;
List<Map<String, Object>> mappings = new ArrayList<Map<String, Object>>(objects.size());
for(Object o : objects) {
HashMap<String, Object> data = new HashMap<String,Object>();
mappings.add(data);
if (o instanceof Image) {
data.put("image", ((Image)o).getBitmap());
data.put("text", "");
} else {
data.put("image", null);
data.put("text", String.valueOf(o));
}
}
SimpleAdapter adapter = new SimpleAdapter(context, mappings, R.layout.layout, new String[] { "image", "text"}, new int[] {R.id.image, R.id.text});
adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
public boolean setViewValue (View view, Object data, String textRepresentation) {
if ( data instanceof Bitmap && view instanceof ImageView) {
// This is the main reason for the ViewBinder
ImageView imageView = (ImageView) view;
imageView.setImageBitmap((Bitmap)data);
return true;
} else if (view instanceof TextView) {
TextView textView = (TextView)view;
textView.setText(textRepresenation);
return true;
}
return false;
}
});
listView.setAdapter(adapter);
Now if you need help parsing out the file, well you need to provide a better description since a text file with a deliminator doesn't seem like it's enough to tag two different types of data.
But if it is and you have the memory just load it as a string and split(deliminator). Then for each item if you decide it is a String cool, keep it as a string and put it in the list. If you decide it is an Image decode the bitmap and put it in the list. plug that list into the object list in the code above and I think it's all set.
four digit numbers which are there are the images ...... i.e 2531, 2215, 6574, 7158.

Categories

Resources