2011-07-17 3 views
0

SAX 파서를 사용하여 웹에서 XML을 파싱했습니다. 이제 구문 분석 된 데이터를 표시하는 데 문제가 있습니다. ImageButton과 텍스트 뷰를 나란히 표시하려고합니다. 그것을 위해 Imagebuttons 및 textview (XML을 사용하지 않음) 목록을 사용해야하므로 코드를 사용하여 상대 레이아웃을 사용했습니다.android에서 xml을 파싱 한 후 동적 내용에 대한 Listview를 만드는 방법

 // 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); 

나는 ..이 ::
하여 ImageButton의 텍스트 뷰
하여 ImageButton의 텍스트 뷰와 같은 뭔가를 보여 주어야 N (n은 없음에 따라 달라집니다. 분석 한 후 XML의 항목)

에서 검색 후 하루 동안 웹이 일을 혼란스러워. 일부 사용
listview.setAdapter (새 ListAdapter (Splash.this, R.id.list_view, mListItem)); - 어디 mListItem ArrayList 문자열이지만, 나는이 이미지 버튼과 텍스트 뷰가있는 relativelayout을 가지고 있으므로 이것을 사용할 수 없다.

표보기 또는 목록보기로 이동해야하며 몇 가지 샘플 코드도 제공해야한다. Pls가 제안합니다.
미리 감사드립니다. Pavan

답변

0

xml 레이아웃을 사용하면 더 쉽게 사용할 수 있습니다.

리스트 뷰에 단일 항목을 나타냅니다 list_item.xml이 레이아웃 res/layout에서 만들기

<?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> 

각지도리스트 뷰에서 단일 항목을 나타내는지도 목록을 만듭니다.

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 ... 

이어서 R.id.mTextView에 매핑되고 "imagePart는" R.id.mImageButton

매핑되는 "textPart"
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); 

SimpleAdapter

를 사용 list_item.xml에 데이터 바인딩

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

관련 문제