私はリストビューを持つアクティビティを持っています。 ListViewのデータはsharedPreferencesに保存する必要があります。私はLoadPreferencesとSavePreferencesという2つのメソッドを持っています。しかし問題は、彼らが一緒に働かないということです。どちらのメソッドも動作し、それらを別々に1つのアダプタに使用します。しかし、私は両方が必要です。そこで私は間違いをする? MainActivityは次のとおりです。ListViewからSharedPreferencesへのデータの保存
public class MainPage extends AppCompatActivity {
SharedPreferences prefs;
EditText input;
Button btnAdd, btnDel;
ListView list;
ArrayAdapter<String> adapter, adapter2;
public static ArrayList<String> taskList = new ArrayList<String>();
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
prefs = getSharedPreferences("myUsers", MODE_PRIVATE);
input = (EditText) findViewById(R.id.txtItem);
btnAdd = (Button) findViewById(R.id.btnAdd);
list = (ListView) findViewById(R.id.list1);
btnDel = (Button) findViewById(R.id.btnDel);
userName();
LoadPreferences();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
///adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,taskList);
list.setAdapter(adapter);
list.setAdapter(adapter2);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String task = input.getText().toString();
adapter.add(task);
adapter.notifyDataSetChanged();
SavePreferences();
}
}
);
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu1, menu);//What Options Menu to present
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
String msg = "";
switch (item.getItemId()) {//selected menu item
case R.id.first:
///msg="first selected";
Logout();
break;
case R.id.second:
msg = "Second selected";
break;
}
/// Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
return super.onOptionsItemSelected(item);
}
public void Logout() {
JSONObject userJson = getUserByName(userName());
Toast.makeText(this, "found" + userJson, Toast.LENGTH_LONG).show();
String status = "status";
try {
userJson.put(status, false);//local json change
prefs.edit().putString(userJson.getString("name"), userJson.toString()).commit();//save changed JSON
} catch (JSONException e) {
e.printStackTrace();
}
Intent i = new Intent(this, Login.class);
this.startActivity(i);
}
private JSONObject getUserByName(String name) {
try {
return new JSONObject(prefs.getString(name, ""));
} catch (JSONException e) {
return null;
}
}
public String userName() {
String msg =null;
Intent i = getIntent();//get Intent - that started this Activity
if (i.hasExtra("userName")) {//if has data by key: "msg"
msg = i.getStringExtra("userName");//get data by key "msg" as String
//// Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
return msg;
}
protected void SavePreferences() {
JSONObject ExistUser = getUserByName(userName());
////Toast.makeText(this,ExistUser+"found",Toast.LENGTH_LONG).show();
String ag = input.getText().toString().trim();
if (ag.length() != 0) {
taskList.add(ag);
input.setText("");
try {
ExistUser.put("task", new JSONArray(taskList));
prefs.edit().putString(ExistUser.getString("name"), ExistUser.toString()).commit();
Toast.makeText(this, ExistUser + "found", Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
} else {
}
}
public ArrayList<String> LoadPreferences() {
JSONObject ExistUser = getUserByName(userName());
try {
if(ExistUser.getJSONArray("task")!=null){
JSONArray jArray = ExistUser.getJSONArray("task");
if (jArray != null) {
for (int i=0;i<jArray.length();i++){
taskList.add(jArray.getString(i));
}
}
}
else{
return null;
}
} catch (JSONException e) {
e.printStackTrace();
}
return taskList;
}
}
レイアウトは次のとおりです。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/txtItem"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="@string/hintTxtItem"
/>
<Button
android:id="@+id/btnAdd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/lblBtnAdd"
android:layout_toRightOf="@id/txtItem"
android:background="#009900"
/>
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/txtItem"
android:text="@string/txtEmpty"
android:gravity="center_horizontal"
/>
<ListView
android:id="@+id/list1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/txtItem"
android:choiceMode="multipleChoice" >
</ListView>
<Button
android:id="@+id/btnDel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/lblBtnDel"
android:background="#009900"
/>
</RelativeLayout>
にアダプタからの文字列を保存したい - SharedPreferencesにリストを保存しないでください...あなたは、データベースに対して何を持っています? –
また、 'list.setAdapter(adapter2);'は動作しません。なぜなら、1) 'adapter2'がnullであり、2)ListViewに** adapter **を一度に割り当てることができるだけです。 –
複雑な私はそれを支持した。私はちょうど学び、私はまだデータベースと対話する方法を知らない。第二に、今私は二つのものだけを接続する必要があります。リストビューと共有参照。私がデータベースを使用する場合、私は3つのものを接続する必要があります。第三に、私は解決に非常に近いと感じました。しかし、これが唯一の可能な方法だと言えば、私は試してみます... – KeitL