11

我已经从ResourceCursorAdapter我使用的地方newView切换bindViewSimpleCursorAdapter我只使用该getView方法的地方。

现在我在onLoaderFinished. 尽管它在我的适配器和游标对象NullPointerException上都给了我NOT null。我将在下面发布我的所有代码。非常感谢任何帮助(没有多少头发可以拔出)。adapter.swapCursor(cursor)

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.ResourceCursorAdapter;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

public class ContactSelect extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {
private static final int LOADER_ID = 1;
private MyAdapter adapter;
private ListView list;
private View row;
private SparseBooleanArray checkedState = new SparseBooleanArray();

@SuppressLint({ "NewApi", "NewApi" })
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.activity_contact_select);       

    adapter = new MyAdapter(this, R.layout.contacts_select_row, null, null, null, 0);     

    getSupportLoaderManager().initLoader(LOADER_ID, null, this);        

    list = (ListView)findViewById(R.id.list);                 

    list.setAdapter(adapter);
    list.setEmptyView(findViewById(R.id.empty));    

}   

@SuppressLint("NewApi")
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
    final String projection[] = new String[]{ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME};
    final Uri uri = ContactsContract.Contacts.CONTENT_URI;

    final String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1" + 
    " AND " + ContactsContract.Contacts.IN_VISIBLE_GROUP + " =1";

    final String order = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    final CursorLoader loader = new CursorLoader(this, uri, projection, selection, null, order);

    return loader;      
}

public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    for(int i=0;i<cursor.getCount();i++){
        checkedState.put(i, false);
    }

    adapter.swapCursor(cursor);                 
}

public void onLoaderReset(Loader<Cursor> loader) {
    adapter.swapCursor(null);       
}

private class MyAdapter extends SimpleCursorAdapter implements OnClickListener{
    private CheckBox markedBox;
    private TextView familyText;
    private Context context;
    private Cursor cursor;

    public MyAdapter(Context context, int layout, Cursor c, String[] from,
            int[] to, int flags) {
        super(context, layout, c, from, to, flags);

        this.context = context;
        this.cursor = getCursor();
    }

    @Override
    public View getView(int position, View view, ViewGroup group) {

        final LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = li.inflate(R.layout.contacts_select_row, group, false);

        view.setTag(cursor.getPosition());
        view.setOnClickListener(this);

        familyText = (TextView)view.findViewById(R.id.contacts_row_family_name);
        markedBox = (CheckBox)view.findViewById(R.id.contacts_row_check);
        familyText.setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));

        boolean currentlyChecked = checkedState.get(cursor.getPosition());
        markedBox.setChecked(currentlyChecked);     


        setProgressBarIndeterminateVisibility(false);

        return super.getView(position, view, group);
    }

    public void onClick(View view) {
        int rowId = (Integer)view.getTag();
        Log.d("OnClick", String.valueOf(rowId));
        boolean currentlyChecked = checkedState.get(rowId);
        markedBox.setChecked(!currentlyChecked);
        checkedState.put(rowId, !currentlyChecked);
        Log.d("checkedState", "checkedState(" + rowId + ") = " + checkedState.get(rowId));
    }       
      }     
}
4

2 回答 2

12

调用该类的swapCursor方法SimpleCursorAdapter将触发一个函数,该函数将String提供给构造函数(第 4 个参数)的数组中的列名映射到表示列索引的整数数组。当您传入表示来自游标的列名的数组null的构造函数时,这将在稍后尝试进行映射时抛出(应该出现在方法中,这是使用列名数组的实际方法)。MyAdapterStringNullPointerExceptionswapCursorNullPointerExceptionfindColumnsString

解决方案是传递一个有效的String数组,您可能还希望对int表示放置数据的视图的 id 的数组执行此操作:

String[] from = {ContactsContract.Contacts.DISPLAY_NAME};
int[] to = {R.id.contacts_row_family_name, R.id.contacts_row_check};
adapter = new MyAdapter(this, R.layout.contacts_select_row, null, from, to, 0);

我不知道您要做什么,但是您对该getView方法的实现并不完全正确:

您为该getView方法执行常规操作(创建布局、搜索视图、绑定数据),然后您只需从超类返回视图(?!?),您可能只会看到默认布局,其中没有任何内容。

您编写该getView方法的方式不是很有效,您可能需要查看视图回收和视图持有者模式。

cursor.getPosition()不会做你想做的事,因为你没有将光标移动到正确的位置。默认情况下,基于光标的适配器将在getView方法中为您执行此操作,但是,当您覆盖该方法时,移动光标的位置是您的工作。

您应该离开该getView 方法并使用这两种方法newViewbindView因为它们提供了更好的逻辑分离。

于 2012-09-08T12:48:29.547 回答
2

适配器 = new MyAdapter(this, R.layout.contacts_select_row, null, null, null, 0);

以及您传递空光标的 MyAdapter 类

  public MyAdapter(Context context, int layout, Cursor c, String[] from,
            int[] to, int flags) {
        super(context, layout, c, from, to, flags);

        this.context = context;
        this.cursor = getCursor();
    }   
于 2012-09-02T11:51:31.833 回答