Java Android自定义ListView适配器不支持';t填充
长期堆栈用户,第一次海报。我已经学习了几个自定义的ListView适配器教程,但未能使其正常工作(以前我已经成功创建了这些适配器)。我花了太多的时间试图让这个工作!!!有人能看看我犯的愚蠢的错误吗 这在MainActivity.java onCreate()中调用列表Java Android自定义ListView适配器不支持';t填充,java,android,xml,android-listview,Java,Android,Xml,Android Listview,长期堆栈用户,第一次海报。我已经学习了几个自定义的ListView适配器教程,但未能使其正常工作(以前我已经成功创建了这些适配器)。我花了太多的时间试图让这个工作!!!有人能看看我犯的愚蠢的错误吗 这在MainActivity.java onCreate()中调用列表 //Declaration for the list private ArrayList<Consumption> meals = new ArrayList<Consumption>(); private
//Declaration for the list
private ArrayList<Consumption> meals = new ArrayList<Consumption>();
private RAdapter rAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meal);
//This is the first of two listviews on the activity
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(adapter);
//This is the listview I am having problems with
ListView listView2 = (ListView) findViewById(R.id.resultList);
rAdapter = new RAdapter(this,meals);
listView2.setAdapter(rAdapter);
}
//列表的声明
private ArrayList fines=new ArrayList();
私人雷达;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、活动);
//这是活动的两个列表视图中的第一个
adapter=new ArrayAdapter(这个,android.R.layout.simple\u list\u item\u 1,listItems);
ListView ListView=(ListView)findViewById(R.id.listView1);
setAdapter(适配器);
//这是我遇到问题的listview
ListView listView2=(ListView)findViewById(R.id.resultList);
rAdapter=新rAdapter(此为膳食);
listView2.setAdapter(rAdapter);
}
我已经三次检查了膳食是否有数据,确实有
这是RAdapter.java(自定义适配器)代码:
公共类雷达扩展阵列适配器{
静态类持有者{
文本视图rName;
编辑权;
图像按钮删除;
}
私人雅丽餐;
私人最终语境;
公共雷达(上下文){
超级(上下文、右布局、结果行);
this.context=上下文;
}
公共雷达(背景、阵列列表){
超级(上下文、右布局、结果行);
this.context=上下文;
这是一顿饭;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
持有者h=null;
如果(v==null){
LayoutInflater充气器=((活动)上下文)。getLayoutInflater();
v=充气机充气(R.layout.result_行,父项,false);
h=新支架();
h、 rName=(TextView)v.findViewById(R.id.resultName);
h、 quan=(编辑文本)v.findViewById(R.id.resultGrams);
h、 delete=(ImageButton)v.findViewById(R.id.resultDelete);
v、 setTag(h);
}否则{
h=(持有者)v.getTag();
}
消费i=膳食。获取(位置);
h、 rName.setText(i.getShortName());
h、 quan.setText(i.getQuantity());
返回v;
}
}
以下是result_row.xml(要填充的特定行):
下面是activity_dine.xml(其中包含listview“resultList”):
我可以发布日志,但没有要报告的错误消息。我试着把系统打印到处都放,但无法解决问题
任何帮助都将不胜感激 只需更改以下代码并用数据填充arraylist即可
public RAdapter(Context context, ArrayList<Consumption> meals) {
super(context, R.layout.result_row);
this.context = context;
this.meals = meals;
}
public RAdapter(上下文、数组列表){
超级(上下文、右布局、结果行);
this.context=上下文;
这是一顿饭;
}
到
public RAdapter(上下文、数组列表){
超级(上下文、右布局、结果行、膳食);
this.context=上下文;
这是一顿饭;
}
在适配器类中。只需更改以下代码并用数据填充arraylist
public RAdapter(Context context, ArrayList<Consumption> meals) {
super(context, R.layout.result_row);
this.context = context;
this.meals = meals;
}
public RAdapter(上下文、数组列表){
超级(上下文、右布局、结果行);
this.context=上下文;
这是一顿饭;
}
到
public RAdapter(上下文、数组列表){
超级(上下文、右布局、结果行、膳食);
this.context=上下文;
这是一顿饭;
}
在适配器类中。像这样更改适配器
public class RAdapter extends ArrayAdapter<Consumption> {
private Activity activity;
private ArrayList<Consumption> meals;
private LayoutInflater inflater = null;
public RAdapter (Activity act, int resource, ArrayList<Consumption> arrayList) {
super(act, resource, arrayList);
this.activity = act;
this.meals= arrayList;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.result_row, parent, false);
}
//remaining code
return view;
}
}
像这样更换适配器
public class RAdapter extends ArrayAdapter<Consumption> {
private Activity activity;
private ArrayList<Consumption> meals;
private LayoutInflater inflater = null;
public RAdapter (Activity act, int resource, ArrayList<Consumption> arrayList) {
super(act, resource, arrayList);
this.activity = act;
this.meals= arrayList;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.result_row, parent, false);
}
//remaining code
return view;
}
}
你在哪里向arraylist中添加项目?这是在onCreate和onActivityResult中完成的(我有系统打印告诉我它工作正常)你能发布代码吗?下面已经解决了:)你在哪里向arraylist中添加项目?这是在onCreate和onActivityResult中完成的(我有系统打印告诉我它工作正常)你能发布那个代码吗?它已经在下面解决了:)非常感谢!我知道那是件小事!非常感谢!我知道那是件小事!
public RAdapter(Context context, ArrayList<Consumption> meals) {
super(context, R.layout.result_row,meals);
this.context = context;
this.meals = meals;
}
public class RAdapter extends ArrayAdapter<Consumption> {
private Activity activity;
private ArrayList<Consumption> meals;
private LayoutInflater inflater = null;
public RAdapter (Activity act, int resource, ArrayList<Consumption> arrayList) {
super(act, resource, arrayList);
this.activity = act;
this.meals= arrayList;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.result_row, parent, false);
}
//remaining code
return view;
}
}
rAdapter = new RAdapter(MainActivity.this,R.layout.result_row,meals);
listView2.setAdapter(rAdapter);