Listview Array Adapter在自定义布局中返回空指针异常

时间:2014-05-29 12:58:54

标签: android listview listadapter

我的问题是,我无法在ArrayList中显示ListView数据。我需要在自定义布局文件中心部分显示listview。我使用自定义适配器来显示listview。我正在从本地数据库中检索ListArray<String>数据。 Listview适配器未收到我的数据库ArrayList<String>项。我为ListAdaptor创建了单独的类,并为ListAdaptor创建了一个构造函数。

我可以通过扩展ListActivity来展示,但我使用CustomAdaptor。以下是我的代码,请告诉我我在哪里做错了代码

布局文件(Dashboard_Notify.xml)

这是我的自定义布局xml文件,在此页面中心我想显示listview数据

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/bg"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="51dp"
        android:background="@drawable/dash_nav_bg_widout_brk_line"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="0dp"
            android:layout_marginTop="0dp"
            android:src="@drawable/dash_nav_bg1_not_select" />
  <ImageView
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="0dp"
            android:layout_marginTop="0dp"
            android:src="@drawable/dash_nav_sep1" />

        <ImageView
            android:id="@+id/imageButton1"
            android:layout_marginLeft="5dp"
            android:layout_width="90dp"
            android:layout_height="50dp"
            android:layout_marginTop="0dp"
            android:src="@drawable/dash_nav_bg1_bday" />

         <ImageView
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="4dp"
            android:layout_marginTop="0dp"
            android:src="@drawable/dash_nav_sep1" />

        <ImageView
            android:id="@+id/imageButton1"
            android:layout_marginLeft="5dp"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_marginTop="0dp"
            android:src="@drawable/dash_nav_bg1_sms" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="280dp"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="20dp"
        android:background="@drawable/dash_midl_bg_notify"
        android:layout_height="280dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:paddingTop="8dp"
            android:layout_marginLeft="60dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="29 MAY" />
 <View
        android:id="@+id/a"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/btnAdd"
        android:background="#8DB3E1" />

        <ListView
            android:id="@+id/List"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/a"
            android:divider="#8DB3E1"
            android:dividerHeight="2dp"
            android:headerDividersEnabled="true" />

    </LinearLayout>

    <ImageButton
        android:layout_marginLeft="70dp"
        android:layout_marginTop="30dp"
        android:id="@+id/imageButton2"
        android:layout_width="180dp"
        android:layout_height="37dp"
        android:src="@drawable/clear" />

</LinearLayout>




***//ListViewItem Row xml(BdayLlist.xml)***

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

         <TableLayout
             android:layout_width="match_parent"
             android:layout_height="127dp"
             android:layout_marginTop="60dp" >

             <TableRow
                 android:layout_marginTop="20dp"
                 android:id="@+id/tableRow1"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content" >

                  <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content" >
  <TextView
        android:id="@+id/test_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="id" />

         <TextView
        android:id="@+id/test_name"
        android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="vennila" />

          <TextView
        android:id="@+id/test_Loc"
        android:layout_marginLeft="120dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Location" />

         </LinearLayout>

             </TableRow>

             <TableRow
                 android:layout_marginTop="20dp"
                 android:id="@+id/tableRow2"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content" >



          <TextView
        android:id="@+id/test_BDay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bday" />


            <TextView
        android:id="@+id/test_Day"
        android:layout_marginLeft="-60dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Wednessday" />


             </TableRow>

             <TableRow
                 android:layout_marginTop="20dp"
                 android:id="@+id/tableRow3"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content" >
          <TextView
        android:id="@+id/test_toDay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Today" />
          <TextView
               android:layout_marginLeft="-60dp"
        android:id="@+id/test_Date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="May 7" />

             </TableRow>


         </TableLayout>


</LinearLayout>

MainActivity类(TodayList.java)

package com.example.sampleparse;



import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class TodayList extends Activity{
    SQLiteDatabase db;
    DbHelper dbh;
ListView lv;
private ArrayList<String> user_loc = new ArrayList<String>();

private ArrayList<String> user_fName = new ArrayList<String>();
private ArrayList<String> user_id = new ArrayList<String>();

 ArrayList<String> list= new ArrayList<String>();

 TestAdaptor ta;
 private ArrayAdapter<String> listAdapter ;  

     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
          setContentView(R.layout.dashboard_notify);
          this.lv=(ListView) findViewById(R.id.List);
            try{
        // BdayList();
                //Bname();
       //  displayData();
                 dbh=new DbHelper(this);
                 db=dbh.getWritableDatabase();
                 list=dbh.Bname();

                 String[] stockArr = new String[list.size()];
                 stockArr = list.toArray(stockArr);
                 for(String s : stockArr)
                        System.out.println(s);
                // System.out.println(stockArr.toString());

                /* user_loc=dbh.BLocation();
                 String[] loc = new String[user_loc.size()];

                 for(String l : loc)
                     System.out.println(l);*/
                 //ta = new  TestAdaptor(TodayList.this, stockArr,loc );
                 ListAdapter la = new ListAdapter(getApplicationContext(), list);
                 lv.setAdapter(la);
                /* ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                            R.layout.bdaylist, R.id.test_name, stockArr);*/

                       // setListAdapter(adapter);   

            }catch(Exception e){
                Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
            }
     }



    public void Bname(){
         dbh=new DbHelper(this);
         db=dbh.getWritableDatabase();
         list=dbh.Bname();
         listAdapter = new ArrayAdapter<String>(this, R.layout.bdaylist, list); 
         Toast.makeText(getApplicationContext(), list.toString(), Toast.LENGTH_LONG).show();
         lv.setAdapter(listAdapter);
     }

     @Override
        protected void onResume() {

            super.onResume();
            //displayData();
        }


     //worked listView(){ code
     //should extends listActivity
     public void list(){
         dbh=new DbHelper(this);
         db=dbh.getWritableDatabase();
         list=dbh.Bname();

         String[] stockArr = new String[list.size()];
         stockArr = list.toArray(stockArr);
         for(String s : stockArr)
                System.out.println(s);
        // System.out.println(stockArr.toString());
         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    R.layout.bdaylist, R.id.test_name, stockArr);
        //should uncomment the line
                //setListAdapter(adapter);   
     }

     public void displayData() {
            db = dbh.getWritableDatabase();
             Calendar c = Calendar.getInstance();
                SimpleDateFormat df1 = new SimpleDateFormat("dd/MM/yyyy");
                String formattedDate1 = df1.format(c.getTime());

                String sub = formattedDate1;
                String s1=sub.substring(0, 5);
                ArrayList<String> test=new ArrayList<String>();
           String selectQuery="SELECT * FROM " + dbh.TABLE_CONTACTS + " where dob = '" + s1 +"'";
            Cursor mCursor = db.rawQuery(selectQuery, null);
            user_id.clear();
            user_loc.clear();
            user_fName.clear();

            if (mCursor.moveToFirst()) {
                do {
                    user_id.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ID)));

                    user_fName.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.EMP_NAME)));
                    user_loc.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.EMP_LOC)));
                    System.out.println("loc " + user_loc);
                    Toast.makeText(getApplicationContext(), user_id.toString()+" "+ user_fName.toString()+ " "+ user_loc.toString(), 1000).show();
                    //user_lName.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ASSID)));
                    //user_dob.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_DOB)));

                    //user_mobno.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_MOBNO)));
                    //user_email.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_EMAIL)));

                } while (mCursor.moveToNext());
            }
            ListAdapter disadpt = new ListAdapter(TodayList.this, user_fName );
            lv.setAdapter(disadpt);
            mCursor.close();
        }





     public void BdayList(){

         dbh=new DbHelper(this);
         db=dbh.getWritableDatabase();
        list= dbh.DOB();
        // listAdapter = new ArrayAdapter<String>(this, R.layout.bdaylist, list);  
        Toast.makeText(getApplicationContext(), list.toString(), Toast.LENGTH_LONG).show();

        /*final StableArrayAdapter adapter = new StableArrayAdapter(TodayList.this,
                android.R.layout.simple_list_item_1, list);*/
        listAdapter = new ArrayAdapter<String>(this, R.layout.simplrrow, list);
            lv.setAdapter(listAdapter);

     }

     private class StableArrayAdapter extends ArrayAdapter<String> {

            HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

            public StableArrayAdapter(Context context, int textViewResourceId,
                List<String> objects) {
              super(context, textViewResourceId, objects);
              for (int i = 0; i < objects.size(); ++i) {
                mIdMap.put(objects.get(i), i);
              }
            }

            @Override
            public long getItemId(int position) {
              String item = getItem(position);
              return mIdMap.get(item);
            }

            @Override
            public boolean hasStableIds() {
              return true;
            }

          }

}

自定义适配器类(ListAdaptor.java)

package com.example.sampleparse;

import java.util.ArrayList;

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

public class ListAdapter extends BaseAdapter{
    private Context mContext;
    private ArrayList<String> id;
    private ArrayList<String> Name;
    private ArrayList<String> Assid;

    private ArrayList<String> Dob;
    private ArrayList<String> Mobno;
    private ArrayList<String> Email;
    private ArrayList<String> dept;
    private ArrayList<String> location;

    public ListAdapter(Context c,  ArrayList<String> name) {
        this.mContext = c;

        //this.id = id;
        this.Name = name;
    /*  this.Assid = assid;
        this.Dob = user_dob;
        this.Mobno = user_mobno;
        this.Email = user_email;
        this.dept = dept;*/
        //this.location = loc;
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return Name.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return Integer.valueOf(arg0);
    }
    public void setAllItems(ArrayList<String> paramArrayList) {
        this.Name.addAll(paramArrayList);
    }

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

    @Override
    public View getView(int pos, View child, ViewGroup parent) {
        // TODO Auto-generated method stub

        Holder mHolder;
        LayoutInflater layoutInflater;
        layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (child == null) {
            mHolder = new Holder();
            child = layoutInflater.inflate(R.layout.bdaylist, parent, false);
            mHolder = new Holder();
            /* localViewholder.categoryName = ((TextView) paramView
                        .findViewById(R.id.name));

                paramView.setTag(localViewholder);*/
        //  mHolder.
            mHolder.txt_id = (TextView) child.findViewById(R.id.test_id);
            mHolder.txt_Name = (TextView) child.findViewById(R.id.test_name);
            mHolder.txt_Loc = (TextView) child.findViewById(R.id.test_Loc);

            mHolder.txt_Bday = (TextView) child.findViewById(R.id.test_BDay);
            mHolder.txt_Day= (TextView) child.findViewById(R.id.test_Day);
            mHolder.txt_Today= (TextView) child.findViewById(R.id.test_toDay);
            mHolder.txt_Date = (TextView) child.findViewById(R.id.test_Date);

            child.setTag(mHolder);
        } else {
            mHolder = (Holder) child.getTag();
        }

        mHolder.txt_id.setText("id");
        mHolder.txt_Name.setText(Name.get(pos).indexOf(0));
        mHolder.txt_Loc.setText(":lov");

        mHolder.txt_Bday.setText("Birth Day");
        mHolder.txt_Day.setText("WEdness day");
        mHolder.txt_Today.setText("Today");
        mHolder.txt_Date.setText("May 7");
        mHolder.txt_Loc.setText("loc");
        return child;


    }

    public class Holder {
        TextView txt_id;
        TextView txt_Name;
        TextView txt_Bday;

        TextView txt_Day;
        TextView txt_Today;
        TextView txt_Date;
        TextView txt_Loc;

    }

}

请给我一个解决方案,我花了一天多时间坚持这个概念。

1 个答案:

答案 0 :(得分:0)

for(String s:stockArr)                         的System.out.println(一个或多个);                 // System.out.println(stockArr.toString());

            /* user_loc=dbh.BLocation();
             String[] loc = new String[user_loc.size()];

             for(String l : loc)
                 System.out.println(l);*/
             //ta = new  TestAdaptor(TodayList.this, stockArr,loc );
             ListAdapter la = new ListAdapter(getApplicationContext(), list);
             lv.setAdapter(la);

有一个问题,如果我没有错,你通过for循环递归设置你的适配器只是在循环之后只启动一次;第二件事是它可能引发的空指针异常,因为你试图访问尚未存在的值。可能是我的评论帮助你