我有一个FragmentStatePager
,其中包含两个EditText
和一个自定义ListView
。
用户可以输入两个输入并单击Button
,它们将添加到ListView
,但不会存储在ListView
中。ListView
包含ArrayList
。
我希望EditText
的输入存储在ArrayList
中。
有人可以写出怎么做吗?
我的 onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.swipeview, container, false);
txtTitel = (TextView) rootView.findViewById(R.id.swipeviewtitle);
// Capture position and set to the TextViews
txtTitel.setText(titel[position]);
// Locate the ImageView in viewpager_item.xml
image = (ImageView) rootView.findViewById(R.id.swipeViewimage);
// Capture position and set to the ImageView
String title = titel[position];
Integer imageRes = TITLE_IMAGES.get(title);
if (imageRes == null) { // not found in map
image.setImageResource(0);
} else {
image.setImageResource(imageRes);
}
final ListView listView = (ListView) rootView.findViewById(R.id.listViewswipeview);
final EditText wdhedittext = (EditText) rootView.findViewById(R.id.editTextswipeView);
final EditText kgedittext = (EditText) rootView.findViewById(R.id.editText2);
final Button btnswipeview = (Button) rootView.findViewById(R.id.imagebuttonswipeview);
adapter2 = new CustomListViewAdapter2(getActivity(),
R.layout.swipeviewrowitem, rowItems);
listView.setAdapter(adapter2);
btnswipeview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View View) {
if (wdhedittext.getText().toString().matches("")) {
Toast toast = Toast.makeText(getActivity(),
"Bitte Wiederholungszahl angeben",
Toast.LENGTH_SHORT);
toast.show();
} else if (kgedittext.getText().toString().matches("")) {
final rowitem2 item = new rowitem2(wdhedittext.getText().toString() + "wdh", kgedittext.getText().toString());
adapter2.add(item);
rowItems.add(item);
} else {
final rowitem2 item = new rowitem2(wdhedittext.getText().toString() + "wdh", kgedittext.getText().toString() + "kg");
adapter2.add(item);
rowItems.add(item);
/*String name = wdhedittext.getText().toString();
int rowId = workoutDBAO2.addWorkout2(name);
HashMap rowValsSets = new HashMap();
rowValsSets.put("sets", name);
rowValsSets.put("id", rowId);
adapter3.add(rowValsSets);*/
}
}
});
rowItems = new ArrayList<rowitem2>();
return rootView;
}
CustomListViewAdapter2 和 rowitem2 类
class CustomListViewAdapter2 extends ArrayAdapter<rowitem2> {
private final ArrayList<rowitem2> items;
Context context;
public CustomListViewAdapter2(Context context, int resourceId,
ArrayList<rowitem2> items) {
super(context, resourceId, items);
this.context = context;
this.items=items;
}
public View getView(int position, View convertView, ViewGroup parent) {
rowitem2 rowItem = items.get(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = mInflater.inflate(R.layout.swipeviewrowitem, null);
TextView txtTitle = (TextView) view.findViewById(R.id.mainText2);
TextView txtDesc = (TextView) view.findViewById(R.id.textView2);
txtDesc.setText(rowItem.getDesc());
txtTitle.setText(rowItem.getTitle());
return view;
}
@Override
public boolean areAllItemsEnabled() {
return true;
}
}
}
class rowitem2 {
private String edittexttitle;
private String edittexttitle2;
public rowitem2(String title, String desc) {
this.edittexttitle = title;
this.edittexttitle2 = desc;
}
public String getTitle() {
return edittexttitle;
}
public String getDesc() {
return edittexttitle2;
}
public void setTitle(String title) {
this.edittexttitle = title;
}
@Override
public String toString() {
return edittexttitle + "\n" + edittexttitle2;
}
}
答案 0 :(得分:0)
答案 1 :(得分:0)
您无法在ArrayList
中保留任何数据。如果您想永久存储它们,则应使用SQLite database
或SharedPreferences
。
Here是如何使用SQLite数据库解决问题的一个很好的例子。如果您计划存储和管理大量数据,这将非常有用。
如果您不打算存储大量数据,使用SharedPreferences
将是一个很好的选择。例如:
//Store a String in SharedPreferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("your_key", your_string_variable);
每当您想要检索它时(例如,onResume
或onCreate
)
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String your_string_variable = settings.getString("your_key", "default_value");
可以找到有关SharedPreferences的更多信息here。