从自定义适配器获取项目位置

Jas*_*sby 3 android android-preferences android-arrayadapter android-listfragment android-support-library

由于PreferenceFragment在支持库中没有提供,因此我创建了一个ListFragment向用户显示设置列表,因为我没有很多要显示的设置.我还创建了一个自定义ArrayAdapter来自定义列表项.当用户检查其中一个CheckBoxs 时我需要处理,以便我可以保存天气或者不检查它.因此,如果检查它,它将保持检查,直到用户取消选中它.如果列表中只有一个设置,但现在有2个,我可能需要添加更多设置,这会容易得多.所以我需要能够确定检查了哪一个.我可以处理检查并取消选中,我无法找到确定检查哪一个的方法.

代码

这是我的清单项目:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"
                      android:orientation="horizontal">

<TextView android:id="@+id/pref_edit_text" android:layout_width="0dp" android:layout_height="30dp"
              android:layout_weight="5"/>
    <CheckBox android:id="@+id/pref_check_box" android:layout_width="0dp" android:layout_height="wrap_content"
              android:layout_weight="1" android:onClick="onCheckBoxClick"/>
    </LinearLayout>

<TextView android:id="@+id/pref_edit_text2" android:layout_width="match_parent"
              android:layout_height="50dp"/>
Run Code Online (Sandbox Code Playgroud)

这是getView()我的适配器:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //mIntCheckBoxPosition = position;
        Typeface tf = Typeface.createFromAsset(mMainActivity.getAssets(), "fonts/ArchitectsDaughter.ttf");
        LayoutInflater inflater = mMainActivity.getLayoutInflater();
        View view = inflater.inflate(mIntLayoutId, parent, false);

        TextView text = (TextView) view.findViewById(R.id.pref_edit_text);
        text.setText(mStringArrayTitle[position]);
        text.setTypeface(tf, Typeface.BOLD);

        text = (TextView) view.findViewById(R.id.pref_edit_text2);
        text.setText(mStringArraySubTitle[position]);
        text.setTypeface(tf);

        mMainActivity.setTitle("Settings");

        return view;
    }
Run Code Online (Sandbox Code Playgroud)

这是我在CheckBox点击时所处理的位置ListFragment:

    public void onCheckBoxClick(View view) {
        boolean isChecked = ((CheckBox) view).isChecked();
        Editor editor = mMainActivity.getSharedPreferences(PREF_KEY_CHECK_BOX, Activity.MODE_PRIVATE).edit();

        switch (view.getId()) {
            case R.id.check_box :
                if (isChecked) {
                    editor.putBoolean(PREF_KEY_ROUNDING, true).commit();
                }
                else {
                    editor.putBoolean(PREF_KEY_ROUNDING, false).commit();
                }
                break;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是我的设置:
在此输入图像描述

我做了什么
1.我尝试在适配器中将变量设置到项目位置并使用getter获取位置,但这只返回显示的最后一项的位置.
2.我尝试使用一些方法ListFragment获取位置,CheckBox但它们总是返回-1.
3.我已经做了很多谷歌搜索和搜索,但我找不到解决办法让这个工作.

因此,如果有人知道我可以通过某种方式获得CheckBox其他方式的位置,我可能会知道哪一个被点击了,我将永远感激不尽.

Tus*_*har 11

您可以使用setTagint视图添加指示其位置的视图,然后使用后续检索getTag.例如:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ... // your other code here

    CheckBox checkbox = (CheckBox) view.findViewById(R.id.pref_check_box);
    checkbox.setTag(new Integer(position));

}
Run Code Online (Sandbox Code Playgroud)

然后,在你的onCheckBoxClick:

public void onCheckBoxClick(View view) {
    Integer pos;
    pos = (Integer) view.getTag();

    ... // do what you want with `pos` here

}
Run Code Online (Sandbox Code Playgroud)