2013-03-13 39 views
1

我已解析所述網絡響應後獲得的XML響應,並在這裏它是數據綁定到5個不同的文本框以線性佈局

<NewDataSet> 
    <Table> 
    <Restaurant_ID>1705</Restaurant_ID> 
    <restaurant_name>1947</restaurant_name> 
    <address>296, Ram Towers, 100 Feet Ring Road, Banashankari, Bangalore</address> 
    <costoftwo>Approx Rs. 600 for two (without alcohol)</costoftwo> 
    <rating>3.3</rating> 
    <timings>11 AM to 11 PM</timings> 
    <Phone>08026791213</Phone> 
    <Location_ID>34</Location_ID> 
    <Location_name>Banashankari</Location_name> 
    <city_ID>1</city_ID> 
    <city_name>Bangalore</city_name> 
    <Cuisine_ID>8</Cuisine_ID> 
    <Cuisine_name>North Indian</Cuisine_name> 

    </Table> 

和我有一個XML佈局到我需要結合上述響應。我的Xml佈局是

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="right" 
    android:orientation="vertical" 
    android:weightSum="1.0" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:background="@color/headerbgcolor" 
     android:weightSum="1.0" 
     > 

    <Button 
      android:id="@+id/restaurant_back" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.2" 
      android:onClick="finishActivity" 
      android:text="@string/back" /> 

     <TextView 

      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_weight="0.6" 
      android:gravity="center" 
      android:text="@string/restauran_details" /> 

     <Button 
      android:id="@+id/restaurant_home" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.2" 
      android:text="@string/home" /> 
    </LinearLayout> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     > 

     <Button 
      android:id="@+id/Refresh" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="left" 
      android:text="@string/refresh" /> 
    </RelativeLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:weightSum="1"> 

    <TextView 
     android:id="@+id/Restaurant_name" 
     android:layout_width="fill_parent" 
     android:layout_height="30dp" 
     android:text="@string/restaurant_name" 
     /> 

    <TextView 
     android:id="@+id/Restaurant_add" 
     android:layout_width="fill_parent" 
     android:layout_height="40dp" 
     android:text="@string/restaurant_address" 
     /> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:orientation="vertical" 
     android:weightSum="1" > 
    <TextView 
     android:id="@+id/Restaurant_rating" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/restaurant_rating" 
     android:layout_weight="0.1" /> 

    <TextView 
     android:id="@+id/Restaurant_timings" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/restaurant_timings" 
     android:layout_weight="0.1" /> 

    <TextView 
     android:id="@+id/Restaurant_cstfortwo" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/cost_for_two" 
     android:layout_weight="0.1" /> 

    <TextView 
     android:id="@+id/Restaurant_cuisine" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/restaurant_cuisine" 
     android:layout_weight="0.1" /> 

    <TextView 
     android:id="@+id/Restaurant_location" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/restaurant_location" 
     android:layout_weight="0.1" /> 

    <TextView 
     android:id="@+id/Restaurant_city" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/restaurant_city" 
     android:layout_weight="0.1" /> 

</LinearLayout> 

    <TextView 
     android:id="@+id/Restaurant_phone" 
     android:layout_width="fill_parent" 
     android:layout_height="20dp" 
     android:text="@string/restaurant_phone" 
     /> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

     <ImageView 
      android:id="@+id/footerimage" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:background="#808000" 
      android:src="@drawable/btn_bg_cuisine" /> 
    </RelativeLayout> 


</LinearLayout> 

而我不知道它是否好,但我使用customadapter綁定。這是我的習慣。

public class CustomAdapterforRestaurant extends BaseAdapter { 

    private List<Item> mlist = null; 
    private Context mcontext; 
    private LayoutInflater l_Inflater; 

    public CustomAdapterforRestaurant(Context context, List<Item> lstresponse) { 
     mlist = lstresponse; 
     mcontext = context; 
     l_Inflater = (LayoutInflater) mcontext 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 

     ViewHolderforcities holder; 
     if (convertView == null) { 
      convertView = l_Inflater.inflate(R.layout.restaurantdetails, null); 
      holder = new ViewHolderforcities(convertView); 

      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolderforcities) convertView.getTag(); 
     } 

     holder.restaurant_location.setText(mlist.get(position).getLocation_name()); 
     holder.restaurant_name.setText(mlist.get(position).getRestaurant_name()); 
     holder.restaurant_add.setText(mlist.get(position).getAddress()); 
     holder.restaurant_rating.setText(mlist.get(position).getRating()); 
     holder.restaurant_timings.setText(mlist.get(position).getTimings()); 
     holder.restaurant_cstfortwo.setText(mlist.get(position).getCostoftwo()); 
     holder.restaurant_cuisine.setText(mlist.get(position).getCuisine_name()); 
     holder.restaurant_city.setText(mlist.get(position).getCity_name()); 
     holder.restaurant_phone.setText(mlist.get(position).getPhone()); 


     return convertView; 
    } 



    class ViewHolderforcities { 

     TextView restaurant_name,restaurant_add,restaurant_rating,restaurant_timings, 
     restaurant_cstfortwo,restaurant_cuisine,restaurant_location,restaurant_city, 
     restaurant_phone; 

     public ViewHolderforcities(View base) 

     { 
      restaurant_name = (TextView) base.findViewById(R.id.Restaurant_name); 
      restaurant_add = (TextView) base.findViewById(R.id.Restaurant_add); 
      restaurant_rating = (TextView) base.findViewById(R.id.Restaurant_rating); 
      restaurant_timings = (TextView) base.findViewById(R.id.Restaurant_timings); 
      restaurant_cstfortwo = (TextView) base.findViewById(R.id.Restaurant_cstfortwo); 
      restaurant_cuisine = (TextView) base.findViewById(R.id.Restaurant_cuisine); 
      restaurant_location = (TextView) base.findViewById(R.id.Restaurant_location); 
      restaurant_city = (TextView) base.findViewById(R.id.Restaurant_city); 
      restaurant_phone = (TextView) base.findViewById(R.id.Restaurant_phone); 
     } 

    } 

可以請一些人解釋我如何將數據綁定到它。

謝謝 Ramu。

回答

0

您需要使用其中一個可用的XML解析器解析此XML(核心android參考是http://developer.android.com/training/basics/network-ops/xml.html)。一旦得到了xml中的信息,就可以使用textView.setText("text")方法編程式地填寫textView。

+0

我正在使用sax解析器進行解析,並且正在列表項中。但它包含巨大的數據。我將如何綁定我所需要的數據? – Beginner 2013-03-13 09:28:18

+0

如果你正在將XML解析成一個對象(可以說你的案例中的餐廳對象)。那麼應該有任何問題來獲得你想要的數據。 – 2013-03-13 09:31:07

+0

謝謝你@編輯。 – 2013-03-13 09:31:41

相關問題