苦苦挣扎于ListView for Android的基本实现

时间:2014-05-11 19:39:26

标签: java android listview android-listview android-arrayadapter

我最近开始使用Android教程,虽然本身不​​是教程,但我已经开始使用this page来尝试实现一个简单的ListView

我在上周刚下载的Android SDK版本中开始使用新的Android应用程序项目,并以我认为符合上述材料的方式修改了两个文件,{{{ 3}}

然而,在我的HTC One上启动我的应用程序时,它会立即失败并显示消息"Unfortunately, [my app] has stopped."我发现了一行代码,我可以注释掉它以防止崩溃,但这样做我正在避免初始化ListView,因此结果是空视图。

如上所述,我只更改了新Android应用程序项目默认情况下两个文件的内容,但这里它们已完整:

fragment_main.xml

<LinearLayout 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: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="com.example.testproj.MainActivity$PlaceholderFragment">

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

</LinearLayout>

MainActivity.java

package com.example.recipeorganiserforandroid;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends ActionBarActivity {

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

        String[] myArray = new String[] {"Tomato", "Potato", "Carrot", "Mushroom", "Beef", "Cheese", "Lettuce", "Bread", "Milk", "Spinach", "Chicken", "Rice"};

        setContentView(R.layout.activity_main);

        ListView myList = (ListView) findViewById(R.id.main_list);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myArray);

        myList.setAdapter(adapter);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

}

我注意到我上面链接的第二个示例页面使用了ArrayAdapter的不同构造函数,但尝试这个并没有给我更多的成功。如果我注释掉myList.setAdapter(adapter);行,那么我的应用程序就会成功运行,但很明显,我的ListView视图中没有提供任何内容。

我已经在网上寻找过其他的例子,但我发现的所有内容都相当复杂,我不认为在这个组合中投入更多的想法会让我更清楚。任何人都可以在这里暗示我可能做错了吗?

1 个答案:

答案 0 :(得分:0)

当您使用Fragment时,您应该初始化数据并在Frament类中设置适配器。出于这个原因,您的应用程序崩溃,当您从活动中致电myList.setAdapter(adapter)时,myList可能是Null,因为您正尝试从活动中检索它,ListViewFragment

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        String[] myArray =
                new String[] {
                        "Tomato", "Potato", "Carrot", "Mushroom", "Beef", "Cheese", "Lettuce",
                        "Bread", "Milk", "Spinach", "Chicken", "Rice"
                };

        ListView myList = (ListView) rootView.findViewById(R.id.list_main);

        ArrayAdapter<String> adapter =
                new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,
                                         myArray);

        myList.setAdapter(adapter);

        return rootView;
    }
}