如何在Listview中添加另一个TextView?

时间:2014-11-03 19:57:25

标签: android listview textview adapter

我已经通过网络上的教程制作了ListView。它工作得很好,完全像我想要的那样。现在我正在尝试在另一个活动中创建另一个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 [] hex;”但它给我一个错误,说“变量可能没有被初始化”。我的想法是复制有关web变量的所有内容,并将其替换为hex变量并更改值。

这是我的活动:

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一样?我希望使用我在strings.xml中声明的数组中的文本填充TextView“hex”。

编辑:我更新的适配器:

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

“无法解决方法”

1 个答案:

答案 0 :(得分:0)

您不能声明最终变量,也不能在任何地方初始化它。

在适配器构造函数中传递十六进制数组,将其分配给适配器中的hex []并在getView(..)中使用它。