0

I am new for Android development.

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

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.

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

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

Here is my layout:

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>

Here is my custom object:

Row.java

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

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

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

Here is my Activity class:

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);
}
3
  • you can use BaseAdapter. Commented Apr 26, 2016 at 11:03
  • 1
    I think you wrote these yourself instead of implementing with the help of IDE. Otherwise there will be override annotation as well as getView method not GetView. Neither it's related to xamarin. Commented Apr 26, 2016 at 11:08
  • Shree Krisbna: Yes, you are right. Next time I will use intelligence sence to avoid this kind of mistakes. Thank you. Commented Apr 26, 2016 at 12:24

4 Answers 4

1

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

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

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

Sign up to request clarification or add additional context in comments.

Comments

1

Change with below code:-

RowAdapter.java

 public class RowAdapter 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() {
              return rows.size();
          }
    
          @Override
          public Object getItem(int position) {
              return rows;
          }
    
          @Override
          public long getItemId(int position) {
              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;
        }
    }

Comments

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

This method is called to get view object. In your code there is GetView, not getView

Comments

0

change your

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

with

@Override

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

and

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

with

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.