1

我想创建一个列表,其中列表视图将显示文本视图和每行的图标。该图应如下所示:

在此处输入图像描述

除此之外,数据是从数据库中检索的。将首先检查“favorite”的属性,如果为真,则列表视图中的图像将被分配到 favorite_icon.png。否则,无需分配图标。

我一直在寻找相关的答案和教程,但是它们都与我想要的相差太大,我不太了解。希望这里有人可以帮助我。先感谢您。

4

1 回答 1

1

这是我完成作业后得到的

首先,我们为列表视图以及我们稍后将使用的列表视图的行设置自定义布局。

这是 custom_listview_main.xml

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

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/customListView" />

    </LinearLayout>

这是 custom_listview_row.xml

     <?xml version="1.0" encoding="utf-8"?>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:minHeight="64dp">

    <ImageView
        android:id="@+id/clv_imageView"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:contentDescription="@string/empty"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="9dp"
        android:layout_alignParentTop="true"/>

    <TextView
    android:id="@+id/clv_textView"
    android:layout_width="97dp"
    android:layout_height="32dp"
    android:layout_alignParentLeft="true"
    android:gravity="center_vertical"
    android:text="@string/tv_definition"
    android:textIsSelectable="true" />


    </RelativeLayout>

然后我们需要自定义我们的自定义 ArrayAdaptor 的外观和行为。但在此之前,请确保您已将图标放入可绘制文件夹中。

这是我的 MyPerformanceArrayAdapter.java

    import java.util.List;

    import android.app.Activity;
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;

    public class MyPerformanceArrayAdapter extends ArrayAdapter<DefinitionObject>{
    private List<DefinitionObject> entries;
    private Activity activity;

    public MyPerformanceArrayAdapter(Activity a, int textViewResourceId, List<DefinitionObject> entries) {
    super(a, textViewResourceId, entries);
    this.entries = entries;
    this.activity = a;
    }

    public static class ViewHolder{
    public TextView item1;
    public ImageView item2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    ViewHolder holder;
    if (v == null) {
        LayoutInflater vi =
            (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.custom_listview_row, null);
        holder = new ViewHolder();
        holder.item1 = (TextView) v.findViewById(R.id.clv_textView);
        holder.item2 = (ImageView) v.findViewById(R.id.clv_imageView);
        v.setTag(holder);
    }
    else
        holder=(ViewHolder)v.getTag();

    final DefinitionObject custom = entries.get(position);
    if (custom != null) {
        holder.item1.setText(custom.getWord());
        if(custom.getFav().equalsIgnoreCase("0"))
        {
            holder.item2.setImageResource(R.drawable.fav);
            holder.item2.setVisibility(holder.item2.INVISIBLE);
        }
        else
        {
            holder.item2.setImageResource(R.drawable.fav2);
        }
    }
    return v;
    }

    }

最后,这是我使用 CustomArrayAdaptor 查看 ListView 的 Activity。

    import java.util.List;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.ListView;

    public class TempCLV extends Activity {

private MySQLiteDefinitionHelper db;
String tblName = "";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.custom_listview_main);

    Intent msjIntent = getIntent();
    tblName = msjIntent.getStringExtra(WordDefinitionHomeActivity.TABLENAME2);
    refresh();
    }

    public void refresh()
    {
    db = new MySQLiteDefinitionHelper(this);
    final List<DefinitionObject> values = db.getAllWords(tblName);

    ListView mylist = (ListView)findViewById(R.id.customListView);
    MyPerformanceArrayAdapter adapter = new MyPerformanceArrayAdapter(this, R.id.customListView, values);
    mylist.setAdapter(adapter);
    }
    }
于 2013-11-02T10:35:55.567 回答