我可以选择项目并添加到共享偏好中。但是,一旦我单击所选项目,它就没有设置为未选中。当我选择任何标签时,它应该添加到 Sharepreference 中,当我点击所选项目时,它应该从 sharepreference 中删除。
这样做的任何其他方式都会非常有帮助,或者请告诉我我的错误:)
下面是我的代码。
public class Filter extends Activity {
ListView filterlist;
ArrayAdapter<String> arrayAdapter;
String tag;
int tagcount = 0;
private ArrayList<String> taglist = new ArrayList<String>();
private ArrayList<String> taglist1 = new ArrayList<String>();
SharedPreferences myPrefs;
private final String TAG_NAME = "tagname";
String prefName, tagged;
SharedPreferences.Editor prefsEditor;
DatabaseHandler db;
Dbresultsummary rs;
ImageAdapter img;
ArrayList< String> preflist = new ArrayList<String>();
String item;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_filter);
myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
prefsEditor = myPrefs.edit();
prefName = myPrefs.getString(TAG_NAME, "No Tag");
rs = new Dbresultsummary(getApplicationContext());
img = new ImageAdapter(this);
filterlist = (ListView) findViewById(R.id.filterlist);
filterlist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
filterlist.setAdapter(img);
filterlist.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
prefName = myPrefs.getString(TAG_NAME, "No Tag");
String s = myPrefs.getString(TAG_NAME, "No Tag");
if (s.equals("No Tag")) {
s = "";
}
else
{
preflist.add(s);
}
int len = filterlist.getCount();
SparseBooleanArray checked = filterlist
.getCheckedItemPositions();
for (int i = 0; i < len; i++) {
if (checked.get(i)) {
item = taglist.get(i);
preflist.add(item);
}
}
HashSet<String> hs = new HashSet();
hs.addAll(preflist);
preflist.clear();
preflist.addAll(hs);
s = preflist.toString().replace("[", "").replace("]", "")
.replace(", ", ",");
if(s.equals(""))
{
s ="No Tag";
}
prefsEditor.putString(TAG_NAME, s);
prefsEditor.commit();
img.notifyDataSetChanged();
}
});
List<Resultsummary> getall = rs.getalltag();
for (Resultsummary cn : getall) {
tag = cn.getTag();
StringTokenizer st = new StringTokenizer(tag, ",");
while (st.hasMoreElements()) {
String temptag = (String) st.nextElement();
taglist1.add(temptag);
HashSet<String> hs = new HashSet();
hs.addAll(taglist1);
taglist1.clear();
taglist1.addAll(hs);
}
}
rs.close();
}
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
String listtag, seltag;
Context mContext;
int count = 0;
public ImageAdapter(Context c) {
mInflater = LayoutInflater.from(c);
}
@Override
public int getCount() {
return taglist1.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.lv, null);
holder = new ViewHolder();
holder.title = (TextView) convertView
.findViewById(R.id.customrow);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
rs = new Dbresultsummary(getApplicationContext());
List<Resultsummary> getall = rs.getalltag();
for (Resultsummary cn : getall) {
tag = cn.getTag();
StringTokenizer st = new StringTokenizer(tag, ",");
while (st.hasMoreElements()) {
String temptag = (String) st.nextElement();
taglist.add(temptag);
HashSet<String> hs = new HashSet();
hs.addAll(taglist);
taglist.clear();
taglist.addAll(hs);
}
}
holder.title.setText(taglist.get(position));
prefName = myPrefs.getString(TAG_NAME, "No Tag");
List<String> prefList = Arrays.asList(prefName.split(","));
for (int k = 0; k < prefList.size(); k++) {
listtag = holder.title.getText().toString();
seltag = prefList.get(k);
if (seltag.equals(listtag)) {
convertView.setSelected(true);
break;
}
else
{
convertView.setSelected(false);
}
}
if (convertView.isSelected()) {
convertView.setBackgroundColor(0x9934B5E4);
} else {
convertView.setBackgroundColor(android.R.color.transparent);
}
rs.close();
return convertView;
}
class ViewHolder {
TextView title;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.filter, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// when you click setting menu
switch (item.getItemId()) {
case R.id.back:
Intent intent = new Intent(this, CompleteResult.class);
startActivity(intent);
return true;
case R.id.cleartag:
String defaulttag = "No Tag";
prefsEditor.putString(TAG_NAME, defaulttag);
prefsEditor.commit();
img.notifyDataSetChanged();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onBackPressed()
{
Intent intent = new Intent(this, CompleteResult.class);
startActivity(intent);
}
}