具有自定义CursorAdapter的ListFragment

时间:2013-01-09 00:29:16

标签: android android-contentprovider android-listfragment android-cursoradapter

我正在使用自定义CursorAdapter和ContentProvider进行ListFragment。在我的ListFragment上需要多行的部分之前,一切都很顺利。

ListFragment代码:

public class FragList extends ListFragment implements LoaderCallbacks<Cursor>{
private final static String TAG = SubsList.class.getCanonicalName();
private Account account = null;
private Logs log = Logs.getInstance();
private SubscriptionAdapter mAdapter;
private ListView subscriptionListView;

private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {                                     
        if(null != mAdapter){
            log.info(TAG, "BroadcastReceiver - DataSetChanged");
                mAdapter.notifyDataSetChanged();
        }           
    }
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);                

    Context mContext = getActivity().getApplicationContext();
    registerBroadcastReceiverFilder(mContext);                

    mAdapter = new MyAdapter(mContext, mContext.getContentResolver()
            .query(CProvider.CONTENT_URI, CProvider.PROJECTION, null, null, null), 
            CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);            
    setHasOptionsMenu(true);
    setListAdapter(mAdapter);
    getLoaderManager().initLoader(0, null, this);        
}

private void registerBroadcastReceiverFilder(Context mContext) {

    IntentFilter filter = new IntentFilter(Configurations.BR_METADATA_DOWNLOAD);
    mContext.registerReceiver(mBroadcastReceiver, filter);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    MenuItem item = menu.add("Refresh");
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch(item.getItemId())
    {
        case 0:
            if(null != account){
                GetData d= new GetData(getActivity(), account);
                d.execute();
                return true;
            }
        default:
        return super.onOptionsItemSelected(item);
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);          

    switch (resultCode) {
    case Configurations.ACCOUNT_ADDED:
        account = data.getBundleExtra(Configurations.BUNDLE_DIALOG_CHOOSE_ACCOUNT).getParcelable("account");
        log.info(TAG, account.name + " - " + account.type);
        GetData d = new GetData(getActivity(), account);
        d.execute();            
        break;
    case Configurations.ACCOUNT_ADD_CANCELED:
        //TODO: Add handler for refusals
        break;
    default:
        break;
    }
}

public boolean onQueryTextSubmit(String query) {
    // Don't care about this.
    return true;
}

@Override 
public void onListItemClick(ListView l, View v, int position, long id) {
    log.info(TAG, "FragmentComplexList - Item clicked: " + id);
}

public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {       
    return new CursorLoader(getActivity(), CProvider.CONTENT_URI,
            CProvider.PROJECTION, null, null,null);
    }

public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {      
    /*log.info(TAG, "onLoadFinished called");
    mAdapter.notifyDataSetChanged();
    mAdapter.swapCursor(cursor);*/

    mAdapter.swapCursor(cursor);

    // The list should now be shown.
    if (isResumed()) {
        setListShown(true);
    } else {
        setListShownNoAnimation(true);
    }
}

public void onLoaderReset(Loader<Cursor> arg0) {
    log.info(TAG, "onLoaderReset called");
    mAdapter.swapCursor(null);
}       
}

CursorAdapter代码:

public class MyAdapter extends CursorAdapter{

public MyAdapter (Context context, Cursor c, int flags) {
    super(context, c, flags);
}

static class ViewHolder {
    public ImageView image;
    public TextView text; 
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater inflater =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.row,null,true);

    ViewHolder holder = new ViewHolder();
    holder.text= (TextView) rowView.findViewById(R.id.text);
    holder.image= (ImageView) rowView.findViewById(R.id.image);
    rowView.setTag(holder);

    return rowView;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
   ViewHolder holder = (ViewHolder) view.getTag();

   String text= cursor.getString(cursor.getColumnIndex(CProvider.TEXT_FIELD));
   holder.text.setText(text);      
}
}

row.xml:

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"   
android:orientation="horizontal"
android:padding="5dip" >

<LinearLayout android:id="@+id/llimage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="3dip"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="5dip">

    <ImageView
        android:id="@+id/image"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:src="@drawable/placeholder" 
        android:contentDescription="TODO"/>

</LinearLayout>


<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/llimage"
    android:layout_toRightOf="@+id/llimage"
    android:text="@string/text_placeholder"
    android:textColor="#040404"
    android:typeface="sans"
    android:textSize="15dip"
    android:textStyle="bold"/>
 </RelativeLayout>

main.xml中:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:orientation="vertical">

<FrameLayout 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:id="@+id/flList">
</FrameLayout> 

</LinearLayout>

</ScrollView> 

我应该有或多或少20行,而我只能获得一行。这是我第一次使用这种混合物,所以我有点迷失。

感谢您的帮助,Dporem

0 个答案:

没有答案