Reputation: 201
Currently working to develop a custom adapater, to display data on a list row as follows:
Currently I am using this tutorial for reference.
http://www.ezzylearning.com/tutorial.aspx?tid=1763429
However, as mentioned, I encounter a problem with the customeradapter, specifically when creating an inflator and in also identifying the ID for Textview in the custom row xml.
Here are the two lines that throw up errors:
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
&
holder.food = (TextView)row.findViewById(R.id.foodName);
Errors given are: LayoutInflater cannot be resolved to a type & R cannot be resolved to a variable.
Many thanks for any suggestions.
Imports
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.view.LayoutInflater;
My full implementation for the adapter:
public class CustomBaseAdapter extends ArrayAdapter<ReportSummary>{
private Context context;
private int layoutResourceId;
private List<ReportSummary> report = new ArrayList<ReportSummary>();
public CustomBaseAdapter(Context context, int layoutResourceId, List<ReportSummary> report) {
super(context, layoutResourceId, report);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.report = report;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.food = (TextView)row.findViewById(R.id.foodName);
row.setTag(holder);
}
else
{
holder = (ViewHolder)row.getTag();
}
holder.food.setText(report.get(0).getFoodName());
holder.status.setText(report.get(0).getStatus());
//holder.foodRDA.setText(report.get(0).getStatus());
holder.userLvl.setText(report.get(0).getLevel());
return row;
}
class ViewHolder {
TextView food;
TextView status;
TextView userLvl;
TextView foodRDA;
TextView difference;
}
}
XML layouts
activity_diet_analysis
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".DietAnalysisActivity" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
row_diet_analysis
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:id="@+id/foodName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:text="TTTT"
/>
<TextView android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/foodName"
android:text="TJJDFDFDF"
/>
<TextView android:id="@+id/foodRDA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/status"
android:text="TTTT"
/>
<TextView android:id="@+id/userLvl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/foodRDA"
android:text="TTTT"
/>
<TextView android:id="@+id/difference"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/userLvl"
android:text="TTTT"
/>
</RelativeLayout>
Upvotes: 0
Views: 1831
Reputation: 3800
Based on the errors you mentioned, you probably need to import LayoutInflator:
import android.view.LayoutInflater;
And import the generated R file for your app:
import com.example.app.R
Be sure you import the correct R file too, not android.R
.
Also, if you're using an IDE such as Eclipse or Android Studio (which you probably should be), it will do this for you automatically.
Note that the R file will not be generated for your project if you have any errors in your resources files.
Upvotes: 1