Android 安卓Listview&;CustomAdapter getView问题
目前正在开发自定义适配器,以在列表行上显示数据,如下所示:Android 安卓Listview&;CustomAdapter getView问题,android,listview,custom-adapter,Android,Listview,Custom Adapter,目前正在开发自定义适配器,以在列表行上显示数据,如下所示: |文本视图| |文本视图| |文本视图| |文本视图| 目前我正在使用本教程作为参考 但是,如上所述,我在customeradapter中遇到了一个问题,特别是在创建充气机时,以及在自定义行xml中标识Textview的ID时 下面是两行引发错误的代码: LayoutInflater inflater = ((Activity)context).getLayoutInflater(); & 给出的错误是:LayoutInflate
|文本视图| |文本视图| |文本视图| |文本视图| 目前我正在使用本教程作为参考 但是,如上所述,我在customeradapter中遇到了一个问题,特别是在创建充气机时,以及在自定义行xml中标识Textview的ID时 下面是两行引发错误的代码:
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
&
给出的错误是:LayoutInflater无法解析为类型&R无法解析为变量
非常感谢你的建议
进口
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.view.LayoutInflater;
适配器的完整实现:
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;
}
}
公共类CustomBaseAdapter扩展了ArrayAdapter{
私人语境;
私人内部布局资源;
私有列表报告=新的ArrayList();
公共CustomBaseAdapter(上下文上下文、内部布局资源ID、列表报告){
超级(上下文、布局资源ID、报告);
this.layoutResourceId=layoutResourceId;
this.context=上下文;
this.report=报告;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行=转换视图;
ViewHolder=null;
if(行==null)
{
LayoutInflater充气器=((活动)上下文)。getLayoutInflater();
行=充气机。充气(layoutResourceId,父级,false);
holder=新的ViewHolder();
holder.food=(TextView)row.findViewById(R.id.foodName);
row.setTag(支架);
}
其他的
{
holder=(ViewHolder)row.getTag();
}
holder.food.setText(report.get(0.getFoodName());
status.setText(report.get(0.getStatus());
//holder.foodRDA.setText(report.get(0.getStatus());
holder.userLvl.setText(report.get(0.getLevel());
返回行;
}
类视图持有者{
TextView食品;
文本视图状态;
TextView用户层;
TextView foodRDA;
文本视图差异;
}
}
XML布局
活动与饮食分析
<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>
<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>
饮食分析
<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>
<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>
根据您提到的错误,您可能需要导入Layoutinor:
import android.view.LayoutInflater;
并为您的应用导入生成的R文件:
import com.example.app.R
确保您也导入了正确的R文件,而不是android.R
此外,如果您使用的是Eclipse或Android Studio(您可能应该使用)等IDE,它会自动为您执行此操作
请注意,如果您的资源文件中有任何错误,则不会为您的项目生成R文件。Hi Rashmi,我已用错误描述更新了原始文件。@MindDrip修复资源文件中的任何错误。并显示您的导入资源文件中没有错误。在原始post中更新的导入。@资源文件中必须有错误。因此,R.java不是像我想的那样生成的,但实际上,没有错误。我已经用xml文件更新了原始文件。