簡體   English   中英

Android自定義ListView適配器未填充

[英]Android Custom ListView Adapter doesn't populate

長時間堆疊的用戶,首次發布者。 我已經遵循了幾個Custom ListView Adapter教程,但無法使它正常工作(我之前已經成功創建了這些適配器)。 我花了太多時間試圖使它工作!!! 有人可以看看嗎,並希望找到我犯的愚蠢錯誤?

這在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,但是沒有錯誤消息要報告。 我嘗試將System打印件放到任何地方,但無法解決問題。

任何幫助將非常感激!!

只需更改以下代碼,並用數據填充您的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;
    }

在您的適配器類中。

像這樣改變你的適配器

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM