Android Custom Arrayadapter With Custom Object
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,
Solution 1:
Here is working adapter code
publicclassRowAdapterextendsArrayAdapter<Row> {
privatefinal Activity _context;
privatefinal ArrayList<Row> rows;
publicclassViewHolder
{
EditText RowNo;
EditText RowText;
}
publicRowAdapter(Activity context, ArrayList<Row> rows)
{
super(context,R.layout.row_layout, R.id.row_id ,rows);
this._context = context;
this.rows = rows;
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent){
ViewHolderholder=null;
if(convertView == null)
{
LayoutInflaterinflater= _context.getLayoutInflater();
convertView = inflater.inflate(R.layout.row_layout,parent,false);
holder = newViewHolder();
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);
Solution 2:
Change with below code:-
RowAdapter.java
publicclassRowAdapterextendsBaseAdapter {
privatefinal Context _context;
privatefinal ArrayList<Row> rows;
publicRowAdapter(Context context, ArrayList<Row> rows)
{
this._context = context;
this.rows = rows;
}
@OverridepublicintgetCount() {
return rows.size();
}
@Overridepublic Object getItem(int position) {
return rows;
}
@OverridepubliclonggetItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflaterinflater= (LayoutInflater) _context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView=inflater.inflate(R.layout.row_layout, null,true);
EditTextRowNo= (EditText) rowView.findViewById(R.id.row_no);
EditTextRowText= (EditText) rowView.findViewById(R.id.row_text);
RowNo.setText(rows.get(position).RowNo);
RowText.setText(rows.get(position).RowText);
return rowView;
}
}
Solution 3:
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
Solution 4:
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);
Post a Comment for "Android Custom Arrayadapter With Custom Object"