问题描述
我能够选择项目并将其添加到sharepreference.但是,一旦我单击选定的项目,它就不会设置为未选定.当我选择任何标签时,应将其添加到共享首选项"中,而当我点击所选项目时,则应将其从共享首选项"中删除.
I am able to select items and adding to sharepreference. But once I click on selected item Its not set as unselected. When I select any tag it should add in Sharepreference and when I tap on selected item then it should be remove from sharepreferance.
任何其他方法都将非常有帮助,或者请告诉我我的错误:)
Any other way of doing this will be very helpful or please show me my mistake:)
下面是我的代码.
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);
}
}
推荐答案
在这种情况下,我认为您需要通过ArrayAdapter
类进行管理.在这里,您必须记住上次单击的项目的视图和位置.
In this case I think you need to manage it from your ArrayAdapter
class.There you have to remember last clicked item view and position.
基本思想是onListItemClick
将点击的位置和itemview传递到那里的适配器我们将使用您喜欢的颜色将其设置为背景,并更改最后选择的颜色以进行查看背景颜色恢复为默认颜色.
Basic idea is onListItemClick
pass the clicked position and itemview to adapter therewe will set it's background with the color you like and changed the last selected to viewbackground color to default color.
public class SampleAdapter extends ArrayAdapter<Object> {
private int mSelection = 0;
public SampleAdapter(Context context, int resource, int textViewResourceId,
List<Object> objects) {
super(context, resource, textViewResourceId, objects);
}
public void setSelection(int mSelection, View selectedItemView) {
this.mSelection = mSelection;
if (selectedItemView!= null && lastSelectedRow != null
&& selectedItemView!= lastSelectedRow) {
lastSelectedRow
.setBackgroundResource(R.drawable.bg_normal);
selectedItemView
.setBackgroundResource(R.drawable.bg_selected);
}
this.lastSelectedRow = selectedItemView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Usual code logic here....
if (mSelection == position) {
mViewHolder.mRootView
.setBackgroundResource(R.drawable.bg_selected);
lastSelectedRow = mViewHolder.mRootView;
} else {
mViewHolder.mRootView
.setBackgroundResource(R.drawable.bg_normal);
}
return view;
}
private static class ViewHolder {
TextView name;
View mRootView;
}
}
在列表项"上单击,您需要将单击的项和位置传递给适配器.
On List Item click you need to pass clicked item and position to adapter.
public void onItemClick(AdapterView<?> arg0, View listItemView,
int position, long id) {
if(myAdapter != null )
{
myAdapter.setSelection(position,listItemView);
}
}
如果要从其他位置调用此设置选择您可以这样称呼它.
if you want to call this set selection from other pointyou can call it like this.
myAdapter.setSelection(position,null);
mListViwe.setSelectionFromTop(position, 0);
这篇关于Listview选择和取消选择Android项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!