ठीक से मुझे यह काम करने के लिए मिला, ठीक है। मुझे एक कस्टम परिभाषित वर्ग का उपयोग करना था जो ListPreference को बढ़ाता है। उसके अंदर मुझे एक कस्टम एडाप्टर क्लास बनाना था जैसे कि आप ListView के लिए और builder.setAdapter() का उपयोग करके निर्माता को सेट कर सकते थे। मुझे श्रोताओं को दोनों रेडियो बटन और सूची दृश्य पंक्तियों के लिए भी परिभाषित करना पड़ा जो रेडियो बटनों की अनचेकिंग को संभालने में कामयाब रहे। मेरे पास अभी भी एकमात्र समस्याएं हैं, मेरी कस्टम सूची प्रस्तुति में एक ठीक और एक रद्द बटन है जहां ListPreference में केवल रद्द बटन है। मुझे नहीं पता कि ओके बटन को कैसे हटाया जाए। साथ ही, जब मैं नियमित रूप से ListPreference में करता हूं, तो मैं उन पर क्लिक करते समय पंक्तियों को हाइलाइट नहीं कर सकता।
कस्टम ListPreference कक्षा के लिए जावा कोड। अपने पैकेज नाम, वरीयता नाम (कुंजी), सूची प्रविष्टि के लिए अपनी प्रविष्टियां और मान, और अपने एक्सएमएल आइटम्स के नाम जैसी चीजों को ध्यान में रखना सुनिश्चित करें।
package your.package.here;
import java.util.ArrayList;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.preference.ListPreference;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
public class CustomListPreference extends ListPreference
{
CustomListPreferenceAdapter customListPreferenceAdapter = null;
Context mContext;
private LayoutInflater mInflater;
CharSequence[] entries;
CharSequence[] entryValues;
ArrayList<RadioButton> rButtonList;
SharedPreferences prefs;
SharedPreferences.Editor editor;
public CustomListPreference(Context context, AttributeSet attrs)
{
super(context, attrs);
mContext = context;
mInflater = LayoutInflater.from(context);
rButtonList = new ArrayList<RadioButton>();
prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
editor = prefs.edit();
}
@Override
protected void onPrepareDialogBuilder(Builder builder)
{
entries = getEntries();
entryValues = getEntryValues();
if (entries == null || entryValues == null || entries.length != entryValues.length)
{
throw new IllegalStateException(
"ListPreference requires an entries array and an entryValues array which are both the same length");
}
customListPreferenceAdapter = new CustomListPreferenceAdapter(mContext);
builder.setAdapter(customListPreferenceAdapter, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
}
});
}
private class CustomListPreferenceAdapter extends BaseAdapter
{
public CustomListPreferenceAdapter(Context context)
{
}
public int getCount()
{
return entries.length;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
View row = convertView;
CustomHolder holder = null;
if(row == null)
{
row = mInflater.inflate(R.layout.custom_list_preference_row, parent, false);
holder = new CustomHolder(row, position);
row.setTag(holder);
// do whatever you need here, for me I wanted the last item to be greyed out and unclickable
if(position != 3)
{
row.setClickable(true);
row.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
for(RadioButton rb : rButtonList)
{
if(rb.getId() != position)
rb.setChecked(false);
}
int index = position;
int value = Integer.valueOf((String) entryValues[index]);
editor.putInt("yourPref", value);
Dialog mDialog = getDialog();
mDialog.dismiss();
}
});
}
}
return row;
}
class CustomHolder
{
private TextView text = null;
private RadioButton rButton = null;
CustomHolder(View row, int position)
{
text = (TextView)row.findViewById(R.id.custom_list_view_row_text_view);
text.setText(entries[position]);
rButton = (RadioButton)row.findViewById(R.id.custom_list_view_row_radio_button);
rButton.setId(position);
// again do whatever you need to, for me I wanted this item to be greyed out and unclickable
if(position == 3)
{
text.setTextColor(Color.LTGRAY);
rButton.setClickable(false);
}
// also need to do something to check your preference and set the right button as checked
rButtonList.add(rButton);
rButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
{
for(RadioButton rb : rButtonList)
{
if(rb != buttonView)
rb.setChecked(false);
}
int index = buttonView.getId();
int value = Integer.valueOf((String) entryValues[index]);
editor.putInt("yourPref", value);
Dialog mDialog = getDialog();
mDialog.dismiss();
}
}
});
}
}
}
}
मेरी प्राथमिकता के लिए xmlActivity। यह मेरा पूरा एक्सएमएल नहीं है, सादगी के लिए मेरी अन्य प्राथमिकता वस्तुओं को बाहर निकाला। फिर, पैकेज नाम को ध्यान में रखना सुनिश्चित करें, कस्टम सूची संदर्भ श्रेणी को पैकेज नाम से संदर्भित किया जाना चाहिए। वरीयताओं और सरणी नामों के नामों को भी ध्यान रखें जो प्रविष्टियों और मानों को धारण करते हैं।
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Your Title">
<your.package.here.CustomListPreference
android:key="yourPref"
android:title="Your Title"
android:dialogTitle="Your Title"
android:summary="Your Summary"
android:defaultValue="1"
android:entries="@array/yourArray"
android:entryValues="@array/yourValues"/>
</PreferenceCategory>
</PreferenceScreen>
डायलॉग की सूची दृश्य पंक्ति के लिए मेरा एक्सएमएल। GetView विधि में इस xml फ़ाइल के नाम को उस पंक्ति में उपयोग करना सुनिश्चित करें जो इसे बढ़ाता है।
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="8dip"
android:paddingTop="8dip"
android:paddingLeft="10dip"
android:paddingRight="10dip">
<TableLayout
android:id="@+id/custom_list_view_row_table_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0">
<TableRow
android:id="@+id/custom_list_view_row_table_row"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/custom_list_view_row_text_view"
android:textSize="22sp"
android:textColor="#000000"
android:gravity="center_vertical"
android:layout_width="160dip"
android:layout_height="40dip" />
<RadioButton
android:checked="false"
android:id="@+id/custom_list_view_row_radio_button"/>
</TableRow>
</TableLayout>
</LinearLayout>
अंत में, यहां res/values के अंतर्गत मेरा array.xml है जिसमें ListPreference के लिए प्रविष्टि नाम और मान हैं। फिर, सादगी के लिए मेरा छोटा छोटा।
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="yourArray">
<item>Item 1</item>
<item>Item 2</item>
<item>Item 3</item>
<item>Item 4</item>
</string-array>
<string-array name="yourValues">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
</resources>
आप इस कस्टमलिस्ट Preference का उपयोग कैसे कर रहे हैं? क्या आप xml से अपनी वरीयता सक्रियता के लिए ui को बढ़ा रहे हैं? कोड में इसे बनाना? – CaseyB
मैं xml के माध्यम से अपनी वरीयता सक्रियता के लिए प्राथमिकता को परिभाषित कर रहा हूं, और उस XML में मेरे पास एक ऐसा आइटम है जो इस कस्टम सूची है। इस वर्ग को संदर्भित करने के लिए पूर्ण पैकेज नाम का उपयोग करना। – Bob
अपडेट: मुझे एडाप्टर को builder.setSingleChoiceItems() का उपयोग कर काम करने के लिए मिला है और मेरे एडाप्टर में पहले तर्क के रूप में गुजर रहा है। यह विधि कुछ बार ओवरलोड हो गई है, इसलिए यदि आप इसे पढ़ रहे हैं तो इसे दस्तावेज़ में देखें। इसे सब एक साथ बांधने पर काम करना। – Bob