0

长期堆栈用户,第一次发帖。我遵循了几个自定义 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,但没有要报告的错误消息。我曾尝试将系统打印无处不在,但无法解决问题。

任何帮助将非常感激!!

4

2 回答 2

3

只需更改以下代码并用数据填充您的数组列表。

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;
    }

在您的适配器类中。

于 2013-10-21T07:11:23.717 回答
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);
于 2013-10-21T07:27:15.163 回答