android - 元素列表由文本+图像+单选按钮组成

时间:2014-06-05 21:01:48

标签: java android list

我使用simple_list_item_1在我的应用中显示列表。我想更改列表,以便列表中的每个元素除了从Const.practisesList获取的文本外还有一个图像+单选按钮。 这是现在的代码:

ListAdapter listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Const.practisesList);
    final ListView tests_list = (ListView)findViewById(R.id.tests_list);
    tests_list.setAdapter(listAdapter);

    tests_list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            switch(position){

            case Const.TEST_TYPE1_MATH:
                p = practiseCreator(1)
                break;


            case Const.TEST_TYPE4_INSTRUCTIONS:
                p = practiseCreator(4)
                break;
            }


        }
    });

这样做的方法是什么?

感谢

1 个答案:

答案 0 :(得分:0)

为您的行创建自定义布局。实现从ArrayAdapter扩展的自定义适配器。

private class SchemeAdapter extends ArrayAdapter<String> {

    private int layout;

    public SchemeAdapter(final Context context, final List<String> objects) {
        //row layout id, content view id
        super(context, R.layout.list_row_practice, R.id.list_row_practice_name,
                objects);

        layout = R.layout.list_row_practice;
    }

    @Override
    public View getView(final int position, final View convertView, final ViewGroup parent) {
        final View row =
                (convertView == null) ? getLayoutInflater().inflate(layout, parent, false) :
                        convertView;

        if (row != null) {
            final TextView name = (TextView) row.findViewById(R.id.list_row_practice_name);
            //set here your text, image view src and radio button value.
        }
        return row;
    }

}