简体   繁体   中英

Custom adapter for listview not working

I'm having trouble creating an adapter to my list filled with an array json I get from my web service query.

EDIT

public class CustomListViewAdapter extends ArrayAdapter<RowItem> {

Context context;
private ArrayList<RowItem> originalList;
private ArrayList<RowItem> items;
private CountryFilter filter;

public CustomListViewAdapter(Context context, int resourceId,
        List<RowItem> items) {
    super(context, resourceId, items);
    this.context = context;
    this.items = new ArrayList<RowItem>();
    this.items.addAll(items);
    this.originalList = new ArrayList<RowItem>();
    this.originalList.addAll(items);
}

/*private view holder class*/
private class ViewHolder {
    ImageView imageView;
    TextView txtNombre;
    TextView txtTicket;
    TextView txtAsiento;
    TextView txtNumero;
    TableLayout tblLayout;
    TableRow row;

}

@Override
public Filter getFilter() {
 if (filter == null){
  filter  = new CountryFilter();
 }
 return filter;
}

@Override
public int getCount() {
    return items.size();
}

@Override
public RowItem getItem(int position) {
    return items.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

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

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.lista, null);
        holder = new ViewHolder();
        holder.row = (TableRow)convertView.findViewById(R.id.tableRow2);
        holder.txtNombre = (TextView)holder.row.getChildAt(0);
        holder.txtTicket = (TextView)holder.row.getChildAt(1);
    } else
        holder = (ViewHolder) convertView.getTag();

    holder.txtNombre.setText(rowItem.getNombre());
    //holder.imageView.setImageResource(rowItem.getImageId());
    holder.txtTicket.setText(rowItem.getTicket());

    return convertView;
}

private class CountryFilter extends Filter
{

 @Override
 protected FilterResults performFiltering(CharSequence constraint) {

  constraint = constraint.toString().toLowerCase();
  FilterResults result = new FilterResults();
  if(constraint != null && constraint.toString().length() > 0)
  {
  ArrayList<RowItem> filteredItems = new ArrayList<RowItem>();

  for(int i = 0, l = originalList.size(); i < l; i++)
  {
   RowItem asistente = originalList.get(i);
   if(asistente.toString().replace('á', 'a').toLowerCase().contains(constraint) || asistente.toString().replace('é', 'e').toLowerCase().contains(constraint) || asistente.toString().replace('í', 'i').toLowerCase().contains(constraint) || asistente.toString().replace('ó', 'o').toLowerCase().contains(constraint) || asistente.toString().replace('ú', 'u').toLowerCase().contains(constraint))
        filteredItems.add(asistente);
  }
  result.count = filteredItems.size();
  result.values = filteredItems;
  }
  else
  {
   synchronized(this)
   {
    result.values = originalList;
    result.count = originalList.size();
   }
  }
  return result;
 }

 @SuppressWarnings("unchecked")
 @Override
 protected void publishResults(CharSequence constraint, FilterResults results) {
     items = (ArrayList<RowItem>)results.values;
      notifyDataSetChanged();
      clear();
      for(int i = 0, l = items.size(); i < l; i++){
          System.out.println("RESULTADO:"+items.get(i));
          add(items.get(i));
          notifyDataSetInvalidated();
      }
    }
   }
 }

My problem is in the getView function. When charged my gives me the error:

08-06 11:48:42.505: E/AndroidRuntime(10018): FATAL EXCEPTION: main
08-06 11:48:42.505: E/AndroidRuntime(10018): java.lang.NoSuchFieldError: com.google.zxing.client.android.R$id.tabla_cuerpo
08-06 11:48:42.505: E/AndroidRuntime(10018):    at com.google.zxing.client.android.CustomListViewAdapter.getView(CustomListViewAdapter.java:70)

my layout :

<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="@+id/tabla_cuerpo" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="match_parent">
    <TableRow android:id="@+id/tableRow1" android:layout_width="match_parent" android:layout_height="wrap_content">
        <TextView android:gravity="center_horizontal"  android:id="@+id/textView0" android:layout_width="wrap_content" android:layout_height="wrap_content"  android:text="Validaciones"/>
    </TableRow>
    <TableRow android:id="@+id/tableRow2" android:layout_width="match_parent" android:layout_height="wrap_content">
        <TextView  android:layout_margin="1dip" android:id="@+id/txtNombre" android:layout_weight="0.3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Celda1"/>
        <TextView  android:layout_margin="1dip" android:id="@+id/txtTicket" android:layout_weight="0.3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Celda2"/>
        <TextView  android:layout_margin="1dip" android:id="@+id/txtAsiento" android:layout_weight="0.3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Celda3"/>
    </TableRow>
</TableLayout>

The problem arises when I loaded the view and can not upload data. The strange thing is that I use the same code in another project with the same format as JSON and working properly. I have seen several custom code in the getView event adapter and exactly to mine but I think my mistake are in my elements and can not find how to fix it.

@OverridegetView方法是搞乱。

Please override the getCount and getItemId as well.

updating answer with code:

@Override
public int getCount() {
    return itemList.size();
}

@Override
public Object getItem(int position) {
    return itemList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

and please don't use getChildAt(VIEW ID) , you have to use findViewByID

      holder.txtNombre = (TextView)holder.row.getChildAt(R.id.txtNombre);
      holder.txtTicket = (TextView)holder.row.getChildAt(R.id.txtTicket);

Thx for the hint to @PPartisan

By the way, you can do the same with less IDs:

<?xml version="1.0" encoding="utf-8"?>

    <TextView android:gravity="center_horizontal"  android:id="@+id/textView0" android:layout_width="wrap_content" android:layout_height="wrap_content"  android:text="Validaciones"/>

</TableRow>
<TableRow  android:layout_width="match_parent" android:layout_height="wrap_content">
    <TextView  android:layout_margin="1dip" android:id="@+id/txtNombre" android:layout_weight="0.3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Celda1"/>

    <TextView  android:layout_margin="1dip" android:id="@+id/txtTicket" android:layout_weight="0.3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Celda2"/>

    <TextView  android:layout_margin="1dip" android:id="@+id/txtAsiento" android:layout_weight="0.3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Celda3"/>

</TableRow>

once you inflated the layout to your convertView variable, you can access all fields view convertview.findViewByID(ID)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM