2014-11-03 58 views
0

我按照網上的教程製作了一個ListView。它非常棒,完全像我想要的那樣。現在我試圖在另一個活動中創建另一個ListView,但是這次我需要兩個TextView,而不是一個。如何在我的Listview中添加另一個TextView?

這裏是我的適配器:

public class CustomListTwo extends ArrayAdapter<String> { 

private final Activity context; 
private final String[] web; 
// private final String[] hex; <-- was trying to add this one but it gave me an error :(
private final Integer[] imageId; 

public CustomListTwo(Activity context, 
        String[] web, Integer[] imageId) { 
    super(context, R.layout.color_item_layout, web); 
    this.context = context; 
    this.web = web; 
    this.imageId = imageId; 
} 

@Override 
public View getView(int position, View view, ViewGroup parent) { 
    LayoutInflater inflater = context.getLayoutInflater(); 

    View rowView= inflater.inflate(R.layout.color_item_layout, null, true); 
    TextView txtTitle = (TextView) rowView.findViewById(R.id.item_value); 
    ImageView imageView = (ImageView) rowView.findViewById(R.id.item_color); 

    txtTitle.setText(web[position]); 
    imageView.setImageResource(imageId[position]); 

    return rowView; 

    } 
} 

我想補充 「的String []十六進制;」但它給了我一個錯誤,說「變量可能未被初始化」。我的想法是複製關於web變量所寫的所有內容,並將其替換爲十六進制變量並更改值。

這裏是我的活動:

public class ColorRed extends Activity { 

ListView list; 
String[] web; 
Integer[] imageId = { 
     R.color.red_50, 
     R.color.red_100, 
     R.color.red_200, 
     R.color.red_300, 
     R.color.red_400, 
     R.color.red_500, 
     R.color.red_600, 
     R.color.red_700, 
     R.color.red_800, 
     R.color.red_900, 
     R.color.red_A100, 
     R.color.red_A200, 
     R.color.red_A400, 
     R.color.red_A700, 
}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_color_red); 

    web = getResources().getStringArray(R.array.values); 

    CustomListTwo adapter = new 
      CustomListTwo(ColorRed.this, web, imageId); 
    list=(ListView)findViewById(R.id.list_red); 
    list.setAdapter(adapter); 
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 
      switch (position){ 
       case 0: r50(view); 
        break; 
       case 1: r50(view); 
        break; 
       default: 
        r50(view); 
        break; 
      } 

      } 
     }); 
    } 

這裏是我的ListView項:

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="72dp" 
    android:background="?android:attr/selectableItemBackground"> 

    <ImageView 
    android:background="?android:attr/selectableItemBackground" 
    android:layout_width="match_parent" 
    android:layout_height="72dp" 
    android:id="@+id/item_color" 
    android:src="@drawable/myrect" /> 

    <TextView 
    android:id="@+id/item_hex" 
    android:layout_width="wrap_content" 
    android:layout_height="72dp" 
    android:textColor="#de000000" 
    android:textAllCaps="true" 
    android:textSize="18sp" 
    android:text="List Item" 
    android:paddingRight="16dp" 
    android:gravity="center_vertical" 
    android:layout_centerVertical="true" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentEnd="true" /> 

    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="72dp" 
    android:text="New Text" 
    android:textColor="#de000000" 
    android:textSize="18sp" 
    android:id="@+id/item_value" 
    android:layout_centerVertical="true" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="true" 
    android:paddingLeft="16dp" 
    android:gravity="center_vertical"/> 

    </RelativeLayout> 

所以基本上,我的問題是如何添加一個新的TextView,並與來自strings.xml中的數組加載就像我使用當前的TextView一樣?我希望TextView「hex」填充來自我在strings.xml中聲明的數組中的文本。

編輯:我更新適配器:

public class CustomListTwo extends ArrayAdapter<String> { 

    private final Activity context; 
    private final String[] web; 
    private final String[] hex; 
    private final Integer[] imageId; 

    public CustomListTwo(Activity context, String[] web, String[] hex, Integer[] imageId) { 
     super(context, R.layout.color_item_layout, web, hex); 
     this.context = context; 
     this.web = web; 
     this.hex = hex; 
     this.imageId = imageId; 
    } 

    @Override 
    public View getView(int position, View view, ViewGroup parent) { 
     LayoutInflater inflater = context.getLayoutInflater(); 

     View rowView= inflater.inflate(R.layout.color_item_layout, null, true); 
     TextView txtTitle = (TextView) rowView.findViewById(R.id.item_value); 
     ImageView imageView = (ImageView) rowView.findViewById(R.id.item_color); 

     TextView txtHex = (TextView) rowView.findViewById(R.id.item_hex); 
     txtHex.setText(hex[position]); 

     txtTitle.setText(web[position]); 
     imageView.setImageResource(imageId[position]); 

     return rowView; 
    } 
} 

更新活動:

public class ColorRed extends Activity { 

    ListView list; 
    String[] web; 
    String[] hex; 
    Integer[] imageId = { 
      R.color.red_50, 
      R.color.red_100, 
      R.color.red_200, 
      R.color.red_300, 
      R.color.red_400, 
      R.color.red_500, 
      R.color.red_600, 
      R.color.red_700, 
      R.color.red_800, 
      R.color.red_900, 
      R.color.red_A100, 
      R.color.red_A200, 
      R.color.red_A400, 
      R.color.red_A700, 
    }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_color_red); 

     web = getResources().getStringArray(R.array.values); 
     hex = getResources().getStringArray(R.array.hex_red); 

     CustomListTwo adapter = new 
       CustomListTwo(ColorRed.this, web, hex, imageId); 
     list=(ListView)findViewById(R.id.list_red); 
     list.setAdapter(adapter); 
     list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
            int position, long id) { 
       switch (position){ 
        case 0: r50(view); 
         break; 
        case 1: r50(view); 
         break; 
        default: 
         r50(view); 
         break; 
       } 

      } 
     }); 
    } 

現在,我得到這個代碼中的錯誤:

super(context, R.layout.color_item_layout, web, hex); 

「無法解析法」

回答

0

你不能聲明一個最終變量並且不在任何地方初始化它。

在適配器構造函數中傳遞十六進制數組,將其分配給適配器中的十六進制[],並在getView(..)中使用它。

+0

感謝您的幫助!當我將變量「hex」添加到super(上下文,R.layout.color_item_layout,web,hex)時;它給了我一個錯誤,說「在調用超類型構造函數之前不能引用'CustomListTwo.hex'」。 – fredthemugwump 2014-11-03 20:13:20

+0

添加了更新的適配器。 – fredthemugwump 2014-11-03 20:16:10

+1

將'CustomListTwo'構造函數中的'super(context,R.layout.color_item_layout,web,hex);'行更改爲'super(context,R.layout.color_item_layout,web);'。 – 2014-11-03 20:26:30

相關問題