根据android中的“值”获取共享首选项“Key”

时间:2016-09-21 21:25:09

标签: android sharedpreferences

我要做的是从android中的listview中删除该行。我成功地做到了这一点。但现在我的问题是当我从ListView删除行值的同时我想从共享首选项中删除值和键。

  

我想怎么做:当我从列表中删除行值时。我想获得该行的值并检查我的共享首选项中的值。如果那个“价值”在那里,那么我将根据“价值”获得“关键”。我知道有一种方式:sharedPrefEditor.remove(key);但是我在想如果有相关的东西:sharedPrefEditor.remove(“key”,“Value”),那么它会很棒,因为这样我可以轻松删除“特定”键/值对。我希望你们好吗

提示:我根据TimeStamp保存了共享首选项的“密钥”,即我保存“密钥”的时间。

随时随地向我发出任何问题。 谢谢你。

编辑:当我向ARRAYLIST添加价值时,这是代码。

 // get the list values from SharedPreferences
        Map<String, ?> allEntries = MapActivity_sp.getAll();
        for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
            if (entry.getKey().toString().contains("passedAddressValue")) {
                if (getpassedAddressValue.contains(entry.getValue().toString())) {
                    //nothing
                } else
                    getpassedAddressValue.add(entry.getValue().toString());

            }
        }
        adapter = new InflateLocations(this, getpassedAddressValue, lv);
        lv.setAdapter(adapter);

3 个答案:

答案 0 :(得分:0)

解决此问题的一种方法是在适配器中填充列表视图时,将密钥作为标记添加到每个视图中。然后,当您要删除行时,可以检索该列表项的标记,这将是您需要删除的密钥。

编辑:根据您的示例代码,我建议修改您的InflateLocations适配器以检索给定位置的密钥:

List<String> keys = new ArrayList<>();
Map<String, ?> allEntries = MapActivity_sp.getAll();
for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
    if (entry.getKey().toString().contains("passedAddressValue")) {
        if (!getpassedAddressValue.contains(entry.getValue().toString())) {
            getpassedAddressValue.add(entry.getValue().toString());
            keys.add(entry.getKey());
        }
    }
}
adapter = new InflateLocations(this, getpassedAddressValue, keys, lv);
lv.setAdapter(adapter);

然后在InflateLocations中,将keys存储在字段中(例如mKeys)并添加方法:

public class InflateLocations extends ... {
    private List<String> mKeys; // initialize in constructor
    ...

    public String getKey(int position) {
        return mKeys.get(position);
    }
}

当您处理列表项单击时,您可以通过查询适配器来检索密钥:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    InflateLocations adapter = (InflateLocations) parent.getAdpater();
    String key = adapter.getKey(position).toString();
    // process key
}

答案 1 :(得分:0)

{  rowView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            stringAdd.remove(position);
            notifyDataSetChanged();
            return true;
        }
    });}

答案 2 :(得分:0)

@TedHopp:这是我的充气机类的完整代码。请检查一下。我也包含了getKey()方法。但我不这么认为它是有意义的,因为它超出了getView()方法。

    package c.silent.it.keep.keepitsilent;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class InflateLocations extends BaseAdapter {
    ArrayList<String> stringAdd;
    private int selectedPosition = -1;
    String[] stringMd;
    SharedPreferences sharedpreferences;
    SharedPreferences.Editor editSP;
    Context context;
    ListView listview;
    private List<String> mKeys;
    // int [] imageId;
    private static LayoutInflater inflater = null;

    //  public InflateLocations(List_Locations mainActivity, ArrayList<String> stringAddress, String[] stringMode, ListView lv) {
    public InflateLocations(List_Locations mainActivity, ArrayList<String> stringAddress, List<String> keys, ListView lv) {
        // TODO Auto-generated constructor stub
        stringAdd = stringAddress;
        context = mainActivity;
        mKeys=keys;
        listview = lv;
        //stringMd = stringMode;
        inflater = (LayoutInflater) context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());

    }



    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return stringAdd.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public class Holder {
        TextView modeTv;
        TextView addresssTextview;
        // CheckBox switchForService;

        // ImageView img;
    }

    @Override
    public View getView(final int position, final View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final Holder holder = new Holder();
        final View rowView;
        rowView = inflater.inflate(R.layout.inflate_list_locations, null);
        holder.modeTv = (TextView) rowView.findViewById(R.id.modeTextView);
        // holder.switchForService = (CheckBox) rowView.findViewById(R.id.switchForService);
        holder.addresssTextview = (TextView) rowView.findViewById(R.id.addresssTextview);
        holder.addresssTextview.setText(stringAdd.get(position));


        rowView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                editSP = sharedpreferences.edit();
                editSP.putInt("listItemPosition", position);
                editSP.putString("SelectedAddress", stringAdd.get(position));
                editSP.apply();
                Intent startactivity = new Intent(context.getApplicationContext(), ServiceButton.class);
                context.startActivity(startactivity);
            }
        });

        rowView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                stringAdd.remove(position);
                notifyDataSetChanged();
                return true;
            }
        });

        return rowView;

    }
    public String getKey(int position) {
        return mKeys.get(position);
    }


}