列表视图选择和取消选择项目Android

时间:2013-10-21 19:18:45

标签: android listview

我可以选择项目并添加到共享偏好。但是,一旦我点击所选项目,它就不会被设置为未选中。当我选择任何标签时,它应该添加到Sharepreference中,当我点击所选项目时,它应该从sharepreferance中删除。

任何其他方式这样做都会非常有帮助,或者请告诉我我的错误:)。

以下是我的代码。

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);
}

}

1 个答案:

答案 0 :(得分:2)

在这种情况下,我认为您需要从ArrayAdapter课程进行管理。 在那里你必须记住最后点击的项目视图和位置。

基本思路是onListItemClick将点击的位置和项目视图传递给那里的适配器 我们将使用您喜欢的颜色设置背景,并更改最后选择的视图 背景颜色为默认颜色。

 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;
    }
}

在列表项目上单击,您需要将单击的项目和位置传递给适配器。

public void onItemClick(AdapterView<?> arg0, View listItemView,
        int position, long id) {

    if(myAdapter != null )
    {
        myAdapter.setSelection(position,listItemView);
    }
}

如果要从其他点调用此设置选择 你可以这样称呼它。

myAdapter.setSelection(position,null);
mListViwe.setSelectionFromTop(position, 0);