Android列表视图

时间:2014-04-07 09:50:47

标签: android android-layout android-listview android-location

我正在尝试开发Android应用程序我希望以一种很好的方式显示用户联系人,我使用的是ListView。但有例外。

这是代码:

package com.example.list;

import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Display extends Activity implements OnItemClickListener{

List<String> name1 = new ArrayList<String>();
List<String> phno1 = new ArrayList<String>();
MyAdapter ma ;
Button select;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.display);

    getAllContacts(this.getContentResolver());
    ListView lv= (ListView)findViewById(android.R.id.list);
        ma = new MyAdapter();
        lv.setAdapter(ma);
        lv.setOnItemClickListener(this); 
        lv.setItemsCanFocus(false);
        lv.setTextFilterEnabled(true);
        // adding
       select = (Button) findViewById(R.id.button1);
    select.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v) {
              StringBuilder checkedcontacts= new StringBuilder();
            System.out.println(".............."+ma.mCheckStates.size());
            for(int i = 0; i < name1.size(); i++)

                {
                if(ma.mCheckStates.get(i)==true)
                {
                     checkedcontacts.append(name1.get(i).toString());
                     checkedcontacts.append("\n");

                }
                else
                {
                    System.out.println("Not Checked......"+name1.get(i).toString());
                }


            }

            Toast.makeText(Display.this, checkedcontacts,1000).show();
        }       
    });
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
     ma.toggle(arg2);
}

public  void getAllContacts(ContentResolver cr) {

    Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
    while (phones.moveToNext())
    {
      String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
      String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
      System.out.println(".................."+phoneNumber); 
      name1.add(name);
      phno1.add(phoneNumber);
    }

    phones.close();
 }
class MyAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener
{  private SparseBooleanArray mCheckStates;
   LayoutInflater mInflater;
    TextView tv1,tv;
    CheckBox cb;
    MyAdapter()
    {
        mCheckStates = new SparseBooleanArray(name1.size());
        mInflater = (LayoutInflater)Display.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return name1.size();
    }

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

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

        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View vi=convertView;
        if(convertView==null)
        // vi = mInflater.inflate(R.layout.row, null); 
             vi = mInflater.inflate(R.layout.gg, null);
         TextView tv= (TextView) vi.findViewById(R.id.textView1);
         tv1= (TextView) vi.findViewById(R.id.textView2);
        // cb = (CheckBox) vi.findViewById(R.id.checkBox1);
         tv.setText("Name :"+ name1.get(position));
         tv1.setText("Phone No :"+ phno1.get(position));
         cb.setTag(position);
         cb.setChecked(mCheckStates.get(position, false));
         cb.setOnCheckedChangeListener(this);

        return vi;
    }
     public boolean isChecked(int position) {
            return mCheckStates.get(position, false);
        }

        public void setChecked(int position, boolean isChecked) {
            mCheckStates.put(position, isChecked);
            System.out.println("hello...........");
            notifyDataSetChanged();
        }
        public void toggle(int position) {
            setChecked(position, !isChecked(position));
        }
    @Override
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
        // TODO Auto-generated method stub

         mCheckStates.put((Integer) buttonView.getTag(), isChecked);         
    }   
}   
}

这里是display xml:

<RelativeLayout 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=".Display" >
<ListView
   android:id="@+android:id/list"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_above="@+id/button1" />
 <Button
     android:id="@+id/button1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true"
     android:layout_centerHorizontal="true"
     android:text="@string/Select" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

和行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" >
<TextView
    android:id="@+android:id/textView1"
    android:layout_width="270dp"
    android:layout_height="wrap_content"
    android:text="TextView" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="243dp"
    android:layout_height="wrap_content"
    android:text="TextView" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/CheckBox" />

这里是logcat:

04-07 12:35:37.249: E/AndroidRuntime(18080): FATAL EXCEPTION: main
04-07 12:35:37.249: E/AndroidRuntime(18080): java.lang.NullPointerException
04-07 12:35:37.249: E/AndroidRuntime(18080):    at com.example.list.Display$MyAdapter.getView(Display.java:137)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.widget.AbsListView.obtainView(AbsListView.java:2624)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1254)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.widget.ListView.onMeasure(ListView.java:1166)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at  android.view.View.measure(View.java:16829)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.widget.RelativeLayout.measureChild(RelativeLayout.java:698)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:494)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.view.View.measure(View.java:16829)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.view.View.measure(View.java:16829)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.view.View.measure(View.java:16829)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2581)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.view.View.measure(View.java:16829)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2186)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1349)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1532)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6339)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.view.Choreographer.doCallbacks(Choreographer.java:591)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.view.Choreographer.doFrame(Choreographer.java:561)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.os.Handler.handleCallback(Handler.java:730)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.os.Looper.loop(Looper.java:137)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at android.app.ActivityThread.main(ActivityThread.java:5450)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at java.lang.reflect.Method.invokeNative(Native Method)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at java.lang.reflect.Method.invoke(Method.java:525)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
04-07 12:35:37.249: E/AndroidRuntime(18080):    at dalvik.system.NativeStart.main(Native Method)

我不知道那里有什么问题。

2 个答案:

答案 0 :(得分:0)

试试这个

@Override

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

View vi=convertView; if(convertView==null){ vi = mInflater.inflate(R.layout.row, null); } TextView tv= (TextView) vi.findViewById(R.id.textView1); tv1= (TextView) vi.findViewById(R.id.textView2); cb = (CheckBox) vi.findViewById(R.id.checkBox1); tv.setText("Name :"+ name1.get(position)); tv1.setText("Phone No :"+ phno1.get(position)); cb.setTag(position); cb.setOnCheckedChangeListener(this); cb.setChecked(mCheckStates.get(position, false)); return vi;

}

答案 1 :(得分:0)

检查listview适配器中的getView()方法,您已注释了复选框cb初始化行

// cb = (CheckBox) vi.findViewById(R.id.checkBox1);

同样 row.xml 布局应该膨胀而不是 gg.xml

所以getView()应如下所示:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View vi=convertView;
    if(convertView==null)
        vi = mInflater.inflate(R.layout.row, null); 

    TextView tv= (TextView) vi.findViewById(R.id.textView1);
    tv1= (TextView) vi.findViewById(R.id.textView2);
    cb = (CheckBox) vi.findViewById(R.id.checkBox1);
    tv.setText("Name :"+ name1.get(position));
    tv1.setText("Phone No :"+ phno1.get(position));
    cb.setTag(position);
    cb.setChecked(mCheckStates.get(position, false));
    cb.setOnCheckedChangeListener(this);

    return vi;
}

希望它会帮助你..