[英]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
. 您可以使用setTag
将int
添加到视图中以指示其位置,然后使用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.