长时间堆叠用户,第一次海报。我已经遵循了几个自定义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(其中包含listview“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,但是没有要报告的错误消息。我试过把系统打印放在任何地方,但无法解决问题。
任何帮助都将非常感谢!!
答案 0 :(得分:3)
只需更改以下代码并使用数据填充您的arraylist。
public RAdapter(Context context, ArrayList<Consumption> meals) {
super(context, R.layout.result_row);
this.context = context;
this.meals = meals;
}
到
public RAdapter(Context context, ArrayList<Consumption> meals) {
super(context, R.layout.result_row,meals);
this.context = context;
this.meals = meals;
}
在适配器类中。
答案 1 :(得分:1)
像这样改变你的适配器
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);