如何在我的自定义适配器中使用数组?

时间:2012-04-06 16:44:57

标签: java android listview adapter

我有自定义listview适配器来显示两行数据。 我可以将此数据设置为String[] name = {some values};,但我无法从数组创建listview。 以下是活动代码:

public class ListAction extends Activity implements OnClickListener {

    String[] data = { "Sample User Data ", "Sample User Data ",
        "Sample User Data ", "Sample User Data ", "Sample User Data ",
        "Sample User Data ", "Sample User Data ", "Sample User Data ",
        "Sample User Data ", "Sample User Data ", "Sample User Data ",
        "Sample User Data ", "Sample User Data ", "Sample User Data ",
        "Sample User Data ", "Sample User Data " };
    String[] data2 = { "Sample User Data 2", "Sample User Data 2",
            "Sample User Data 2", "Sample User Data 2", "Sample User Data 2",
            "Sample User Data 2", "Sample User Data 2", "Sample User Data 2",
            "Sample User Data 2", "Sample User Data 2", "Sample User Data 2",
            "Sample User Data 2", "Sample User Data 2", "Sample User Data 2",
            "Sample User Data 2", "Sample User Data 2" };

    ListView myList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);

        myList = (ListView) findViewById(R.id.listView);
        myList.setAdapter((ListAdapter) new MyCustomAdapter());
        myList.setCacheColorHint(0);

        myList.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> a, View v, int position,
                    long id) {
                String itemname = new Integer(position).toString();

                Intent intent = new Intent();
                intent.setClass(ListAction.this, ViewClient.class);

                Bundle b = new Bundle();
                b.putString("fileName", itemname);
                intent.putExtras(b);

                startActivity(intent);
            }
        });
    }

    class MyCustomAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return data.length;
        }

        @Override
        public String getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public int getItemViewType(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public int getViewTypeCount() {
            // TODO Auto-generated method stub
            return data.length;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = getLayoutInflater();
            View row;

            row = inflater.inflate(R.layout.list_item, parent, false);
            TextView textLabel = (TextView) row.findViewById(R.id.text);
            textLabel.setText(data[position]);

            TextView textLabel2 = (TextView) row.findViewById(R.id.text2);
            textLabel2.setText(data2[position]);

            return (row);
        }
    }
}

我想用数组中的数据填充listview,例如: String[] data = getResources().getStringArray(R.array.categories);但我只在此行获得NullPointer。 请帮忙。


日志:

04-06 23:35:00.110: E/AndroidRuntime(11420): FATAL EXCEPTION: main
04-06 23:35:00.110: E/AndroidRuntime(11420): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example/com.example.ListAction}: java.lang.NullPointerException
04-06 23:35:00.110: E/AndroidRuntime(11420):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
04-06 23:35:00.110: E/AndroidRuntime(11420):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
04-06 23:35:00.110: E/AndroidRuntime(11420):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-06 23:35:00.110: E/AndroidRuntime(11420):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
04-06 23:35:00.110: E/AndroidRuntime(11420):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 23:35:00.110: E/AndroidRuntime(11420):    at android.os.Looper.loop(Looper.java:130)
04-06 23:35:00.110: E/AndroidRuntime(11420):    at android.app.ActivityThread.main(ActivityThread.java:3687)
04-06 23:35:00.110: E/AndroidRuntime(11420):    at java.lang.reflect.Method.invokeNative(Native Method)
04-06 23:35:00.110: E/AndroidRuntime(11420):    at java.lang.reflect.Method.invoke(Method.java:507)
04-06 23:35:00.110: E/AndroidRuntime(11420):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-06 23:35:00.110: E/AndroidRuntime(11420):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-06 23:35:00.110: E/AndroidRuntime(11420):    at dalvik.system.NativeStart.main(Native Method)
04-06 23:35:00.110: E/AndroidRuntime(11420): Caused by: java.lang.NullPointerException
04-06 23:35:00.110: E/AndroidRuntime(11420):    at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
04-06 23:35:00.110: E/AndroidRuntime(11420):    at com.example.ListAction.<init>(ListAction.java:25)
04-06 23:35:00.110: E/AndroidRuntime(11420):    at java.lang.Class.newInstanceImpl(Native Method)
04-06 23:35:00.110: E/AndroidRuntime(11420):    at java.lang.Class.newInstance(Class.java:1409)
04-06 23:35:00.110: E/AndroidRuntime(11420):    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
04-06 23:35:00.110: E/AndroidRuntime(11420):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
04-06 23:35:00.110: E/AndroidRuntime(11420):    ... 11 more

第25行是String[] data = getResources().getStringArray(R.array.categories);

4 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

确保在String[] data = getResources().getStringArray(R.array.categories);之后onCreate()位于super.onCreate()。在调用其方法之前,必须先实例化上下文。

答案 2 :(得分:0)

您没有将数组传递给适配器。使用构造函数传递它或使用ArrayAdapter。试试这个教程。

http://www.shubhayu.com/android/listview-with-arrayadapter-and-customized-items

答案 3 :(得分:0)

您需要将数组传递到自定义适配器中,否则它将无法确定正确的大小和位置是否无效