在选定的ListView行中放置复选标记

时间:2014-12-15 17:16:53

标签: java android android-listview android-arrayadapter

我正在尝试在列表视图的选定行中放置复选标记,但复选标记未显示。我对Android很陌生,所以我知道我要么缺少某些东西,要么做错了。

public class SettingsFragment extends Fragment {

    private ListView defaultScreen;
    private static SharedPreferences sharedPref;
    private static SharedPreferences.Editor editor;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View rootView = inflater.inflate(R.layout.fragment_benefits, null);

        Benefits mediaData[] = new Benefits[] {
                new Benefits(R.drawable.ag_news, "Ag News"),
                new Benefits(R.drawable.action_alerts, "Action Alerts"),
                new Benefits(R.drawable.market, "Market Updates"),
                new Benefits(R.drawable.magazine, "KFB Magazine"),
                new Benefits(R.drawable.ag_facts, "Ag Facts"),
                new Benefits(R.drawable.member_benefits, "Member Benefits"),
                new Benefits(R.drawable.roadside_markets, "Roadside Farm Markets"),
                new Benefits(R.drawable.media, "Media"),
                new Benefits(R.drawable.annual_meeting, "Annual Meeting"),
                new Benefits(R.drawable.my_kyfb, "My KYFB"),
        };

        BenefitsAdapter adapter = new BenefitsAdapter(this.getActivity(), R.layout.benefits_item, mediaData);

        defaultScreen = (ListView)rootView.findViewById(R.id.listFeed);
        defaultScreen.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        defaultScreen.setAdapter(adapter);

        defaultScreen.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch(position) {
                    case 0:
                        sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                        editor = sharedPref.edit();
                        editor.putString("DEFAULT", "ag news");
                        editor.apply();

                        defaultScreen.setItemChecked(0, true);
                        break;
                    case 1:
                        sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                        editor = sharedPref.edit();
                        editor.putString("DEFAULT", "action alerts");
                        editor.apply();

                        defaultScreen.setItemChecked(1, true);
                        break;
                    case 2:
                        sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                        editor = sharedPref.edit();
                        editor.putString("DEFAULT", "market updates");
                        editor.apply();

                        defaultScreen.setItemChecked(2, true);
                        break;
                    case 3:
                        sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                        editor = sharedPref.edit();
                        editor.putString("DEFAULT", "kfb magazine");
                        editor.apply();

                        defaultScreen.setItemChecked(3, true);;
                        break;
                    case 4:
                        sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                        editor = sharedPref.edit();
                        editor.putString("DEFAULT", "ag facts");
                        editor.apply();

                        defaultScreen.setItemChecked(4, true);
                        break;
                    case 5:
                        sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                        editor = sharedPref.edit();
                        editor.putString("DEFAULT", "member benefits");
                        editor.apply();

                        defaultScreen.setItemChecked(5, true);
                        break;
                    case 6:
                        sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                        editor = sharedPref.edit();
                        editor.putString("DEFAULT", "roadside markets");
                        editor.apply();

                        defaultScreen.setItemChecked(6, true);
                        break;
                    case 7:
                        sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                        editor = sharedPref.edit();
                        editor.putString("DEFAULT", "media");
                        editor.apply();

                        defaultScreen.setItemChecked(7, true);
                        break;
                    case 8:
                        sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                        editor = sharedPref.edit();
                        editor.putString("DEFAULT", "annual meeting");
                        editor.apply();

                        defaultScreen.setItemChecked(8, true);
                        break;
                    case 9:
                        sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                        editor = sharedPref.edit();
                        editor.putString("DEFAULT", "my kyfb");
                        editor.apply();

                        defaultScreen.setItemChecked(9, true);
                        break;
                    default:
                        break;
                }
            }
        });

        return rootView;
    }
}

这是适配器。

public class BenefitsAdapter extends ArrayAdapter<Benefits> {
    Context context;
    int layoutResourceId;
    Benefits data[] = null;

    public BenefitsAdapter(Context context, int layoutResourceId, Benefits[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        BenefitsHolder holder = null;

        if(row == null) {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new BenefitsHolder();
            holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            holder.textTitle = (TextView)row.findViewById(R.id.textTitle);

            row.setTag(holder);
        }
        else {
            holder = (BenefitsHolder)row.getTag();
        }

        Benefits benefits = data[position];
        holder.textTitle.setText(benefits.title);
        holder.imgIcon.setImageResource(benefits.icon);

        return row;
    }

    static class BenefitsHolder {
        ImageView imgIcon;
        TextView textTitle;
    }
}

fragment_benefits 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:background="@color/plain_background">

    <ListView
        android:id="@+id/listFeed"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fadeScrollbars="true"
        android:fastScrollEnabled="true"
        android:fadingEdge="none"
        android:divider="@color/kfb_blue"
        android:dividerHeight="1dp"
        android:background="@color/plain_background"/>

</RelativeLayout>

benefits_item 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:padding="10dp"
    android:background="@color/plain_background">

    <de.hdodenhof.circleimageview.CircleImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/imgIcon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="15dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        app:border_width="2dp"
        app:border_color="#00000000"/>

    <TextView
        android:id="@+id/textTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:textStyle="normal"
        android:textSize="22dp"
        android:textColor="@color/kfb_blue"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

由于行布局中没有复选标记,因此无法在每行旁边显示复选标记: - )

如果你想要复选标记,你必须使用像android.R.layout.simple_list_item_multiple_choice这样的基本布局并牺牲图像,或者如果你想要一个带有复选标记的自定义布局,你必须在行布局中加上一个复选标记并实现Checkable对于物品。

看看这里:how to set choice mode single for listview with images

希望它有所帮助。