具有自定义对象的Android自定义ArrayAdapter [英] Android custom ArrayAdapter with custom object

查看:91
本文介绍了具有自定义对象的Android自定义ArrayAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android开发的新手.

I am new for Android development.

我正在尝试实现自定义 ArrayAdapter 以便在Android Studio中接受自定义对象.

I was trying to implement a custom ArrayAdapter to accept custom object in Android Studio.

我已经参考了一些教程中的源代码,但是我的屏幕输出不正确,即自定义对象仅填充了 TextView ,该链接与 textViewResourceId > ArrayAdapter ,但自定义对象的属性未正确填写布局中的所有 EditText .

I have referenced the source code from some tutorial, but my screen output was improper that the custom object only fill in the TextView which link with the textViewResourceId of ArrayAdapter , but not the custom object's properties fill in all EditText in the layout appropriately.

我尝试删除 ArrayAdapter 的构造函数中的 textViewResourceId 参数以解决此问题,但Android Studio返回了错误-您必须为TextView

I tried to remove textViewResourceId parameter in the constructor of ArrayAdapter to fix the problem, but Android Studio returned error - You must supply a resource ID for a TextView

我希望自定义对象的属性填写所有 EditText ,并且不返回错误消息,有人可以给我提示吗?

I want the properties of custom object fill in all EditText and no error message be returned, can anyone give me a hint?

这是我的布局:

row_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/row_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"/>

<EditText
    android:id="@+id/row_no"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_margin="1dp"
    android:layout_gravity="center_horizontal"
    android:background="@color/colorWhite"/>

<EditText
    android:id="@+id/row_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_margin="1dp"
    android:layout_gravity="center_horizontal"
    android:background="@color/colorWhite"/>


</LinearLayout>

这是我的自定义对象:

Row.java

public class Row
{
    public int RowNo;
    public String RowText;

    public Row(int no, String text)
    {
        this.RowNo = no;
        this.RowText = text;
    }
}

这是我自定义的ArrayAdapter:

Here is my custom ArrayAdapter:

RowAdapter.java

public class RowAdapter extends ArrayAdapter<Row> {

    private final Activity _context;
    private final ArrayList<Row> rows;

    static class ViewHolder
    {
        EditText RowNo;
        EditText RowText;
    }

    public RowAdapter(Activity context, ArrayList<Row> rows)
    {
        super(context,R.layout.row_layout, R.id.row_id ,rows);
        this._context = context;
        this.rows = rows;
    }

    public View GetView(int position, View convertView, ViewGroup parent){
        ViewHolder holder = null;

        if(convertView == null)
        {
            LayoutInflater inflater = _context.getLayoutInflater();
            convertView = inflater.inflate(R.layout.row_layout,parent,false);

            holder = new ViewHolder();
            holder.RowNo = (EditText)convertView.findViewById(R.id.row_no);
            holder.RowText = (EditText)convertView.findViewById(R.id.row_text);

            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.RowNo.setText(rows.get(position).RowNo);
        holder.RowText.setText(rows.get(position).RowText);

        return convertView;
    }
}

这是我的活动课程:

RowActivity.java

   @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.row_list_page);

        listView = (ListView)findViewById(R.id.list_view);

        ArrayList<Row> rows = new ArrayList<Row>();
        rows.add(new Row(1,"Test"));
        rows.add(new Row(2,"Test"));
        rows.add(new Row(3"Test"));

        RowAdapter adapter = new RowAdapter(this,rows);
        listView.setAdapter(adapter);
}

推荐答案

此处正在使用适配器代码

Here is working adapter code

public class RowAdapter extends ArrayAdapter<Row> {

    private final Activity _context;
    private final ArrayList<Row> rows;

    public class ViewHolder
    {
        EditText RowNo;
        EditText RowText;
    }

    public RowAdapter(Activity context, ArrayList<Row> rows)
    {
        super(context,R.layout.row_layout, R.id.row_id ,rows);
        this._context = context;
        this.rows = rows;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        ViewHolder holder = null;

        if(convertView == null)
        {
            LayoutInflater inflater = _context.getLayoutInflater();
            convertView = inflater.inflate(R.layout.row_layout,parent,false);

            holder = new ViewHolder();
            holder.RowNo = (EditText)convertView.findViewById(R.id.row_no);
            holder.RowText = (EditText)convertView.findViewById(R.id.row_text);

            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.RowNo.setText(""+rows.get(position).RowNo);
        holder.RowText.setText(rows.get(position).RowText);

        return convertView;
    }
}

您在holder.RowNo.setText(rows.get(position).RowNo)处的异常;

Your getting exception at holder.RowNo.setText(rows.get(position).RowNo);

因此将其替换为
holder.RowNo.setText(" + rows.get(position).RowNo);

so replace it with
holder.RowNo.setText(""+rows.get(position).RowNo);

这篇关于具有自定义对象的Android自定义ArrayAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆