2013-01-04 66 views
0

我想爲android微調製作一個自定義樣式,但是有幾個問題。 我在很多例子中已經注意到,它們將數組傳遞給getCustomView方法。我想知道我是否可以傳遞一個列表而不是一個數組。將列表傳遞給自定義視圖?

第二個問題是爲什麼他們聲明變量並在類變量作用域中初始化? 他們已經聲明瞭這樣的數組。

String []data1={"Brainbitz:java","Brainbitz:Android","Brainbitz:Embedded Systems","Brainbitz:PHP"}; 

在類變量作用域中。但是當我嘗試爲列表做我會得到一個錯誤。爲什麼?

三是,

如果我們可以通過列表來getCustomView我該怎麼做呢?這裏是教程的鏈接tutorial

我正在考慮這個源代碼。

public View getCustomView(int position, View convertView, ViewGroup parent) { 

      LayoutInflater inflater=getLayoutInflater(); 
      View row=inflater.inflate(R.layout.spinner, parent, false); 
      TextView label=(TextView)row.findViewById(R.id.textView1); 
      label.setText(list3.get(position)); 

//   TextView sub=(TextView)row.findViewById(R.id.textView2); 
//   sub.setText(data2[position]); 
// 
//   ImageView icon=(ImageView)row.findViewById(R.id.imageView1); 
//   icon.setImageResource(images[position]); 

      return row; 
      } 

在上面的代碼中,我不知道將位置傳遞給list3列表類型引用的語法。

請友好解釋一下。

+0

請先標記機器人,這個Android問題 – Unknown

回答

2

首先,

您正在使用默認ArrayAdapter類..

new ArrayAdapter<String>(); 

它採用String類數據綁定。如果您想用或List製作ArrayAdapter,則必須通過擴展ArrayAdapter<Custom_Class>BaseAdapter來製作自定義適配器

ArrayAdapter類可以處理任何Java對象作爲輸入。它將此輸入的數據映射到佈局中的TextView。

ArrayAdapter使用數據輸入對象的toString()方法來確定應該顯示的字符串。

How to use ArrayAdapter<myClass>Tutorial

更新:

public class MyAdapter extends ArrayAdapter<String> 
{ 
    private List<String> listString = new ArrayList<String>(); 

    public MyAdapter(Context context, int textViewResourceId, 
         List<String> objects) { 
    super(context, textViewResourceId, objects); 
    // TODO Auto-generated constructor stub 
    listString = objects; 
    } 

    public View getCustomView(int position, View convertView, ViewGroup parent) { 

    LayoutInflater inflater=getLayoutInflater(); 
    View row=inflater.inflate(R.layout.spinner, parent, false); 
    TextView label=(TextView)row.findViewById(R.id.textView1); 
    label.setText(listString.get(position)); // How to use listString 
    . 
    . 
    . 
+0

我使用已作爲followes延長本教程的課,'公共類MyAdapter擴展ArrayAdapter ' – newday

+0

然後做一個**構造的那個**定義適配器** **類參數'列表'並在你的自定義適配器類的'getView()'中使用該對象。 – user370305

+1

在使用任何教程之前,請檢查其遵循的是否是標準的android編程規則。 – user370305

0

以下是更正後的解決方案,

public class MyAdapter extends ArrayAdapter<String> 
    { 

     public MyAdapter(Context context, int textViewResourceId,List<String> objects) { 
       super(context, textViewResourceId, objects); 
       // TODO Auto-generated constructor stub 
     } 
      @Override 
     public View getDropDownView(int position, View convertView,ViewGroup parent) { 
      return getCustomView(position, convertView, parent); 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      return getCustomView(position, convertView, parent); 
     } 

     public View getCustomView(int position, View convertView, ViewGroup parent) { 

      LayoutInflater inflater=getLayoutInflater(); 
      View row=inflater.inflate(R.layout.spinner, parent, false); 
      TextView label=(TextView)row.findViewById(R.id.textView1); 
      label.setText(list3.get(position)); 

//   TextView sub=(TextView)row.findViewById(R.id.textView2); 
//   sub.setText(data2[position]); 
// 
//   ImageView icon=(ImageView)row.findViewById(R.id.imageView1); 
//   icon.setImageResource(images[position]); 

      return row; 
      } 

    } 

Basiclly,我做了1級的錯誤,一個是我說好的正確地使用了構造函數。

+0

你的編程代碼還有一件事是錯誤的。因爲你沒有在你的MyAdapter中使用List 對象,那麼在構造函數中使用它是什麼... :-) – user370305

+0

如果你正在使用靜態的'List 對象'類級變量,那麼不需要將它傳遞給' MyAdapter'構造函數。 – user370305