在android中使用arrayadapter的listview

时间:2013-12-31 05:39:09

标签: android listview android-arrayadapter

我正在尝试在listview

中显示元素列表
I have also used holder design pattern for efficiency of listview 

MainActivity.java

public class MainActivity extends Activity {
    ListView LV;

    String[] TITLE;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Bring the data from the resources to Java
        Resources res=getResources();//To Bring it use the resources references

        TITLE=res.getStringArray(R.array.title);

        LV=(ListView) findViewById(R.id.listView_id);

        AdapterClass myAdapter=new AdapterClass(this,TITLE);//declare the custom adapter
        LV.setAdapter(myAdapter);//set the adapter
    }
}

AdapterClass.java

public class AdapterClass extends ArrayAdapter<String>{

    Context cxt; String[] titleArray;
    public AdapterClass(Context c, String[] tITLE) {
        // TODO Auto-generated constructor stub
        super(c, R.layout.single_item, R.id.title_textView_id, tITLE);
        this.titleArray=tITLE;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        View row=convertView;
        MyViewHolder holder=null;
        if(row==null)
        {
            LayoutInflater inflater=(LayoutInflater) cxt.getSystemService(cxt.LAYOUT_INFLATER_SERVICE);
            row=inflater.inflate(R.layout.single_item, parent, false);
            holder=new MyViewHolder(row);
            row.setTag(holder);
        }
        else
        {
            holder=(MyViewHolder) row.getTag();
        }

        holder.myTitle.setText(titleArray[position]);
        return row;
    }



}

MyViewHolder.java

public class MyViewHolder {

    TextView myTitle;

    MyViewHolder(View row)
    {
        myTitle=(TextView) row.findViewById(R.id.title_textView_id);
    }


}

登录 ::

12-31 11:03:17.070: E/AndroidRuntime(313): FATAL EXCEPTION: main
12-31 11:03:17.070: E/AndroidRuntime(313): java.lang.NullPointerException
12-31 11:03:17.070: E/AndroidRuntime(313):  at com.example.listviewpractice.AdapterClass.getView(AdapterClass.java:29)
12-31 11:03:17.070: E/AndroidRuntime(313):  at android.widget.AbsListView.obtainView(AbsListView.java:1315)
12-31 11:03:17.070: E/AndroidRuntime(313):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
12-31 11:03:17.070: E/AndroidRuntime(313):  at android.widget.ListView.onMeasure(ListView.java:1109)
12-31 11:03:17.070: E/AndroidRuntime(313):  at android.view.View.measure(View.java:8171)
12-31 11:03:17.070: E/AndroidRuntime(313):  at android.widget.RelativeLayout.measureChild(RelativeLayout.java:563)
12-31 11:03:17.070: E/AndroidRuntime(313):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:378)
12-31 11:03:17.070: E/AndroidRuntime(313):  at android.view.View.measure(View.java:8171)
12-31 11:03:17.070: E/AndroidRuntime(313):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-31 11:03:17.070: E/AndroidRuntime(313):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
12-31 11:03:17.070: E/AndroidRuntime(313):  at android.view.View.measure(View.java:8171)
12-31 11:03:17.070: E/AndroidRuntime(313):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
12-31 11:03:17.070: E/AndroidRuntime(313):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
12-31 11:03:17.070: E/AndroidRuntime(313):  at android.view.View.measure(View.java:8171)
12-31 11:03:17.070: E/AndroidRuntime(313):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-31 11:03:17.070: E/AndroidRuntime(313):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
12-31 11:03:17.070: E/AndroidRuntime(313):  at android.view.View.measure(View.java:8171)
12-31 11:03:17.070: E/AndroidRuntime(313):  at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
12-31 11:03:17.070: E/AndroidRuntime(313):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
12-31 11:03:17.070: E/AndroidRuntime(313):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-31 11:03:17.070: E/AndroidRuntime(313):  at android.os.Looper.loop(Looper.java:123)
12-31 11:03:17.070: E/AndroidRuntime(313):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-31 11:03:17.070: E/AndroidRuntime(313):  at java.lang.reflect.Method.invokeNative(Native Method)
12-31 11:03:17.070: E/AndroidRuntime(313):  at java.lang.reflect.Method.invoke(Method.java:521)
12-31 11:03:17.070: E/AndroidRuntime(313):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-31 11:03:17.070: E/AndroidRuntime(313):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-31 11:03:17.070: E/AndroidRuntime(313):  at dalvik.system.NativeStart.main(Native Method)

{编辑} - Wrt- Raghunandan回答

AdapterClass.java

public class AdapterClass extends ArrayAdapter<String>{

    Context cxt; String[] titleArray;

    LayoutInflater mInflater;

    public AdapterClass(Context c, String[] tITLE) {
        // TODO Auto-generated constructor stub
        super(c, R.layout.single_item, R.id.title_textView_id, tITLE);
        this.titleArray=tITLE;
        this.cxt=c;
         mInflater = LayoutInflater.from(c);


    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        View row=convertView;
        MyViewHolder holder=null;
        if(row==null)
        {

            row=mInflater.inflate(R.layout.single_item, parent, false);
            holder.myTitle =(TextView) row.findViewById(R.id.title_textView_id);
            //holder=new MyViewHolder(row);
            row.setTag(holder);
        }
        else
        {
            holder=(MyViewHolder) row.getTag();
        }

        holder.myTitle.setText(titleArray[position]);
        return row;
    }



}

MyViewHolder.java

public class MyViewHolder {

    TextView myTitle;



}

日志 ::

12-31 11:33:07.360: E/AndroidRuntime(504): FATAL EXCEPTION: main
12-31 11:33:07.360: E/AndroidRuntime(504): java.lang.NullPointerException
12-31 11:33:07.360: E/AndroidRuntime(504):  at com.example.listviewpractice.AdapterClass.getView(AdapterClass.java:39)
12-31 11:33:07.360: E/AndroidRuntime(504):  at android.widget.AbsListView.obtainView(AbsListView.java:1315)
12-31 11:33:07.360: E/AndroidRuntime(504):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
12-31 11:33:07.360: E/AndroidRuntime(504):  at android.widget.ListView.onMeasure(ListView.java:1109)
12-31 11:33:07.360: E/AndroidRuntime(504):  at android.view.View.measure(View.java:8171)
12-31 11:33:07.360: E/AndroidRuntime(504):  at android.widget.RelativeLayout.measureChild(RelativeLayout.java:563)
12-31 11:33:07.360: E/AndroidRuntime(504):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:378)
12-31 11:33:07.360: E/AndroidRuntime(504):  at android.view.View.measure(View.java:8171)
12-31 11:33:07.360: E/AndroidRuntime(504):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-31 11:33:07.360: E/AndroidRuntime(504):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
12-31 11:33:07.360: E/AndroidRuntime(504):  at android.view.View.measure(View.java:8171)
12-31 11:33:07.360: E/AndroidRuntime(504):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
12-31 11:33:07.360: E/AndroidRuntime(504):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
12-31 11:33:07.360: E/AndroidRuntime(504):  at android.view.View.measure(View.java:8171)
12-31 11:33:07.360: E/AndroidRuntime(504):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-31 11:33:07.360: E/AndroidRuntime(504):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
12-31 11:33:07.360: E/AndroidRuntime(504):  at android.view.View.measure(View.java:8171)
12-31 11:33:07.360: E/AndroidRuntime(504):  at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
12-31 11:33:07.360: E/AndroidRuntime(504):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
12-31 11:33:07.360: E/AndroidRuntime(504):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-31 11:33:07.360: E/AndroidRuntime(504):  at android.os.Looper.loop(Looper.java:123)
12-31 11:33:07.360: E/AndroidRuntime(504):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-31 11:33:07.360: E/AndroidRuntime(504):  at java.lang.reflect.Method.invokeNative(Native Method)
12-31 11:33:07.360: E/AndroidRuntime(504):  at java.lang.reflect.Method.invoke(Method.java:521)
12-31 11:33:07.360: E/AndroidRuntime(504):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-31 11:33:07.360: E/AndroidRuntime(504):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-31 11:33:07.360: E/AndroidRuntime(504):  at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:2)

Context cxt;

未在您的代码中的任何位置初始化..

所以此行抛出异常..

LayoutInflater inflater=(LayoutInflater) cxt.getSystemService(cxt.LAYOUT_INFLATER_SERVICE);

在构造函数中初始化,如..

 public AdapterClass(Context c, String[] tITLE) {
    // TODO Auto-generated constructor stub
    super(c, R.layout.single_item, R.id.title_textView_id, tITLE);
    this.titleArray=tITLE;
    cxt=c;
}

答案 1 :(得分:1)

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

 if(row==null)
    {
        LayoutInflater inflater=(LayoutInflater) cxt.getSystemService(cxt.LAYOUT_INFLATER_SERVICE);
        row=inflater.inflate(R.layout.single_item, parent, false);
        holder=new MyViewHolder(row);
        holder.myTitle =(TextView) row.findViewById(R.id.textviewidhere); // initialize
        row.setTag(holder);
    }

并改为

static class MyViewHolder { 
TextView myTitle;
}

String[] titleArray;
 LayoutInflater mInflater
    public AdapterClass(Context c, String[] tITLE) {
        // TODO Auto-generated constructor stub
        super(c, R.layout.single_item, R.id.title_textView_id, tITLE);
        this.titleArray=tITLE;
        mInflater = LayoutInflater.from(c);  
    }

然后在getView中使用mInflater。你可以删除这个

LayoutInflater inflater=(LayoutInflater) cxt.getSystemService(cxt.LAYOUT_INFLATER_SERVICE)

并使用

row=mInflater.inflate(R.layout.single_item, parent, false);

编辑:1

row=mInflater.inflate(R.layout.single_item, parent, false);
holder=new MyViewHolder(row);
holder.myTitle =(TextView) row.findViewById(R.id.title_textView_id);

答案 2 :(得分:0)

实际上问题是你没有在'else condition'中初始化视图持有者,你只在'if condition'中初始化它,然后如果代码通过'else条件'然后没有得到视图持有者的初始化它会给出nullpointerexception 。