2012-02-29 79 views
0

我似乎有問題創建一個適配器的列表視圖顯示項目(標題上的東西)從另一個.xml類。與textview,圖像,複選框和按鈕的列表視圖

有人可以提供一個示例或提示我應該使用哪個適配器 ?在ListView上添加項目xml項目。

反正我沒有生成任何新項目。我只是使用來自另一個XML的已有項目。

這是我希望將我的其他XML轉換爲列表視圖的圖像。

This is the image.

這樣做的理由是因爲在未來,我想添加項目到我的新名單。

回答

0

現在所有的硬性答案沒有一致性。

這是一個更簡單的解決方案或幾乎是它的破解和實際操作過程。

  1. 使用GUI設置開始構建線性佈局,確保它們排列正確。我的方向是垂直的

  2. 一切都完成後,從上到下複製並粘貼記事本上的所有內容,然後刪除。

  3. 現在在相對佈局上創建。寬度&高度=匹配父級。方向垂直。

  4. 現在添加一個帶有wrap_content高度和寬度的滾動視圖。然後粘貼2號任務中的所有內容。

在那裏您可以看到整個視圖的整個滾動視圖。

0

您需要使用CustomAdapter延伸BaseAdapter按下面的代碼和鏈接 http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html

public class List14 extends ListActivity { 

    private static class EfficientAdapter extends BaseAdapter { 
     private LayoutInflater mInflater; 
     private Bitmap mIcon1; 
     private Bitmap mIcon2; 

     public EfficientAdapter(Context context) { 
      // Cache the LayoutInflate to avoid asking for a new one each time. 
      mInflater = LayoutInflater.from(context); 

      // Icons bound to the rows. 
      mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_1); 
      mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_2); 
     } 

     /** 
     * The number of items in the list is determined by the number of speeches 
     * in our array. 
     * 
     * @see android.widget.ListAdapter#getCount() 
     */ 
     public int getCount() { 
      return DATA.length; 
     } 

     /** 
     * Since the data comes from an array, just returning the index is 
     * sufficent to get at the data. If we were using a more complex data 
     * structure, we would return whatever object represents one row in the 
     * list. 
     * 
     * @see android.widget.ListAdapter#getItem(int) 
     */ 
     public Object getItem(int position) { 
      return position; 
     } 

     /** 
     * Use the array index as a unique id. 
     * 
     * @see android.widget.ListAdapter#getItemId(int) 
     */ 
     public long getItemId(int position) { 
      return position; 
     } 

     /** 
     * Make a view to hold each row. 
     * 
     * @see android.widget.ListAdapter#getView(int, android.view.View, 
     *  android.view.ViewGroup) 
     */ 
     public View getView(int position, View convertView, ViewGroup parent) { 
      // A ViewHolder keeps references to children views to avoid unneccessary calls 
      // to findViewById() on each row. 
      ViewHolder holder; 

      // When convertView is not null, we can reuse it directly, there is no need 
      // to reinflate it. We only inflate a new View when the convertView supplied 
      // by ListView is null. 
      if (convertView == null) { 
       convertView = mInflater.inflate(R.layout.list_item_icon_text, null); 

       // Creates a ViewHolder and store references to the two children views 
       // we want to bind data to. 
       holder = new ViewHolder(); 
       holder.text = (TextView) convertView.findViewById(R.id.text); 
       holder.icon = (ImageView) convertView.findViewById(R.id.icon); 

       convertView.setTag(holder); 
      } else { 
       // Get the ViewHolder back to get fast access to the TextView 
       // and the ImageView. 
       holder = (ViewHolder) convertView.getTag(); 
      } 

      // Bind the data efficiently with the holder. 
      holder.text.setText(DATA[position]); 
      holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2); 

      return convertView; 
     } 

     static class ViewHolder { 
      TextView text; 
      ImageView icon; 
     } 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setListAdapter(new EfficientAdapter(this)); 
    } 

    private static final String[] DATA = Cheeses.sCheeseStrings; 
} 
+0

當我看持有者,這意味着我可能在一行中填充2個通用項目的列表。我想要的更像是我的xml的數據到listview之間的轉換。我會發布一張圖片,以便更好地理解我想要做的事情。 – wesdfgfgd 2012-02-29 18:39:14

0

的主要思想是爲您的列表視圖的單個項目創建一個XML佈局。

然後創建一個CustomAdapter以附加到您的ListView。用getView()可以初始化其中的單行數據/事件。你可以找到各種樣品在那裏執行相同..

+0

與我的主要區別只是將其轉換爲列表視圖,因爲我說我有我的其他xml文件上的項目,我想放在一個列表視圖。我不想用剛剛預先存在xml的新信息填充它。大多數其他示例顯示如何填充它們,而不是從一個xml抓取它們並僅顯示它們。 – wesdfgfgd 2012-02-29 18:36:04

+0

xml只是一個空的視圖..你可以抓住它的View/Design/UI(通過Java代碼,通過從現有的xml佈局中膨脹),它的數據:在getView()中,你必須提供一些方法數據進入... – 2012-02-29 18:41:59

+0

沒錯,但通貨膨脹需要人口。在通用項目羣體中,從另一個.xml文件中調用項目以將其轉換爲列表視圖會很困難。我有一個圖片張貼,所以我可以告訴你我想要轉換成一個列表視圖。 – wesdfgfgd 2012-02-29 18:47:18