tags:

views:

64

answers:

2

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?

A: 

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.

Greg
My textfile contains images which are in the form of code(say 6468) followed by delimeter then text, delimeter, tex,t and so on .................................. Eg: abcd^2531~abcdCity^2215~ abcd~^~^~14:00 HRS~abcd ~~2010101610531~abcd~abcd~6~N~20101016~|abcd^6574~abcd^7158~abcd~^~^~14:00 HRS~Old abcd~~2010101610854~abcd~abcd~6~N~20101016~| How will i split the delimeters and store it in a string? .... I hav tried many ways bt m nt successful.
snn
I am sorry I don't understand the format you are explaining. Where is the image here?
Greg
A: 

four digit numbers which are there are the images ...... i.e 2531, 2215, 6574, 7158.

snn
Plz forget about the images i thnk i will keep the images static ....... Just let me knw how can i split all these delimeters .......
snn