Android自定义适配器 - 相对于Listview

时间:2017-10-05 08:12:13

标签: java android listview android-sqlite

我目前正在努力使用我希望在我的应用程序中使用的功能。用户收入显示在ListView中。有两种类型的收入,正常的“收入”和“其他收入”,当用户将收入提交给他或她收到的SQLite数据库时,在应用程序中选择这种收入。现在 - 我想在ListView中显示不同的收入,其中ListView中的图像相对于类别的类型(两种类型的收入类别 - “收入”和“其他收入”。

我无法弄清楚我应该怎么做...你能做某种转换声明吗?? 我的自定义ListView的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"
              android:weightSum="100">

    <ImageView
        android:orientation="vertical"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:gravity="center_vertical"
        android:id="@+id/icon1"/>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="220dp"
        android:layout_height="48dp"
        android:layout_weight="33.3">

        <TextView
            android:text="TextView1"
            android:textSize="18dp"
            android:gravity="center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:id="@+id/text1"/>

        <TextView
            android:text="Textview2"
            android:textSize="14dp"
            android:gravity="center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:="@id/text2"/>

    </LinearLayout>

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

        <TextView
            android:text="Textview3"
            android:textSize="16dp"
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:layout_gravity=""
            android:gravity="center_vertical"
            android:id="@+id/text3"/>

    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="48dp"
        android:layout_height="48dp">

        <ImageView
            android:orientation="vertical"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:gravity="center_vertical"
            android:id="@+id/icon2"/>


    </LinearLayout>

</LinearLayout>

我的AdapterClass看起来像这样:

public class CustomIncomeListAdapter extends ArrayAdapter<IncomeModel> {

    private ArrayList<IncomeModel> incomeList;
    private Activity context;
    private ViewHolder viewHolder;
    int[] incomeIcon = {R.drawable.income, R.drawable.otherincome};
    int[] forwardIcon = {R.drawable.forward};

    public CustomIncomeListAdapter(Activity context, int[] incomeIcon, ArrayList<IncomeModel> incomeList) {
        super(context, R.layout.adapter_view_layout, incomeList);
        this.incomeList = incomeList;
        this.context = context;
        this.incomeIcon = incomeIcon;

    }

    private static class ViewHolder {
        TextView incomeReference;
        TextView incomeDate;
        TextView incomeAmount;
        ImageView incomeIcon;
        ImageView forwardIcon;

    }

    public View getView(int position, View convertView, ViewGroup parent) {
        IncomeModel incomeModel = getItem(position);

        if (convertView == null) {
            viewHolder = new ViewHolder();
            LayoutInflater inflater = context.getLayoutInflater();
            convertView = inflater.inflate(R.layout.adapter_view_layout, null, true);
            viewHolder.incomeReference = (TextView) convertView.findViewById(R.id.text1);
            viewHolder.incomeDate = (TextView) convertView.findViewById(R.id.text2);
            viewHolder.incomeAmount = (TextView) convertView.findViewById(R.id.text3);
            viewHolder.incomeIcon = (ImageView) convertView.findViewById(R.id.icon1);
            viewHolder.forwardIcon = (ImageView) convertView.findViewById(R.id.icon2);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.incomeReference.setText(incomeModel.getIncomeCategory());
        viewHolder.incomeDate.setText(incomeModel.getIncomeDate());
        viewHolder.incomeAmount.setText(incomeModel.getIncomeAmount());
        switch (...?) {

    }
} 

2 个答案:

答案 0 :(得分:0)

viewHolder.incomeAmount.setText(incomeModel.getIncomeAmount());
switch (income) {
        //define your income to 1 for other income
        case 1:
            incomeIcon.setBackgroundResource(R.drawable.otherincome);
            break;
        default:
            incomeIcon.setBackgroundResource(R.drawable.income);
            break;
    }

答案 1 :(得分:0)

你可以检查类型,如果类型是收入意味着设置相应的图像,否则设置其他图像