自定义ListView在Android Eclipse中设置SqliteDatabase值的背景图像

时间:2015-09-29 14:32:50

标签: java android eclipse sqlite listview

假设我创建了一个自定义listView来显示“图像图标”,“标题”和“副标题”。并且该3 View的值将从sqlite数据库中查询。

这是我的main.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/bg_result"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/listView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:divider="#b5b5b5"
            android:dividerHeight="1dp"
            android:listSelector="@drawable/list_selector" />

    </LinearLayout>

这是我的 custom.xml ,其中我设计了如何从我的ListView组织视图。

<?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:background="@drawable/list_selector"
        android:orientation="horizontal"
        android:padding="5dp" >

        <!--  ListRow Left side image icon-->
        <LinearLayout android:id="@+id/thumbnail" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="3dip"      
            android:layout_alignParentLeft="true"
            android:background="@drawable/image_bg" 
            android:layout_marginRight="5dip">

            <ImageView     
                android:id="@+id/word_icon"   
                android:layout_width="50dip"
                android:layout_height="50dip"
                android:contentDescription="@string/descrip"
                android:src="@drawable/word_icon_p"/>

        </LinearLayout>

        <!-- Title-->
        <TextView
           android:id="@+id/wordTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/thumbnail"
            android:layout_toRightOf="@+id/thumbnail"
            android:text="@string/sample"
            android:textColor="#040404"
            android:typeface="sans" 
            android:textSize="18sp"
            android:textStyle="bold"
            android:layout_marginTop="5dp"/>

        <!-- Subtitle -->
        <TextView
            android:id="@+id/wordSub"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/wordTitle"
            android:layout_below="@+id/wordTitle"
            android:text="@string/sample"
            android:textColor="#343434"
            android:textSize="16sp" />
    </RelativeLayout> 

在我的 Sqlite数据库上,我有以下表和数据

--------------------------------------
= KEY_CODE = KEY_NAME = KEY_SUBTITLE = //My Table
-------------------------------------- 
=  p       = Sam_Tit1 = Sam_Sub1     = // This
=  x       = Sam_Tit2 = Sam_Sub2     = // is the
=  n       = Sam_Tit3 = Sam_Sub3     = // Data Value
======================================

所以,情节是,我想设置

的文本
 -> wordTitle TextView is equal to KEY_NAME,
 -> wordSub TextView is equal to KEY_SUBTITLE, and to set the background image of
 -> word_icon ImageView is equal to KEY_CODE value 

所以listView看起来像

 ====================================
  =            ListView              =
  ====================================
  = word_icon = wordTitle = wordSub  =
  ====================================
  =   p.png   = Sam_Tit1  = Sam_Sub1 =
  =   x.png   = Sam_Tit2  = Sam_Sub2 =
  =   n.png   = Sam_Tit3  = Sam_Sub3 =
  ====================================

现在,这是我的 MainActivity ,我将其命名为 AndroidListViewCursorAdaptorActivity.Java

public class AndroidListViewCursorAdaptorActivity extends Activity {

        private CountriesDbAdapter dbHelper;
        private SimpleCursorAdapter dataAdapter;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            dbHelper = new CountriesDbAdapter(this);
            dbHelper.open();
            displayListView(); // I call this function to set an Adapter for my ListView
        }

        private void displayListView() {
            Cursor cursor = dbHelper.fetchAllCountries();
            // The desired columns to be bound
            String[] columns = new String[] { CountriesDbAdapter.KEY_NAME, CountriesDbAdapter.KEY_SUBTITLE };
            // the XML defined views which the data will be bound to
            int[] to = new int[] { R.id.wordTitle, R.id.wordSub };
            // create the adapter using the cursor pointing to the desired data
            // as well as the layout information
            dataAdapter = new SimpleCursorAdapter(this, R.layout.custom,
                    cursor, columns, to, 0);
            ListView listView = (ListView) findViewById(R.id.listView1);
            // Assign adapter to ListView
            listView.setAdapter(dataAdapter);
        }
    }

所以,一切都很好,所有工作都没有错误,我设置 WordTitle WordSub 的文本并将其绑定到我的 ListView

现在,我的问题是我在设置 ImageView 的背景图像时遇到了一些麻烦,即 word_icon 。我想设置 word_icon 的背景图片 取决于 KEY_CODE 值。我已经知道如何做到这一点。

我尝试使用此代码更新 mainActivity

        private CountriesDbAdapter dbHelper;
        private SimpleCursorAdapter dataAdapter;
        private ImageView im;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            dbHelper = new CountriesDbAdapter(this);
            dbHelper.open();
            displayListView(); // I call this function to set an Adapter for my ListView
        }

        private void displayListView() {
            Cursor cursor = dbHelper.fetchAllCountries();
            // The desired columns to be bound
            String[] columns = new String[] { 
                CountriesDbAdapter.KEY_CODE, //Trying to fetch the KEY_CODE column
                CountriesDbAdapter.KEY_NAME, 
                CountriesDbAdapter.KEY_SUBTITLE };

            // the XML defined views which the data will be bound to
            int[] to = new int[] { 
                R.id.word_icon, //Trying to Put it here
                R.id.wordTitle, 
                R.id.wordSub };
            // create the adapter using the cursor pointing to the desired data
            // as well as the layout information
            dataAdapter = new SimpleCursorAdapter(this, R.layout.custom,
                    cursor, columns, to, 0);
            ListView listView = (ListView) findViewById(R.id.listView1);
            // Assign adapter to ListView
            listView.setAdapter(dataAdapter);
        }

但没有运气,这段代码不起作用,正如我所料,因为我知道我需要设置背景图像,

im.setBackgroundImage(R.Drawable.something_image);

但我已经锁定了想法,需要有人帮助。

所以,就是这样,我希望有人可以帮助我。如果有的话,只要求澄清一下。

谢谢你!

0 个答案:

没有答案