开发者

how to create a Listview for dynamic content after parsing xml in android

I have parsed an xml from the web using SAX parser. Now I have a problem displaying the parsed data. I want to show an ImageButton and a textview side by side. For that I have开发者_StackOverflow used the relative layout using code as I have to use list of Imagebuttons and textview (not using xml).

      // Create a new TextView to display the parsing result later.
        RelativeLayout layout = new RelativeLayout(this);


        /* Create a new TextView to display the parsingresult later. */
        TextView tv = new TextView(this);
        tv.setId(1);

        // Defining the layout parameters of the TextView
         RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                 RelativeLayout.LayoutParams.WRAP_CONTENT,
                 RelativeLayout.LayoutParams.WRAP_CONTENT);
         lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, tv.getId());
         // Setting the parameters on the TextView
         tv.setLayoutParams(lp);

         ImageButton imgBtn = new ImageButton(this);
         imgBtn.setId(2);
         RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
                 RelativeLayout.LayoutParams.WRAP_CONTENT,
                 RelativeLayout.LayoutParams.WRAP_CONTENT);
         lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, imgBtn.getId());
         imgBtn.setLayoutParams(lp1); 
          layout.addView(tv);
        layout.addView(imgBtn);

I have to show Something like this ::

ImageButton textview

ImageButton textview .. n (n depends on the no. of entries in the xml after parsing)

After searching in the web for a day Im confused how to do this. Some used

listview.setAdapter(new ListAdapter(Splash.this, R.id.list_view, mListItem)); -- where mListItem is an ArrayList of string but i have a relativelayout which has imagebutton and textview so cann't use this

Should I go for table view or list view and also provide some sample code. Pls suggest.

Thanks in advance. Pavan


You should use xml layout to make it easier.

Create list_item.xml under res/layout this layout represent a single item on the ListView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  >
  <ImageButton
        android:id="@+id/mImageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
   <TextView
        android:id="@+id/mTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        />
</LinearLayout>

Create List of Map, which each Map represent a single item in the ListView.

List<Map<String, Object>> list = new ArrayList<HashMap<String, Object>>();
Map<String, Object> item;

item = new HashMap<String, Object>();
item.put("textPart",  "The Text Part of Parsed Data");
item.put("imagePart", /* Drawable instance of image data here */ );
list .put(item);

item = new HashMap<String, Object>();
item.put("textPart",  "The Text Part 2");
item.put("imagePart", /* yet another Drawable instance*/);
list.put(item);

// and so on ...

Then bind the data to the list_item.xml using SimpleAdapter

SimpleAdapter adapter;
String[] source = new String[]{"imagePart", "textPart"};
int[] target = new int[]{R.id.mImageButton, R.id.mTextView};

// "list" is the List<Map<String, Object>> instance
adapter = SimpleAdapter(this, list, R.layout.list_item, source, target);

adapter.setViewBinder(new SimpleAdapter.ViewBinder(){
    @Override
    public boolean setViewValue(View view, Object data, String textRepresentation) {
        if (view instanceof ImageView && data instanceof Drawable) {
            ((ImageView) view).setImageDrawable((Drawable) data);
            return true; // we handle it manually
        }
        return false; // let system handle it
    }
});

listView.setAdapter(adapter);

The "textPart" is mapped to R.id.mTextView and "imagePart" is mapped to R.id.mImageButton

Please see:
http://developer.android.com/reference/android/widget/SimpleAdapter.html
http://developer.android.com/resources/tutorials/views/hello-listview.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜