简体   繁体   English

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

[英]Get Item Position From Custom Adapter

Since PreferenceFragment is not available in the support library I created a ListFragment to show a list of settings to a user since I don't have a lot of settings to show. 由于PreferenceFragment在支持库中不可用,因此我创建了一个ListFragment来向用户显示设置列表,因为我没有很多要显示的设置。 I also created a custom ArrayAdapter to customize the list items. 我还创建了一个自定义ArrayAdapter来自定义列表项。 I need to handle when a user checks one of the CheckBox s so I can save weather or not it was checked. 当用户检查其中一个CheckBox时我需要处理,这样我就可以保存天气,但是它已被检查。 So if its checked then it will stay checked until the user uncheckes it. 因此,如果检查它,它将保持检查,直到用户取消选中它。 This would be much easier if there was only one setting in the list but there are 2 for now and I might need to add more. 如果列表中只有一个设置,但现在有2个,我可能需要添加更多设置,这会容易得多。 So I need to be able to determine which one was checked. 所以我需要能够确定检查了哪一个。 I can handle the check and uncheck fine I just cant find a way to determine which one was checked. 我可以处理检查并取消选中,我无法找到确定检查哪一个的方法。

THE CODE 编码

Here is my list item: 这是我的清单项目:

<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"/>

Here is getView() in my adapter: 这是我的适配器中的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;
    }

And here is where I handle when a CheckBox is clicked which is in the ListFragment : 这里是我在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;
            }
        }
    }

Here is what my settings look like: 这是我的设置:
在此输入图像描述

WHAT I HAVE TRIED 我做了什么
1. I have tried setting a variable in the adapter to the item position and using a getter to get the position, but that only returns the position of the last item shown. 1.我尝试在适配器中将变量设置到项目位置并使用getter获取位置,但这只返回显示的最后一项的位置。
2. I have tried using some of the methods in ListFragment to get the position of the CheckBox but they always return -1. 2.我尝试使用ListFragment一些方法来获取CheckBox的位置,但它们总是返回-1。
3. I have done a lot of Googling and searching on SO but I have not been able to find a solution to get this to work. 3.我已经做了很多谷歌搜索和搜索,但我找不到解决办法让这个工作。

So if anyone knows of a way I can get the position of the CheckBox or any other ways I might be able to tell which one was clicked I will be eternally grateful. 因此,如果有人知道我可以获得CheckBox的位置或任何其他方式,我可以告诉哪一个被点击,我将永远感激。

You can use setTag to add an int to the view indicating its position, and then retreive that later using getTag . 您可以使用setTagint添加到视图中以指示其位置,然后使用getTag进行getTag检索。 Ex: 例如:

@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));

}

Then, in your onCheckBoxClick : 然后,在你的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.

相关问题 如何从Adapter类中设置自定义gridview获取项目位置? - How to get the item position from custom gridview set in an Adapter class? 如何从AutoCompleteTextView获取适配器项的位置? - How to get adapter item position from AutoCompleteTextView? 如何从自定义适配器获取可过滤的原始Listview项目位置 - How to Get Original Listview Item Position from Custom Adapter Implements Filterable 从自定义适配器获取Hashmap项 - Get Hashmap item from custom adapter 如何使用自定义适配器类获取最后的ListView项目位置? - How to get last ListView item position using Custom Adapter class? 自定义光标适配器(获取位置) - Custom Cursor Adapter ( get position ) 筛选的自定义适配器获取错误的项目位置 - Filtered custom adapter gets wrong item position Android-通过复选框使用自定义适配器从ListView获取项目 - Android - Get item from listview with custom adapter through checkbox 如何使用自定义适配器从 AutoCompleteTextView 获取所选项目 - How to get the selected item from a AutoCompleteTextView with a custom Adapter 如何使用自定义适配器从列表视图上单击的项目中获取文本 - How get text from item clicked on a List view with a Custom Adapter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM