为什么我没有从ArrayList获取ListView数据?

时间:2011-09-02 07:29:36

标签: android android-layout android-emulator android-widget android-ndk

这是我从ArrayList获取数据的代码:

 getTables=new Runnable()
    {
        @Override
        public void run() 
        {
            db.open();
            try 
            {  

                tables =  db.showAllTable();    
                 for(int i=0;i<db.showAllTable().size();i++)
                 {
                        System.out.println("=====>"+(i+1));
                        System.out.println("Table :"+tables.get(i).toString());
                 }

                 customAdapter=new ImageAdapter(ListofTests.this, R.layout.list_row,tables);

            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }
            //setListAdapter(customAdapter);
            runOnUiThread(hideProgressBar);
            db.close();
        }
    };

这是我的ImageAdapter类:

class ImageAdapter extends ArrayAdapter<Object> 
{

    private ArrayList<Object> tables;
    //ImageLoader imageLoader;
    Context m_context;
    public ImageAdapter(Context context, int textViewResourceId,ArrayList<Object> items) 
    {
        super(context, textViewResourceId, items);
        m_context=context;
        //this.tables = tables;
    }
    public View getView(final int position, View convertView, ViewGroup parent) 
    {
        View v = convertView;
        ViewHolder holder = null;

        if (v == null) 
        {

            try
            {
            holder=new ViewHolder();
            //imageLoader=new ImageLoader(CustomSearch.this);
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_row, null);
            holder.table_name=(TextView) v.findViewById(R.id.table_name);

            v.setTag(holder);

            }
            catch(Exception e)
            {
                System.out.println("Excption Caught"+e);
            }
        }
        else
            holder=(ViewHolder)v.getTag();

        Object table = tables.get(position);

        if(table!=null)
        {
            holder.table_name.setText("Hello");
        }

        return v;

    }
}

现在我不知道我哪里错了。我无法在列表视图中看到数据列表。 请帮我解决这个问题。 感谢。

错误日志:

09-02 14:14:13.654: ERROR/AndroidRuntime(771): FATAL EXCEPTION: main

09-02 14:14:13.654:ERROR / AndroidRuntime(771):java.lang.NullPointerException 09-02 14:14:13.654:ERROR / AndroidRuntime(771):at com.quiz.spellingquiz.ListofTests $ ImageAdapter.getView(ListofTests.java:143) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.widget.AbsListView.obtainView(AbsListView.java:1315) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.widget.ListView.measureHeightOfChildren(ListView.java:1198) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.widget.ListView.onMeasure(ListView.java:1109) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.view.View.measure(View.java:8171) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.widget.RelativeLayout.measureChild(RelativeLayout.java:563) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:378) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.view.View.measure(View.java:8171) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.widget.LinearLayout.measureVertical(LinearLayout.java:381) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.widget.LinearLayout.onMeasure(LinearLayout.java:304) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.view.View.measure(View.java:8171) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.view.View.measure(View.java:8171) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.widget.LinearLayout.measureVertical(LinearLayout.java:526) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.widget.LinearLayout.onMeasure(LinearLayout.java:304) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.view.View.measure(View.java:8171) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.view.View.measure(View.java:8171) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.view.ViewRoot.performTraversals(ViewRoot.java:801) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.view.ViewRoot.handleMessage(ViewRoot.java:1727) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.os.Handler.dispatchMessage(Handler.java:99) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.os.Looper.loop(Looper.java:123) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):在android.app.ActivityThread.main(ActivityThread.java:4627) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):at java.lang.reflect.Method.invokeNative(Native Method) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):at java.lang.reflect.Method.invoke(Method.java:521) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 09-02 14:14:13.654:ERROR / AndroidRuntime(771):at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

您永远不会在适配器中设置tables

我想你想要这个:

 public ImageAdapter(Context context, int textViewResourceId,ArrayList<Object> items) 
    {
        super(context, textViewResourceId, items);
        m_context=context;
        this.tables = items; // set to items instead of tables (which doesnt exist in the argument list)
    }