本文介绍了如何在共享首选项中存储Listitems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有2个名为labels和values的数组我将这些元素发送到适配器类并在listview上设置它。现在我希望所有这些值都保存在共享首选项中,并且它应该是可编辑的更新更改。如何在listview中使用Sharedpreferences我没有得到。 public class MyProfile_Fragment extends 片段 implements View.OnClickListener { ArrayList< string>标签; ArrayList< string>值; ArrayList< SelectUser> selectUsers = null; ProfileAdapter适配器; ListView listView; 按钮editbtn; Context _c = getActivity(); 查看个人资料; SelectUser cat; int clickCount = 0 ; @覆盖 public 查看onCreateView(LayoutInflater inflater,ViewGroup) container, Bundle savedInstanceState){ profile = inflater.inflate(R.layout.profile_layout,container,false); selectUsers = new ArrayList< SelectUser>(); editbtn =(Button)profile.findViewById(R.id.edit); listView =(ListView)profile.findViewById(R.id.profileList); label = new ArrayList< string>(); label.add( Fname); label.add( Lname); label.add( Industry); label.add( 公司); label.add( Address); values = new ArrayList< string>(); values.add( abc); values.add( M); values.add( IT); values.add( XYZ); values.add( qwerty); for ( int i = 0 ; i< = label.size(); i ++){ SelectUser selectUser = new SelectUser(); selectUser.setLabel(label.get(i)); String val = values.get(i); selectUser.setValues( String .valueOf(values.get(i)); selectUsers.add(selectUser); } adapter = new ProfileAdapter(getActivity(),selectUsers); listView.setAdapter(adapter); // on edit clickbutton通过调用actv()方法启用或禁用编辑文本 editbtn.setOnClickListener( new View.OnClickListener(){ @ Override public void onClick(查看视图){ if (clickCount == 0 ){ editbtn.setText( 保存 ); adapter.actv(true); adapter.notifyDataSetChanged(); listView.setAdapter(adapter); clickCount = 1 ; } else if (clickCount == 1 ){ editbtn.setText( 编辑); adapter.actv(false); adapter.notifyDataSetChanged(); listView.setAdapter(adapter); Toast.makeText(getActivity(), 点击1,Toast.LENGTH_LONG)。节目(); clickCount = 0 ; } } }); 返回个人资料; } // 采用者类:处理列表项 类 ProfileAdapter extends BaseAdapter { ViewHolder holder = new ViewHolder(); public ArrayList& lt; SelectUser> groupItem; int flag = 0; public ProfileAdapter(Context context,ArrayList& lt; SelectUser> group){ groupItem = group; _c = context; } @覆盖 public int getCount(){ return groupItem.size(); } @覆盖 public SelectUser getItem ( int i){ return groupItem.get(i); } @覆盖 public long getItemId( int i){ return i; } @ Override public 查看getView(最终 int 位置, final 查看convertView,ViewGroup父级){ 查看v = convertView; if (v == null){ LayoutInflater inflater =(LayoutInflater)_c.getSystemService (Context。 LAYOUT_INFLATER_SERVICE); v = inflater.inflate(R.layout.profile_listitems,parent,false); v.setTag(持有人); } else { holder =(ViewHolder)v.getTag(); } holder.mLable =(TextView)v.findViewById(R.id.label); holder.mValues =(EditText)v.findViewById(R.id.values); flag = position; cat = groupItem.get(flag); holder.mLable.setText(cat.getLabel()); holder.mValues.setText(cat.getValues()); holder.mValues.addTextChangedListener( new GenericTextWatcher(holder.mValues)); if (clickCount == 0) {holder.mValues.setEnabled(false) ; actv(false); } return v; } 受保护 void actv( final boolean active){ holder.mValues.setEnabled(active); if (有效){ holder.mValues.requestFocus(); } } } class ViewHolder { TextView mLable; EditText mValues; } // 声明 private class GenericTextWatcher implements TextWatcher { 私人查看视图; private GenericTextWatcher(查看视图){ 此 .view = view; } public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2){ } public void onTextChanged(CharSequence charSequence, int i, int i1, int i2){ } public void afterTextChanged(可编辑的可编辑){ String text = editable.toString(); switch (view.getId()){ case R.id.值: cat.setValues(text); break ; } } } } 解决方案 你可以循环遍历ArrayAdapter的元素并将它们存储到 SharedPreferences 中,如下所示: SharedPreferences.Editor editor = prefs.edit(); // 这将删除特定SharedPreferences的所有条目 editor.clear (); for ( int i = 0 ; i < adapter.getCount(); ++ i){ // 这假设您只有SharedPreferences中的列表项。 editor.putString( String .valueOf(i),adapter.getItem(i)); } editor.commit(); 另见: Android:如何在SharedPreferences中存储值列表Android开放教程 [ ^ ] I have 2 arrays named "labels" and "values" I am sending these elements to an adapter class and setting it on the listview. Now I want all these values to be saved in shared preferences and also it should be editable an update the changes. how to use Sharedpreferences in listview I am not getting.public class MyProfile_Fragment extends Fragment implements View.OnClickListener{ ArrayList<string> label; ArrayList<string> values; ArrayList<SelectUser> selectUsers = null; ProfileAdapter adapter; ListView listView; Button editbtn; Context _c=getActivity(); View profile; SelectUser cat; int clickCount = 0; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { profile = inflater.inflate(R.layout.profile_layout, container, false); selectUsers = new ArrayList<SelectUser>(); editbtn = (Button) profile.findViewById(R.id.edit); listView = (ListView) profile.findViewById(R.id.profileList); label = new ArrayList<string>(); label.add("Fname"); label.add("Lname"); label.add("Industry"); label.add("Company"); label.add("Address"); values = new ArrayList<string>(); values.add("abc"); values.add("M"); values.add("IT"); values.add("XYZ"); values.add("qwerty"); for (int i = 0; i<= label.size(); i++) { SelectUser selectUser = new SelectUser(); selectUser.setLabel(label.get(i)); String val=values.get(i); selectUser.setValues(String.valueOf(values.get(i)); selectUsers.add(selectUser); } adapter = new ProfileAdapter(getActivity(), selectUsers); listView.setAdapter(adapter);// on edit clickbutton enable or disable the edit text by calling actv() method editbtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (clickCount == 0) { editbtn.setText("Save"); adapter.actv(true); adapter.notifyDataSetChanged(); listView.setAdapter(adapter); clickCount = 1; } else if (clickCount == 1) { editbtn.setText("Edit"); adapter.actv(false); adapter.notifyDataSetChanged(); listView.setAdapter(adapter); Toast.makeText(getActivity(), "click 1", Toast.LENGTH_LONG).show(); clickCount = 0; } } }); return profile; } // Adopter class : handle list items class ProfileAdapter extends BaseAdapter { ViewHolder holder = new ViewHolder(); public ArrayList<SelectUser> groupItem; int flag=0; public ProfileAdapter(Context context, ArrayList<SelectUser> group) { groupItem = group; _c = context; } @Override public int getCount() { return groupItem.size(); } @Override public SelectUser getItem(int i) { return groupItem.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(final int position, final View convertView, ViewGroup parent) { View v = convertView; if (v == null) { LayoutInflater inflater = (LayoutInflater) _c.getSystemService (Context.LAYOUT_INFLATER_SERVICE); v = inflater.inflate(R.layout.profile_listitems, parent, false); v.setTag(holder); } else { holder = (ViewHolder) v.getTag(); } holder.mLable = (TextView) v.findViewById(R.id.label); holder.mValues = (EditText) v.findViewById(R.id.values); flag=position; cat = groupItem.get(flag); holder.mLable.setText(cat.getLabel()); holder.mValues.setText(cat.getValues()); holder.mValues.addTextChangedListener(new GenericTextWatcher(holder.mValues)); if(clickCount==0) {holder.mValues.setEnabled(false); actv(false); } return v; } protected void actv(final boolean active) { holder.mValues.setEnabled(active); if (active) { holder.mValues.requestFocus(); } } } class ViewHolder { TextView mLable; EditText mValues; } //Declaration private class GenericTextWatcher implements TextWatcher { private View view; private GenericTextWatcher(View view) { this.view = view; } public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } public void afterTextChanged(Editable editable) { String text = editable.toString(); switch (view.getId()) { case R.id.values: cat.setValues(text); break; } } }} 解决方案 You can loop through the elements of your ArrayAdapter and store them to your SharedPreferences as like:SharedPreferences.Editor editor = prefs.edit();// This will remove all entries of the specific SharedPreferenceseditor.clear();for (int i = 0; i < adapter.getCount(); ++i){ // This assumes you only have the list items in the SharedPreferences. editor.putString(String.valueOf(i), adapter.getItem(i));}editor.commit();Also see:Android: How to store list of values in SharedPreferences | Android Open Tutorials[^] 这篇关于如何在共享首选项中存储Listitems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 06-30 01:59