繁体   English   中英

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

[英]Get Item Position From Custom Adapter

由于PreferenceFragment在支持库中不可用,因此我创建了一个ListFragment来向用户显示设置列表,因为我没有很多要显示的设置。 我还创建了一个自定义ArrayAdapter来自定义列表项。 当用户检查其中一个CheckBox时我需要处理,这样我就可以保存天气,但是它已被检查。 因此,如果检查它,它将保持检查,直到用户取消选中它。 如果列表中只有一个设置,但现在有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"/>

这是我的适配器中的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;
    }

这里是我在ListFragment点击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;
            }
        }
    }

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

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

因此,如果有人知道我可以获得CheckBox的位置或任何其他方式,我可以告诉哪一个被点击,我将永远感激。

您可以使用setTagint添加到视图中以指示其位置,然后使用getTag进行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));

}

然后,在你的onCheckBoxClick

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

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM