使用Listfragment更新或刷新的Listview失败

时间:2015-08-15 17:59:24

标签: java android listview android-fragments

我已经用滑动视图创建了一个Android应用程序,我添加了2个选项卡,在一个选项卡中我试图加载所有联系人,我成功了但是现在我试图过滤联系人但是收到错误

java.lang.IllegalStateException: Could not execute method of the activity

这是我的代码

 package com.Main.vsales;
 import android.content.Context;
 import android.database.Cursor;
 import android.os.Bundle;
 import android.provider.ContactsContract;
 import android.support.v4.app.ListFragment;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ListView;
 import android.widget.SimpleCursorAdapter;
 import android.app.Activity;

public class Contacts extends ListFragment  {


ListView lv;
Cursor cursor1;
int itemPosition;
String  itemValue;
SimpleCursorAdapter listadapter;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

    lv=(ListView) getListView().findViewById(android.R.id.list);

 cursor1 = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME +" ASC");     
    getActivity().startManagingCursor(cursor1);
 String[] from = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone._ID,}; 


    int[] to = {android.R.id.text1, android.R.id.text2}; 


     listadapter = new SimpleCursorAdapter(getActivity().getBaseContext(),      android.R.layout.simple_list_item_2, cursor1, from, to);

        setListAdapter(listadapter);


    lv = getListView();
        lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

}


  public void loadfiltercontact(String filterLetters,Context actContext) {

lv=(ListView) ((Activity) actContext).findViewById(android.R.id.list);
  cursor1 = actContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" LIKE '"+filterLetters+"%'", null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME +" ASC");

    ((Activity) actContext).startManagingCursor(cursor1);


   String[] from = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,        ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone._ID,}; 
    int[] to = {android.R.id.text1, android.R.id.text2}; 


     listadapter = new SimpleCursorAdapter(actContext, android.R.layout.simple_list_item_2, cursor1, from, to);


        setListAdapter(listadapter);

        listadapter.notifyDataSetChanged();

        lv = getListView();
        lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

   }


public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

View v = inflater.inflate(R.layout.contactslistlayot ,container,false);


      return v;


}


 }

这是我的xml页面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@android:color/holo_blue_dark"
android:layout_height="match_parent" >

<LinearLayout
    android:id="@+id/linearLayout1"
    android:background="@android:color/holo_orange_light"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="SHOP PICK"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"

        android:onClick="searchbuttonclick"
        android:text="search" />

</LinearLayout>

<Space
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/linearLayout1"
    android:layout_marginLeft="51dp"
    android:layout_marginTop="23dp" />

<Space
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/linearLayout1"
    android:layout_marginLeft="44dp"
    android:layout_marginTop="37dp" />

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/linearLayout1"
    android:layout_marginTop="25dp" >
</ListView>

我的错误是

  1. 致命的例外
  2. java.lang.IllegalStateException:无法执行活动的方法
  3. 在android.os.Handler.handleCallback(Handler.java:733)
  4. 在android.os.Looper.loop(Looper.java:146)
  5. 等等然后如何刷新我的列表视图?

0 个答案:

没有答案