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.