长期堆栈用户,第一次发帖。我遵循了几个自定义 ListView 适配器教程,但无法让这个工作(我之前已经成功创建了这些适配器)。我花了太多时间试图让它工作!!!有人可以看看并希望找到我犯的愚蠢错误吗?
这是在 MainActivity.java onCreate() 调用列表
//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);
}
我已经三次检查了餐点是否有数据,确实如此。
这是 RAdapter.java(自定义适配器)代码:
public class RAdapter extends ArrayAdapter<Consumption>{
static class holder{
TextView rName;
EditText quan;
ImageButton delete;
}
private ArrayList<Consumption> meals;
private final Context context;
public RAdapter(Context context) {
super(context, R.layout.result_row);
this.context = context;
}
public RAdapter(Context context, ArrayList<Consumption> meals) {
super(context, R.layout.result_row);
this.context = context;
this.meals = meals;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
holder h = null;
if (v == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
v = inflater.inflate(R.layout.result_row, parent, false);
h = new holder();
h.rName = (TextView) v.findViewById(R.id.resultName);
h.quan = (EditText) v.findViewById(R.id.resultGrams);
h.delete = (ImageButton) v.findViewById(R.id.resultDelete);
v.setTag(h);
}else{
h = (holder) v.getTag();
}
Consumption i = meals.get(position);
h.rName.setText(i.getShortName());
h.quan.setText(i.getQuantity());
return v;
}
}
这是 result_row.xml(要填充的特定行):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="@+id/resultDelete"
android:layout_marginLeft="10dp"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@android:drawable/ic_notification_clear_all"
android:contentDescription="@string/meal_remove" />
<TextView
android:id="@+id/resultName"
android:layout_marginLeft="10dp"
android:layout_width="150dp"
android:layout_height="40sp"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/resultDelete" />
<EditText
android:id="@+id/resultQuantity"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_width="50dp"
android:layout_height="40sp"
android:inputType="numberDecimal"
android:layout_toRightOf="@+id/resultName"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/resultGrams"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/resultQuantity"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="@string/grams" />
</RelativeLayout>
这是activity_meal.xml(其中包含列表视图“resultList”):
<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:background="@drawable/semislantedbacktransparent"
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=".MealActivity" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/translateButton"
android:layout_width="100dp"
android:layout_height="65dp"
android:background="@drawable/addtorecipe"
android:contentDescription="@string/dummy_button"
android:text="@string/button_add_recipe" />
</LinearLayout>
<SearchView
android:id="@+id/searchView1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/search"
android:focusable="true"
android:iconifiedByDefault="false"
android:onClick="enterTomInput"
android:queryHint="Search for food items"
android:showAsAction="always"
android:textColor="#000000" >
</SearchView>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignLeft="@+id/searchView1"
android:layout_below="@+id/searchView1"
android:cacheColorHint="@color/black_overlay" >
</ListView>
<ListView
android:id="@+id/resultList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/linearLayout1"
android:layout_below="@+id/listView1" />
<ImageButton
android:id="@+id/completeButton"
android:layout_width="100dp"
android:layout_height="65dp"
android:layout_alignRight="@+id/resultList"
android:layout_alignTop="@+id/linearLayout1"
android:background="@drawable/completemeal"
android:contentDescription="@string/dummy_button" />
</RelativeLayout>
我可以发布 logcat,但没有要报告的错误消息。我曾尝试将系统打印无处不在,但无法解决问题。
任何帮助将非常感激!!