如何更改不是来自"到"的简单光标适配器中项目的可见性。参数

时间:2018-02-06 09:47:59

标签: android simplecursoradapter

我有一个SimpleCursorAdapter包含一些textViews和ImageViews,他的结构是这样的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@android:color/holo_red_dark"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="10dp"
        android:paddingLeft="10dp"
        android:paddingTop="10dp">
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent">
            <ImageView
                android:id="@+id/ImgViewIconFoodList"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:background="@android:color/transparent"
                android:src="@drawable/ic_spice" />
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/ImgViewIconFoodList"
                android:orientation="vertical"
                android:paddingLeft="10dp">
                <TextView
                    android:id="@+id/txtViewFoodCategoryList"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Category"
                    android:textColor="#949494"
                    android:textSize="12dp" />
                <TextView
                    android:id="@+id/txtViewFoodNameList"
                    style="@style/foodListText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Name"
                    android:textSize="15dp" />
            </LinearLayout>
            <ImageView
                android:id="@+id/imgAddedCart"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="10dp"
                android:adjustViewBounds="false"
                android:visibility="invisible"
app:srcCompat="@drawable/ic_shopping_cart_verified_symbol" />
        </RelativeLayout>
    </LinearLayout>
</LinearLayout>

当我填充大多数项目(textviews,imgviews等等)来自数据库并且在参数&#34; from&#34;和&#34;到&#34;在SimpleCursorAdapter创建中。

但我的问题是当我需要访问(ImageView)id / imgAddedCart时。

此ImageView不在&#34;到&#34;参数,但我需要与此进行交互。 我想根据条件改变他的人物能见度(如果

  

(id_alimento == codigo))

但是我无法做到这一点,因为我不知道如何访问此项目。

有人可以帮助我吗?

非常感谢。

String[] from = new String[]{MyDBAdapter.KEY_NOMBRE,MyDBAdapter.KEY_CAT,MyDBAdapter.KEY_ID_CAT};
    int[] to = new int[]{R.id.txtViewFoodNameList,R.id.txtViewFoodCategoryList,R.id.ImgViewIconFoodList};

    private void fillData() {

        //obtain all codes for principal cursor
        myCursor = mDBHelper.fetchAllCodes();

        //obtain all codes for other cursor to compare with principal
        myShoppingListCursor = mDBHelper.fetchShoppingList();


        getActivity().startManagingCursor(myShoppingListCursor);

        getActivity().startManagingCursor(myCursor);


        SimpleCursorAdapter codes = new SimpleCursorAdapter(rootView.getContext(),R.layout.row,myCursor,from,to,0);

        codes.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
            @Override
            public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                int viewId = view.getId();
                String id_alimento = cursor.getString(cursor.getColumnIndex("_id"));

                if (myShoppingListCursor.moveToFirst()) {
                    //Recorremos el cursor hasta que no haya más registros
                    do {
                        String codigo = myShoppingListCursor.getString(myShoppingListCursor.getColumnIndex("id_alimento"));
                        if (id_alimento == codigo){

                            // HERE I WANNA CHANGE THE @+id/imgAddedCart visibility property to Visible

                        }
                    } while(myShoppingListCursor.moveToNext());
                }
                if (viewId == R.id.txtViewFoodNameList) {((TextView) view).setText(cursor.getString(columnIndex)); }
                if (viewId == R.id.txtViewFoodCategoryList){((TextView) view).setText(cursor.getString(columnIndex));};
                if (viewId == R.id.ImgViewIconFoodList){
                    icName = "ic_oil";
                }

                    Context c = getActivity().getApplicationContext();
                    int id = c.getResources().getIdentifier("drawable/"+icName, null, c.getPackageName());
                    ((ImageView) view).setImageResource(id);
                }
                return true;
            }});
        lv.setAdapter(codes);
    }

0 个答案:

没有答案