使用多个选择项将图像添加到优先列表中 [英] add image to preferance list with multiple selection items

查看:75
本文介绍了使用多个选择项将图像添加到优先列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我创建了列表首选项,其中有多个选项,如文本和复选框,我想添加图片,我的偏好代码在下面。

< br $>






公共类ListPreferenceMultiSelect扩展ListPreference {

私有字符串分隔符;

private static final String DEFAULT_SEPARATOR =bujji;

private static final String LOG_TAG =ListPreferenceMultiSelect;

private String checkAllKey = null;

private boolean [] mClickedDialogEntryIndi​​ces;

String selectedTech =Selected Tech - ;

boolean [] itemsChecked;

// = new boolean [items.length];

private int [] resourceIds = null;

//构造函数

public ListPreferenceMultiSelect(Context context,AttributeSet attrs){

super( context,attrs);

TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.ListPreferenceMultiSelect);

checkAllKey = a.getString(R.styleable.ListPreferenceMultiSelect_checkAll);

String s = a.getString(R.styleable.ListPreferenceMultiSelect_separator);

if(s!= null){

separator = s;



}否则{

分隔符= DEFAULT_SEPARATOR;

} $ / $
//将布尔数组初始化为与条目数相同的大小

mlickedDialogEntryIndi​​ces = new boolean [getEntries()。length];



}



@Override

public void setEntries(CharSequence [] entries){

super.setEntries(entries);

//将布尔数组初始化为与条目数相同的大小

mlickedDialogEntryIndi​​ces = new boolean [entries。长度];

itemsChecked = new boolean [entries.length];



}



public ListPreferenceMultiSelect(Context context){

this(context,null);

}



@Override

protected void onPrepareDialogBu​​ilder(Builder builder){

final CharSequence [] entries = getEntries();

CharSequence [] entryValues = getEntryValues(); <无线电通信/>
if(entries == null || entryValues == null || entries.length!= entryValues.length){

抛出新的IllegalStateException(

ListPreference需要条目数组和entryValues数组,它们都是相同的长度); < br $>
}



restoreCheckedEntries();

builder.setMultiChoiceItems(entries,mClickedDialogEntryIndi​​ces,

new DialogInterface.OnMultiChoiceClickListener(){

public void onClick(DialogInterface dialog,int which boolean val){

if(isCheckAllValue(which)== true){

checkAll(dialog,val);

}

mlickedDialogEntryIndi​​ces [which] = val;

/ * for (int i = 0; i< entries.length; i ++){

if(itemsChecked [i]){



selectedTech = selectedTech + entries [i] +;

它emsChecked [i] = false;

Toast.makeText(getContext(),+ selectedTech,9).show();

}

} * /

//

}



});

}



private boolean isCheckAllValue(int which){

final CharSequence [] entryValues = getEntryValues();

if(checkAllKey!= null){





返回entryValues [which] .equals(checkAllKey);

}

返回false;

}



private void checkAll(DialogInterface对话框,boolean val) {

ListView lv =((AlertDialog)对话框).getListView();

int size = lv.getCount();

for( int i = 0;我<尺寸; i ++){

lv.setItemChecked(i,val);

mlickedDialogEntryIndi​​ces [i] = val;

}

}



public String [] parseStoredValue(CharSequence val){

if(.equals(val)){

返回null;

}

else {

return((String)val).split(separator); < br $>
}

}



private void restoreCheckedEntries(){

CharSequence [] entryValues = getEntryValues();



//分解在共享偏好中读取的字符串

String [] vals = parseStoredValue(getValue());



if(vals!= null){

List< string> valuesList = Arrays.asList(vals);

// for(int j = 0; j< vals.length;> // TODO:检查为什么修剪...可以有一些随机的以某种方式添加空格?如果我们想要一个带尾随空格的值,那么这是一个问题吗?

//字符串val = vals [j] .trim();

for (int i = 0; i< entryvalues.length;> CharSequence entry = entryValues [i];

if(valuesList.contains(entry)){

mlickedDialogEntryIndi​​ces [ i] = true;

}

}

//}

}

}



@Override

protected void onDialogClosed(boolean positiveResult){

// super.onDialogClosed( positiveResult);

ArrayList< string> values = new ArrayList< string>();



CharSequence [] entryValues = getEntryValues();

if(positiveResult&& ; entryValues!= null){

for(int i = 0; i< entryvalues.length;> if(mClickedDialogEntryIndi​​ces [i] == true){

//不要保存检查所有选项的状态 - 如果有的话

String val =(String)entryValues [i];

if(checkAllKey == null ||(val) .equals(checkAllKey)== false)){

values.add(val);

Toast.makeText(getContext(),+ values,9)。 show();

}

}

}



if(callChangeListener (值)){

setValue(join(values,separator));

}

}

}



//在这篇文章中向kurellajunior致记http://snippets.dzone.com/posts/show/91

protected static String join(Iterable<?extends Object> pColl,String separator)

{

Iterator<? extends Object> oter;

if(pColl == null ||(!(oIter = pColl.iterator())。hasNext()))

return;

StringBuilder oBuilder = new StringBuilder(String.valueOf(oIter.next()));

while(oIter.hasNext())

oBuilder.append (分隔符).append(oIter.next());

返回oBuilder.toString();

}



// TODO:想保持静态,但需要手动放入分隔符或使用默认分隔符OV = I = XseparatorX = I = VO......



public static boolean contains(String straw,String haystack,String separator){

if(separator == null){

separator = DEFAULT_SEPARATOR;

}

String [] vals = haystack.split(separator);

for(int i = 0; i< vals.length; > if(vals [i] .equals(straw)){

返回true;

}

}

返回false;

}

}

Hi All,
I have create list preference with multiple selection item like text and check box i want to add image to that my preference code is below.




public class ListPreferenceMultiSelect extends ListPreference {
private String separator;
private static final String DEFAULT_SEPARATOR = "bujji";
private static final String LOG_TAG = "ListPreferenceMultiSelect";
private String checkAllKey = null;
private boolean[] mClickedDialogEntryIndices;
String selectedTech="Selected Tech - ";
boolean[] itemsChecked ;
//= new boolean[items.length];
private int[] resourceIds = null;
// Constructor
public ListPreferenceMultiSelect(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ListPreferenceMultiSelect);
checkAllKey = a.getString( R.styleable.ListPreferenceMultiSelect_checkAll );
String s = a.getString(R.styleable.ListPreferenceMultiSelect_separator );
if( s != null ) {
separator = s;

} else {
separator = DEFAULT_SEPARATOR;
}
// Initialize the array of boolean to the same size as number of entries
mClickedDialogEntryIndices = new boolean[getEntries().length];

}

@Override
public void setEntries(CharSequence[] entries) {
super.setEntries(entries);
// Initialize the array of boolean to the same size as number of entries
mClickedDialogEntryIndices = new boolean[entries.length];
itemsChecked = new boolean[entries.length];

}

public ListPreferenceMultiSelect(Context context) {
this(context, null);
}

@Override
protected void onPrepareDialogBuilder(Builder builder) {
final CharSequence[] entries = getEntries();
CharSequence[] 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");
}

restoreCheckedEntries();
builder.setMultiChoiceItems(entries, mClickedDialogEntryIndices,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int which, boolean val) {
if( isCheckAllValue( which ) == true ) {
checkAll( dialog, val );
}
mClickedDialogEntryIndices[which] = val;
/* for (int i = 0; i < entries.length; i++) {
if (itemsChecked[i]) {

selectedTech=selectedTech+entries[i]+" ";
itemsChecked[i]=false;
Toast.makeText(getContext(), ""+selectedTech, 9).show();
}
}*/
//
}

});
}

private boolean isCheckAllValue( int which ){
final CharSequence[] entryValues = getEntryValues();
if(checkAllKey != null) {


return entryValues[which].equals(checkAllKey);
}
return false;
}

private void checkAll( DialogInterface dialog, boolean val ) {
ListView lv = ((AlertDialog) dialog).getListView();
int size = lv.getCount();
for(int i = 0; i < size; i++) {
lv.setItemChecked(i, val);
mClickedDialogEntryIndices[i] = val;
}
}

public String[] parseStoredValue(CharSequence val) {
if ( "".equals(val) ) {
return null;
}
else {
return ((String)val).split(separator);
}
}

private void restoreCheckedEntries() {
CharSequence[] entryValues = getEntryValues();

// Explode the string read in sharedpreferences
String[] vals = parseStoredValue(getValue());

if ( vals != null ) {
List<string> valuesList = Arrays.asList(vals);
// for ( int j=0; j<vals.length;>// TODO: Check why the trimming... Can there be some random spaces added somehow? What if we want a value with trailing spaces, is that an issue?
// String val = vals[j].trim();
for ( int i=0; i<entryvalues.length;> CharSequence entry = entryValues[i];
if ( valuesList.contains(entry) ) {
mClickedDialogEntryIndices[i] = true;
}
}
// }
}
}

@Override
protected void onDialogClosed(boolean positiveResult) {
// super.onDialogClosed(positiveResult);
ArrayList<string> values = new ArrayList<string>();

CharSequence[] entryValues = getEntryValues();
if (positiveResult && entryValues != null) {
for ( int i=0; i<entryvalues.length;> if ( mClickedDialogEntryIndices[i] == true ) {
// Don't save the state of check all option - if any
String val = (String) entryValues[i];
if( checkAllKey == null || (val.equals(checkAllKey) == false) ) {
values.add(val);
Toast.makeText(getContext(), ""+values, 9).show();
}
}
}

if (callChangeListener(values)) {
setValue(join(values, separator));
}
}
}

// Credits to kurellajunior on this post http://snippets.dzone.com/posts/show/91
protected static String join( Iterable< ? extends Object > pColl, String separator )
{
Iterator< ? extends Object > oIter;
if ( pColl == null || ( !( oIter = pColl.iterator() ).hasNext() ) )
return "";
StringBuilder oBuilder = new StringBuilder( String.valueOf( oIter.next() ) );
while ( oIter.hasNext() )
oBuilder.append( separator ).append( oIter.next() );
return oBuilder.toString();
}

// TODO: Would like to keep this static but separator then needs to be put in by hand or use default separator "OV=I=XseparatorX=I=VO"...

public static boolean contains( String straw, String haystack, String separator ){
if( separator == null ) {
separator = DEFAULT_SEPARATOR;
}
String[] vals = haystack.split(separator);
for( int i=0; i<vals.length;> if(vals[i].equals(straw)){
return true;
}
}
return false;
}
}

推荐答案

这篇关于使用多个选择项将图像添加到优先列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆