0

我正在尝试创建一个基本的列表视图应用程序。我让它在默认的系统阵列适配器上运行良好。然后我尝试修改它以允许我使用自定义适配器。不幸的是我遇到了一个问题。

当我运行应用程序时,我没有遇到运行时错误,而是出现黑屏,应该显示列表,这让我认为它可能正在显示列表,但由于某种原因适配器没有找到数据....

无论如何,下面是我的列表活动:

    package com.example.waitronproto2;

    import java.util.ArrayList;
    import java.util.List;

    import android.app.ListActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.Toast;

    public class AppetizersActivity extends ListActivity{

            //Instance Variables
            //Array to hold menu appetizers - In future this will be pulled from web server
            private List<MenuItem> appetizers;
            private MenuItemArrayAdapter appetizersAdapter;


            @Override
            public void onCreate(Bundle savedInstanceState){
                super.onCreate(savedInstanceState);
                //Garbage collection
                System.gc();
                setContentView(R.layout.appetizers_list);

                fillList();

                //Create custom array adapter - links list with source of data, an array.
                appetizersAdapter = new MenuItemArrayAdapter(this, appetizers);


                //Set the newly created adapter as the lists adapter.
                this.setListAdapter(appetizersAdapter); 
            }

            @Override
            protected void onListItemClick(ListView l, View v, int position, long id){
                //Toast for testing
                Toast.makeText(this.getApplicationContext(), l.getItemAtPosition(position) + " Clicked!", Toast.LENGTH_LONG).show();        

                //store clicked item in String variable
                String choice = l.getItemAtPosition(position).toString();

                //add to an array that is being passed from Activity to Activity
            }

            //fill the list with  menu items
            private void fillList(){
                appetizers = new ArrayList<MenuItem>();
                appetizers.add(new MenuItem("Appetizer 1", "Appetizer", 8.95));
                appetizers.add(new MenuItem("Appetizer 2", "Appetizer", 9.95));
                appetizers.add(new MenuItem("Appetizer 3", "Appetizer", 12.95));
                appetizers.add(new MenuItem("Appetizer 4", "Appetizer", 4.95));
                appetizers.add(new MenuItem("Appetizer 5", "Appetizer", 5.95));
                appetizers.add(new MenuItem("Appetizer 6", "Appetizer", 7.95));
                appetizers.add(new MenuItem("Appetizer 7", "Appetizer", 5.95));
                appetizers.add(new MenuItem("Appetizer 8", "Appetizer", 12.95));
                appetizers.add(new MenuItem("Appetizer 9", "Appetizer", 10.95));


            }
    }

这是我的适配器类:

    package com.example.waitronproto2;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

//Default array adapter for menu items
public class MenuItemArrayAdapter extends ArrayAdapter<MenuItem> {

    private List<MenuItem> menuItems;

    //Constructor
    public MenuItemArrayAdapter(Context context, List<MenuItem> menuItems) {
        super(context, R.layout.menuitem_row);
        this.menuItems = menuItems;
    }

    //get views
    public View getView(final int position, View convertView, ViewGroup parent){
        View v = convertView;


        if(v == null){
            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.menuitem_row, null);
        }

        //assign values to view
        MenuItem item = this.menuItems.get(position);

        TextView nameView = (TextView) v.findViewById(R.id.item_name);
        TextView priceView = (TextView) v.findViewById(R.id.item_price);


        nameView.setText(item.getName());
        priceView.setText(String.valueOf(item.getPrice()));

        return v;

    }

}

有什么想法我可能会出错吗?

非常感谢!

4

1 回答 1

1

通过扩展 ArrayAdapter 但在您的自定义适配器中使用本地列表,您会使事情变得有些混乱。ArrayAdapter 不知道该列表有任何数据,因为 getCount()引用了一个列表。

改用这个超级构造函数:

//Constructor
public MenuItemArrayAdapter(Context context, List<MenuItem> menuItems) {
    super(context, R.layout.menuitem_row, menuItems);
    //  Pass the ArrayAdapter your list   ^^^^^^^^^
}

并且不要保留menuItems自己,否则您的代码会随着时间的推移变得过于复杂。您需要getItem(position)在当前使用的地方使用this.menuItems.get(position).


或者,您可以覆盖getCount()getItem()getItemId()指向您的menuItems列表。但是如果你这样做,你还不如直接扩展 BaseAdapter,因为你已经剥夺了 ArrayAdapter 的好处。

于 2012-12-04T23:56:39.820 回答