This question already has an answer here:
I am using a SQLite database to populate a ListView.
I am getting the error - "ArrayAdapter requires the resource ID to be a TextView" on execution of the code.
In my list_row.xml class I have an image, a ProgressBar and 2 TextViews.
I have read through other solutions for the same problems, but nothing seems to work for me (it's possible I might be interpreting them wrong).
My list_row.xml file is:
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@drawable/bg_image">
<ImageView
xmlns:android="http://ift.tt/nIICcg"
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/ingImageid"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ingNameid"
android:layout_toRightOf="@+id/ingImageid"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:textSize="13sp"
android:textStyle="bold"
android:textColor="#9920e4ee"/>
<ProgressBar
android:layout_width="110dp"
android:layout_height="20dp"
android:id="@+id/ingProgressBarid"
android:max="210"
android:layout_toRightOf="@+id/ingImageid"
android:layout_below="@+id/ingNameid"
android:layout_marginTop="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ingQuantid"
android:textColor="#9920e4ee"
android:textSize="13sp"
android:layout_toRightOf="@+id/ingProgressBarid"
android:layout_below="@+id/ingNameid"
android:layout_marginTop="8dp"
android:layout_marginLeft="5dp"/>
</RelativeLayout>
My Adapter code is:
public class CocktailIngredientAdapter extends ArrayAdapter<MyIngredient>{
Context context;
List<MyIngredient> ingridients;
static class ViewHolder {
private View row;
private TextView tvIngriendint_name;
private TextView tvIngriendint_qty;
private ProgressBar ingridientBar;
private ImageView ingredientImage;
public ViewHolder(TextView name, TextView tvIngriendint_qty,ProgressBar ingridientBar,ImageView ingredientImage) {
this.tvIngriendint_name = name;
this.ingridientBar = ingridientBar;
this.tvIngriendint_qty = tvIngriendint_qty;
this.ingredientImage = ingredientImage;
}
}
public CocktailIngredientAdapter(Context context, int resource,List<MyIngredient> objects) {
super(context, resource, objects);
this.context = context;
this.ingridients = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
super.getView(position, convertView, parent);
ViewHolder holder = null;
TextView name, tvIngriendint_qty;
ProgressBar ingridientBar;
ImageView ingredientImage;
if (null == convertView) {
LayoutInflater vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.cocktail_ingredient_row, null);
name = (TextView) convertView.findViewById(R.id.ingNameid);
tvIngriendint_qty = (TextView) convertView.findViewById(R.id.ingQuantid);
ingridientBar = (ProgressBar) convertView.findViewById(R.id.ingProgressBarid);
ingredientImage = (ImageView) convertView.findViewById(R.id.ingImageid);
convertView.setTag(new ViewHolder(name,tvIngriendint_qty, ingridientBar,ingredientImage));
}
holder = (ViewHolder) convertView.getTag();
MyIngredient ingridient = ingridients.get(position);
String ingridintName = (String) ingridient.getIngName();
String ingridintImage = (String) ingridient.getIngImage();
//setting up the ingredient image resource
Context context = holder.ingredientImage.getContext();
int id = context.getResources().getIdentifier(ingridintImage, "drawable", context.getPackageName());
holder.ingredientImage.setImageResource(id);
float quantity = Float.parseFloat(ingridient.getIngQuant());
float qty = quantity;
holder.ingridientBar.setMax(270);
holder.tvIngriendint_name.setText(ingridintName);
int progress = (int) (qty); // claculatePercentage(qty);
holder.tvIngriendint_qty.setText(Integer.toString(progress)+"ml");
if (android.os.Build.VERSION.SDK_INT >= 11 && progress > 0) {
// will update the "progress" propriety of seekbar until it reaches
// progress
ObjectAnimator animation = ObjectAnimator.ofInt(
holder.ingridientBar, "progress", progress);
animation.setDuration(500); // 0.5 second
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
} else {
holder.ingridientBar.setProgress(progress);
}
return convertView;
}
What am I doing wrong here?
Can anyone please share some knowledge?
I am very new to coding for Android
Aucun commentaire:
Enregistrer un commentaire