我们可以有多个扩展 CursorAdapter 的活动吗

维萨姆

我需要有多个 CursorAdapter 以便每个都引用某个片段。但是我注意到只有一个 CursorAdapter 在工作。我需要知道应该怎么做才能使两者都工作。

PlatformCursorAdapter 访问表存储

public class PlatformCursorAdapter extends CursorAdapter {

private static final String LOG_TAG = PlatformCursorAdapter.class.getName();

public PlatformCursorAdapter(Context context, Cursor cursor){
    super(context, cursor, 0);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.list_view, parent, false);
}

@Override
public void bindView(final View view, final Context context, Cursor cursor) {
    //find which fields should be populated
    TextView itemName = (TextView) view.findViewById(R.id.item_name);
    TextView itemPrice = (TextView) view.findViewById(R.id.item_price);
    final TextView itemQuantity = (TextView) view.findViewById(R.id.item_quantity);
    final TextView itemWeight = view.findViewById(R.id.item_weight);
    TextView supplierName = (TextView) view.findViewById(R.id.item_supplier_name);
    TextView supplierNumber = (TextView) view.findViewById(R.id.item_supplier_number);

    //find the columns of pets we're interested in
    final String mItemName = cursor.getString(cursor.getColumnIndex(StoreEntry.COLUMN_PRODUCT_NAME));
    String mItemPrice = cursor.getString(cursor.getColumnIndex(StoreEntry.COLUMN_PRODUCT_PRICE));
    final String mItemQuantity = cursor.getString(cursor.getColumnIndex(StoreEntry.COLUMN_PRODUCT_QUANTITY));
    final String mItemWeight = cursor.getString(cursor.getColumnIndex(StoreEntry.COLUMN_PRODUCT_WEIGHT));
    String mSupplierName = cursor.getString(cursor.getColumnIndex(StoreEntry.COLUMN_SUPPLIER_NAME));
    final String mSupplierNumber = cursor.getString(cursor.getColumnIndex(StoreEntry.COLUMN_SUPPLIER_NUMBER));

    //get current item id
    int currentId = cursor.getInt(cursor.getColumnIndex(StoreEntry._ID));
    //Create the content Uri for the current Id
    final Uri contentUri = Uri.withAppendedPath(StoreEntry.CONTENT_URI, Integer.toString(currentId));

    //populate fields RecipeFragment
    itemName.setText(mItemName);
    itemPrice.setText(mItemPrice);
    itemQuantity.setText(mItemQuantity);
    itemWeight.setText(mItemWeight);
    supplierName.setText(mSupplierName);
    supplierNumber.setText(mSupplierNumber);
}
}

这是访问 recipies 表的 RecipeCursorAdapter

public class RecipeCursorAdapter extends CursorAdapter {

private static final String LOG_TAG = RecipeCursorAdapter.class.getName();

public RecipeCursorAdapter (Context context, Cursor cursor1){
    super(context, cursor1, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.recipe_activity, parent, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    //find which fields would be populated
    TextView recipeName = view.findViewById(R.id.recipe_name);
    TextView recipeType = view.findViewById(R.id.recipe_type);
    TextView recipeServings = view.findViewById(R.id.recipe_servings);
    TextView recipeValues = view.findViewById(R.id.ingrediant_name_1);

    //get the data from the table recipes
    Log.e(LOG_TAG, "Number of Columns " + cursor.getColumnCount());
    Log.e(LOG_TAG, "This is the Cursor Location " + cursor.getColumnIndex(RecipeEntry.COLUMN_RECIPE_SERVINGS));
    String mRecipeName = cursor.getString(cursor.getColumnIndex(RecipeEntry.COLUMN_RECIPE_NAME));
    String mRecipeType = cursor.getString(cursor.getColumnIndex(RecipeEntry.COLUMN_RECIPE_TYPE));
    String mRecipeServings = cursor.getString(cursor.getColumnIndex(RecipeEntry.COLUMN_RECIPE_SERVINGS));
    String mRecipeValues = cursor.getString(cursor.getColumnIndex(RecipeEntry.COLUMN_RECIPE_VALUES));

    //get current item Id
    int currenId = cursor.getInt(cursor.getColumnIndex(RecipeEntry._ID));
    //create a content Uri for the current Id
    final Uri contentUri = Uri.withAppendedPath(RecipeEntry.CONTENT_URI, Integer.toString(currenId));

    //populate fields with their respective data
    recipeName.setText(mRecipeName);
    recipeType.setText(mRecipeType);
    recipeServings.setText(mRecipeServings);
    recipeValues.setText(mRecipeValues);

}
}

我注意到 RecipeCursorAdapter 也在引用商店表,并产生错误。

    05-26 17:51:33.218 3468-3468/com.example.android.businessplatform E/com.example.android.businessplatform.RecipeCursorAdapter: Number of Columns 7
    This is the Cursor Location -1
05-26 17:51:33.218 3468-3468/com.example.android.businessplatform E/CursorWindow: Failed to read row 0, column -1 from a CursorWindow which has 2 rows, 7 columns.
05-26 17:51:33.220 3468-3468/com.example.android.businessplatform E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.android.businessplatform, PID: 3468
    java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
        at android.database.CursorWindow.nativeGetString(Native Method)
        at android.database.CursorWindow.getString(CursorWindow.java:438)
        at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
        at android.database.CursorWrapper.getString(CursorWrapper.java:137)
        at com.example.android.businessplatform.RecipeCursorAdapter.bindView(RecipeCursorAdapter.java:37)
        at android.widget.CursorAdapter.getView(CursorAdapter.java:289)
        at android.widget.AbsListView.obtainView(AbsListView.java:2413)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1331)
        at android.widget.ListView.onMeasure(ListView.java:1238)
        at android.view.View.measure(View.java:19937)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
        at android.view.View.measure(View.java:19937)
        at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1059)
        at android.view.View.measure(View.java:19937)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1632)
        at android.view.View.measure(View.java:19937)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
        at android.view.View.measure(View.java:19937)
        at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1059)
        at android.view.View.measure(View.java:19937)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:141)
        at android.view.View.measure(View.java:19937)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)
        at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:400)
        at android.view.View.measure(View.java:19937)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:19937)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1489)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:775)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:657)
        at android.view.View.measure(View.java:19937)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:729)
        at android.view.View.measure(View.java:19937)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2442)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1488)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1747)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1372)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6782)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:933)
        at android.view.Choreographer.doCallbacks(Choreographer.java:742)
        at android.view.Choreographer.doFrame(Choreographer.java:674)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:919)
        at android.os.Handler.handleCallback(Handler.java:761)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:156)
        at android.app.ActivityThread.main(ActivityThread.java:6623)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
迈克

消息:-

05-26 17:51:33.218 3468-3468/com.example.android.businessplatform E/CursorWindow: Failed to read row 0, column -1 from a CursorWindow which has 2 rows, 7 columns.

是说在具有 2 行和 7 列的游标中,偏移量 -1 处没有列。因此偏移量 0-6 将与游标中的列相关。

您得到-1,因为当指定的列不是 Cursor 中的列之一时,getColumnIndex方法返回-1

例如,如果您有一个包含 col1、col2 列的表,并且您运行了一个创建 Cursor c 的查询,那么:-

c.getColumnIndex("col1")将返回偏移量 0。c.getColumnIndex("col2")将返回偏移量 1。

同时c.getColumnIndex("col3"),因为没有指定的列将返回-1的offfset COL3

不能仅从适配器的名称中确定列名不存在的确切原因,因为可能的问题是传递给适配器的游标和适配器内正在访问的列的组合。因此需要用于调用适配器的代码来确定确切原因。

因此,您需要检查传递给适配器的 Cursor 是否包含预期的列。您可以通过使用适配器DatabaseUtils.dumpCurrentRow方法中的bindView方法然后检查输出到日志来完成此操作。


这是两个片段的非常基本的示例,每个片段都有一个列表(StoreEntry 和 RecipeeEntry),但基于您的一些代码。

它有一个StoreEntry类:-

public class StoreEntry {
    public static final String TBNAME = "store_entry";
    public static final String COLUMN_PRODUCT_NAME = "product_name";
    public static final String COLUMN_PRODUCT_PRICE = "product_price";
    public static final String COLUMN_PRODUCT_QUANTITY = "product_quantity";
    public static final String COLUMN_PRODUCT_WEIGHT = "product_weight";
    public static final String COLUMN_SUPPLIER_NAME = "supplier_name";
    public static final String COLUMN_SUPPLIER_NUMBER = "supplier_number";
}

RecipeeEntry类为:-

public class RecipeeEntry {
    public static final String COLUMN_RECIPEE_SERVINGS = "recipee_servings";
    public static final String COLUMN_RECIPEE_NAME = "recipee_name";
    public static final String COLUMN_RECIPEE_TYPE = "recipee_type";
    public static final String COLUMN_RECIPEE_VALUES = "recipee_values";
}

数据库助手类DBHelper :-

public class DBHelper extends SQLiteOpenHelper {

    public static final String DBNAME = "mydatabase";
    public static final int DBVERSION = 1;

    public static final String TBSTORE = "store";
    public static final String TBRECIPEE = "recipee";

    private static final String store_crt = "CREATE TABLE IF NOT EXISTS " +
            TBSTORE + "(" +
            BaseColumns._ID + " INTEGER PRIMARY KEY, " +
            StoreEntry.COLUMN_PRODUCT_NAME + " TEXT, " +
            StoreEntry.COLUMN_PRODUCT_PRICE + " REAL, " +
            StoreEntry.COLUMN_PRODUCT_QUANTITY + " INTEGER, " +
            StoreEntry.COLUMN_PRODUCT_WEIGHT + " INTEGER, " +
            StoreEntry.COLUMN_SUPPLIER_NAME + " TEXT, " +
            StoreEntry.COLUMN_SUPPLIER_NUMBER + " INTEGER" +
            ")";

    private static final String recipee_crt = "CREATE TABLE IF NOT EXISTS " +
            TBRECIPEE + "(" +
            BaseColumns._ID + " INTEGER PRIMARY KEY, " +
            RecipeeEntry.COLUMN_RECIPEE_NAME + " TEXT, " +
            RecipeeEntry.COLUMN_RECIPEE_TYPE + " TEXT, " +
            RecipeeEntry.COLUMN_RECIPEE_SERVINGS + " INTEGER, " +
            RecipeeEntry.COLUMN_RECIPEE_VALUES + " TEXT " +
            ")";

    SQLiteDatabase mDB;


    public DBHelper(Context context) {
        super(context, DBNAME, null, DBVERSION);
        mDB = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(store_crt);
        db.execSQL(recipee_crt);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {

    }

    public long addStore(String product_name,
                         Float product_price,
                         int product_quantity,
                         int product_weight,
                         String supplier_name,
                         int supplier_number) {

        ContentValues cv = new ContentValues();
        cv.put(StoreEntry.COLUMN_PRODUCT_NAME,product_name);
        cv.put(StoreEntry.COLUMN_PRODUCT_PRICE,product_price);
        cv.put(StoreEntry.COLUMN_PRODUCT_QUANTITY,product_quantity);
        cv.put(StoreEntry.COLUMN_PRODUCT_WEIGHT,product_weight);
        cv.put(StoreEntry.COLUMN_SUPPLIER_NAME,supplier_name);
        cv.put(StoreEntry.COLUMN_SUPPLIER_NUMBER,supplier_number);
        return mDB.insert(TBSTORE,null,cv);
    }

    public Cursor getStoreEntries() {
        return mDB.query(TBSTORE,null,null,null,null,null,null);
    }

    public long addRecipee(String recipee_name, String recipee_type,int recipee_servings, String recipee_values) {
        ContentValues cv = new ContentValues();
        cv.put(RecipeeEntry.COLUMN_RECIPEE_NAME, recipee_name);
        cv.put(RecipeeEntry.COLUMN_RECIPEE_TYPE,recipee_type);
        cv.put(RecipeeEntry.COLUMN_RECIPEE_SERVINGS,recipee_servings);
        cv.put(RecipeeEntry.COLUMN_RECIPEE_VALUES,recipee_values);
        return mDB.insert(TBRECIPEE,null,cv);
    }

    public Cursor getRecipeeEntries() {
        return mDB.query(TBRECIPEE,null,null,null,null,null,null);
    }
}

一个非常基本的 MainActivity,如果不存在,它将添加一些数据:-

public class MainActivity extends AppCompatActivity
    implements
        StoreEntryFragment.OnFragmentInteractionListener,
        RecipeeEntryFragment.OnFragmentInteractionListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DBHelper mDBHlpr = new DBHelper(this);
        if (DatabaseUtils.queryNumEntries(mDBHlpr.getWritableDatabase(),DBHelper.TBSTORE) < 1) {
            mDBHlpr.addStore("Rice",1.53f,10,1000,"The Rice Company",1);
            mDBHlpr.addStore("Bran",2.34f,100,500,"BranDed",25);
        }
        if(DatabaseUtils.queryNumEntries(mDBHlpr.getWritableDatabase(),DBHelper.TBRECIPEE) < 1) {
            mDBHlpr.addRecipee("RiceCrisps","Yucky",10,"This and that");
            mDBHlpr.addRecipee("Bran Flakes","Makes you go",5,"Flakes or something");
        }
    }

    @Override
    public void onFragmentInteraction(Uri uri) {
    }
}

类 StoreEntryFragment :-

public class StoreEntryFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    DBHelper mDBHlpr;
    Cursor mCsr;
    SimpleCursorAdapter mSCA;
    ListView mStoreList;

    public StoreEntryFragment() {
    }

    public static StoreEntryFragment newInstance(String param1, String param2) {
        StoreEntryFragment fragment = new StoreEntryFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_store_entry, container, false);
        mStoreList = view.findViewById(R.id.store_entry_listview);
        mDBHlpr = new DBHelper(getActivity());
        mCsr = mDBHlpr.getStoreEntries();
        mSCA = new SimpleCursorAdapter(
                getActivity(),
                android.R.layout.simple_list_item_2,
                mCsr,
                new String[]{StoreEntry.COLUMN_PRODUCT_NAME,StoreEntry.COLUMN_PRODUCT_PRICE},
                new int[]{android.R.id.text1,android.R.id.text2}
        );
        mStoreList.setAdapter(mSCA);

        return view;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
  • 注意 onCreateView 方法中的代码和预先定义的变量。
  • 这使用带有提供的布局的 SimpleCursorAdapter。
  • 处理是
    1. 根据它的 id 获取 ListView。
    2. 获取 DBHelper 的实例。
    3. 获取此列表的光标。
    4. 使用刚刚检索到的游标创建 SimpleCursorAdapter 的实例。
    5. 将适配器附加(设置)到 ListView。

这是 RecipeeList 的等效项(基本相同):-

public class RecipeeEntryFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    DBHelper mDBHlpr;
    Cursor mCsr;
    SimpleCursorAdapter mSCA;
    ListView mRecipeeList;


    public RecipeeEntryFragment() {
    }

    public static RecipeeEntryFragment newInstance(String param1, String param2) {
        RecipeeEntryFragment fragment = new RecipeeEntryFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_recipee_entry, container, false);
        mRecipeeList = view.findViewById(R.id.recipee_entry_listview);
        mDBHlpr = new DBHelper(getActivity());
        mCsr = mDBHlpr.getRecipeeEntries();
        Log.d("RECIPEEFRAG","Number of rows in Cursor is " + String.valueOf(mCsr.getCount()));
        mSCA = new SimpleCursorAdapter(
                getActivity(),
                android.R.layout.simple_list_item_2,
                mCsr,
                new String[]{RecipeeEntry.COLUMN_RECIPEE_NAME,RecipeeEntry.COLUMN_RECIPEE_TYPE},
                new int[]{android.R.id.text1, android.R.id.text2}
                );
        mRecipeeList.setAdapter(mSCA);
        return view;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

结果是:-

在此处输入图片说明

布局是:-

活动_main.xml :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp">
        <fragment
            android:name="flv.fragmentlistviews.StoreEntryFragment"
            android:id="@+id/store_entry_fragment"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent">
        </fragment>
        <fragment
            android:name="flv.fragmentlistviews.RecipeeEntryFragment"
            android:id="@+id/recipee_entry_fragment"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent">
        </fragment>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp">

    </LinearLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</LinearLayout>

fragment_store_entry.xml :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".StoreEntryFragment"
    android:background="#AA00FF55">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="10"
        android:text="@string/hello_blank_fragment" />

    <ListView
        android:id="@+id/store_entry_listview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="90"
        android:background="#FF00EE33">
    </ListView>
</LinearLayout>

fragment_recipee_entry.xml :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".RecipeeEntryFragment"
    android:background="#AA0055FF">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="10"
        android:text="@string/hello_blank_fragment" />
    <ListView
        android:id="@+id/recipee_entry_listview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="90"
        android:background="#FF0033EE">
    </ListView>
</LinearLayout>

继续

从上面开始,这是介绍您的适配器(根据问题的代码)的一小步,它确实有效。

  • 笔记
    • 由于我使用了 recipee 而不是 recipe,所以做了一些小改动。
    • StoreEntry 和 RecipeeEntry 也进行了修改以包含_IDCONTENT_URI常量。

对于StoreEntryFragment:-

  1. 为 PlatformCursorAdapter 添加另一个类变量:- PlatformCursorAdapter mPCA;

    所以你有:-

    DBHelper mDBHlpr;
    Cursor mCsr;
    SimpleCursorAdapter mSCA;
    ListView mStoreList;
    PlatformCursorAdapter mPCA; //<<<< ADDED
    
  2. onCreateViewMethod之后mStoreList.setAdapter(mSCA);和之前return View添加以下两行:-

    mPCA = new PlatformCursorAdapter(getActivity(),mCsr);
    mStoreList.setAdapter(mPCA);
    

对于RecipeeEntryFragment,您将拥有:-

    DBHelper mDBHlpr;
    Cursor mCsr;
    SimpleCursorAdapter mSCA;
    ListView mRecipeeList;
    RecipeCursorAdapter mRCA; //<<<< ADDED

和 :-

    mRCA = new RecipeCursorAdapter(getActivity(),mCsr);
    mRecipeeList.setAdapter(mRCA);

笔记!使用了以下(非常基本的)布局:-

list_view.xml :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/item_name"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />
    <TextView
        android:id="@+id/item_price"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />
    <TextView
        android:id="@+id/item_quantity"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />
    <TextView
        android:id="@+id/item_weight"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />
    <TextView
        android:id="@+id/item_supplier_name"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />
    <TextView
        android:id="@+id/item_supplier_number"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />
</LinearLayout>

recipe_activity.xml :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/recipe_name"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />
    <TextView
        android:id="@+id/recipe_type"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />
    <TextView
        android:id="@+id/recipe_servings"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />
    <TextView
        android:id="@+id/ingrediant_name_1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />
</LinearLayout>

结果(仅显示部分屏幕)现在是:-

在此处输入图片说明

为颜色道歉,不是最好的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

我们可以使用导出属性在jsp页面中扩展多个类吗

来自分类Dev

我们可以为所有方法编写扩展方法吗?

来自分类Dev

我们可以用具有扩展方法的接口替换抽象类吗?

来自分类Dev

我们可以为所有方法编写扩展方法吗?

来自分类Dev

我们可以用具有扩展方法的接口替换抽象类吗?

来自分类Dev

我们可以获取扩展特定类的所有类吗

来自分类Dev

我们可以编写Visual Studio Code扩展吗?

来自分类Dev

我们可以有多个juju配置文件吗?

来自分类Dev

我们可以有多个juju配置文件吗?

来自分类Dev

我们可以配置具有多个DAO的服务类吗?

来自分类Dev

我们可以在No4j的单个属性键下拥有多个属性吗?

来自分类Dev

我们可以在同一系统上有多个Postgres XL集群吗?

来自分类Dev

我们可以在 gitswarm 的一个项目下有多个存储库吗?

来自分类Dev

我们可以在文件中存储多个对象吗?

来自分类Dev

我们可以在findAll中提供多个东西吗

来自分类Dev

我们可以在代码中执行多个程序吗?

来自分类Dev

我们可以在Django中创建多个视图吗

来自分类Dev

我们可以在文件中存储多个对象吗?

来自分类Dev

我们可以同时过滤多个标签吗

来自分类Dev

我们可以在getstream中获得没有用户ID的所有活动吗

来自分类Dev

如何在Android的ListView中显示sqlite数据库的结果?arrayadapter有可能吗?或cursoradapter?

来自分类Dev

我们可以设置没有密码的根吗?

来自分类Dev

我们可以设置没有密码的根吗?

来自分类Dev

我们可以在 NuSMV 中有终端状态吗?

来自分类Dev

我们可以拿起手腕举起苹果手表的活动吗?

来自分类Dev

我们可以知道启动当前活动的类名吗?

来自分类Dev

我们可以在 Kotlin 单元测试中使用代码调用活动吗

来自分类Dev

我们可以对附加到非活动 FBO 插槽的纹理进行采样吗?

来自分类Dev

我们可以在 jquery 中获取数组中的活动按钮吗?

Related 相关文章

  1. 1

    我们可以使用导出属性在jsp页面中扩展多个类吗

  2. 2

    我们可以为所有方法编写扩展方法吗?

  3. 3

    我们可以用具有扩展方法的接口替换抽象类吗?

  4. 4

    我们可以为所有方法编写扩展方法吗?

  5. 5

    我们可以用具有扩展方法的接口替换抽象类吗?

  6. 6

    我们可以获取扩展特定类的所有类吗

  7. 7

    我们可以编写Visual Studio Code扩展吗?

  8. 8

    我们可以有多个juju配置文件吗?

  9. 9

    我们可以有多个juju配置文件吗?

  10. 10

    我们可以配置具有多个DAO的服务类吗?

  11. 11

    我们可以在No4j的单个属性键下拥有多个属性吗?

  12. 12

    我们可以在同一系统上有多个Postgres XL集群吗?

  13. 13

    我们可以在 gitswarm 的一个项目下有多个存储库吗?

  14. 14

    我们可以在文件中存储多个对象吗?

  15. 15

    我们可以在findAll中提供多个东西吗

  16. 16

    我们可以在代码中执行多个程序吗?

  17. 17

    我们可以在Django中创建多个视图吗

  18. 18

    我们可以在文件中存储多个对象吗?

  19. 19

    我们可以同时过滤多个标签吗

  20. 20

    我们可以在getstream中获得没有用户ID的所有活动吗

  21. 21

    如何在Android的ListView中显示sqlite数据库的结果?arrayadapter有可能吗?或cursoradapter?

  22. 22

    我们可以设置没有密码的根吗?

  23. 23

    我们可以设置没有密码的根吗?

  24. 24

    我们可以在 NuSMV 中有终端状态吗?

  25. 25

    我们可以拿起手腕举起苹果手表的活动吗?

  26. 26

    我们可以知道启动当前活动的类名吗?

  27. 27

    我们可以在 Kotlin 单元测试中使用代码调用活动吗

  28. 28

    我们可以对附加到非活动 FBO 插槽的纹理进行采样吗?

  29. 29

    我们可以在 jquery 中获取数组中的活动按钮吗?

热门标签

归档