Android自定义ArrayAdapter与自定义对象

时间:2016-04-26 10:59:51

标签: java android android-arrayadapter

我是Android开发的新手。

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

我已经从一些教程中引用了源代码,但我的屏幕输出不正确,自定义对象只填充与TextView textViewResourceId链接的ArrayAdapter,但不是自定义对象的属性会相应地填充布局中的所有EditText

我尝试删除textViewResourceId构造函数中的ArrayAdapter参数来解决问题,但Android Studio返回错误 - 您必须为TextView提供资源ID < / p>

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

这是我的布局:

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:

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

这是我的Activity类:

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

4 个答案:

答案 0 :(得分:1)

使用以下代码进行更改: -

<强> RowAdapter.java

 public class RowAdapter extends extends BaseAdapter {

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

        public RowAdapter(Context context, ArrayList<Row> rows)
        {
            this._context = context;
            this.rows = rows;
        }

          @Override
          public int getCount() {
              // TODO Auto-generated method stub
              return rows.size();
          }

          @Override
          public Object getItem(int position) {
              // TODO Auto-generated method stub
              return rows;
          }

          @Override
          public long getItemId(int position) {
              // TODO Auto-generated method stub
              return position;
          }

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

               LayoutInflater inflater = (LayoutInflater) _context
                  .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

               View rowView=inflater.inflate(R.layout.row_layout, null,true);

               EditText RowNo = (EditText) rowView.findViewById(R.id.row_no);
               EditText RowText = (EditText) rowView.findViewById(R.id.row_text);

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

            return rowView;
        }
    }

答案 1 :(得分:1)

这是工作适配器代码

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

获得例外

所以用
替换它 holder.RowNo.setText(&#34;&#34 + rows.get(位置).RowNo);

答案 2 :(得分:0)

public View getView (int position, View convertView, ViewGroup parent)

调用此方法以获取视图对象。 在您的代码中有GetView,而不是getView

答案 3 :(得分:0)

改变你的

public View GetView(int position, View convertView, ViewGroup parent)

@Override

public View getView(int position,View convertView,ViewGroup parent)

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

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