当我想加载listview

时间:2015-07-05 20:46:15

标签: java android android-layout listview android-listview

:定位

我正在尝试在Android中创建购物清单。这有一个活动类(MainActivity.java)和一个布局(activity_main.xml) 该程序具有用于添加项目的编辑文本和用于购物清单项目的列表视图。

我使用genymotion运行mt程序。当我运行这个程序时,它完全崩溃了。

结构

我的布局activity_main.xml类:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <EditText
        android:id="@+id/newItemText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/addItemHint"
        android:contentDescription="@string/addItemContentDescription"
        />

    <ListView
        android:id="@+id/shoppingListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>

</LinearLayout>

这是我的MainActivity类中的OnCreate方法

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Inflating the view
        setContentView(R.layout.activity_main);

        //References from the UI widgets
        ListView shoppingListView = (ListView) findViewById(R.id.shoppingListView);
        final EditText newShoppingItemText = (EditText) findViewById(R.id.newItemText);

        //the list of shopping items
        final ArrayList<String> shoppingListItems = new ArrayList<String>();
        shoppingListItems.add("Eggs");
        shoppingListItems.add("Bacon");
        //the array adapter, which binds the shopping list items array to the listview
        final ArrayAdapter<String> aa;
        aa = new ArrayAdapter<String>(MainActivity.this, R.layout.activity_main,shoppingListItems);

        //Bind the adapter to the listview
        shoppingListView.setAdapter(aa);

        newShoppingItemText.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if(event.getAction() == KeyEvent.ACTION_DOWN)
                    if( (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) || (keyCode == KeyEvent.KEYCODE_ENTER) ) {
                        shoppingListItems.add(0, newShoppingItemText.getText().toString());
                        aa.notifyDataSetChanged();
                        newShoppingItemText.setText("");
                        return true;
                    }
                        return false;
            }
        });
    }

问题

当我在Genymotion中运行此代码时,我在logcat中得到以下错误输出:

  

处理:com.example.jonas.shoppinglist,PID:8354       java.lang.IllegalStateException:ArrayAdapter要求资源ID为TextView               在android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)               在android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)               在android.widget.AbsListView.obtainView(AbsListView.java:2255)               在android.widget.ListView.measureHeightOfChildren(ListView.java:1263)               在android.widget.ListView.onMeasure(ListView.java:1175)               在android.view.View.measure(View.java:16497)

2 个答案:

答案 0 :(得分:1)

代码的这一行出现问题

aa = new ArrayAdapter<String>(MainActivity.this, R.layout.activity_main,shoppingListItems);

您无法将yout活动的布局作为listview中单个项目的布局。试试这个:

aa = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,shoppingListItems);

答案 1 :(得分:0)

此行不正确:

aa = new ArrayAdapter<String>(MainActivity.this, R.layout.activity_main,shoppingListItems);

您正在调用的构造函数是:

http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter%28android.content.Context,%20int,%20java.util.List%3CT%3E%29

您正在传递活动布局,您应该在其中传递列表中特定项目的布局。