我有一个显示用户添加的任务的ListView,我目前已经将名称的字符串保存到listview随后加载的数组中。但我需要保存用户输入的字符串以及他们添加的任务是否已完成。我考虑过使用共享偏好。这就是我已经做过的事情:
package com.swapsy.jack.bucketlist;
import android.content.Context;
import android.content.SharedPreferences;
import android.renderscript.ScriptGroup;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private AdView mAdView;
Button mShowComplete;
FloatingActionButton mAddTask;
boolean showComplete;
ListView mlvToDoList;
Context mContext;
SharedPreferences mPrefs;
SharedPreferences.Editor mEditor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//--------- Advertisement ------------
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequestBanner = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("5DF76A39EE68E7EAD108ACDD62B2728C") //Change to "ca-app-pub-1914598267234410/4838548938"
.build();
mAdView.loadAd(adRequestBanner);
mAdView.setAdListener(new AdListener(){
@Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
mAdView.setVisibility(View.VISIBLE);
}
@Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
mAdView.setVisibility(View.GONE);
}
});
//---------------Main code---------------
mShowComplete = (Button) findViewById(R.id.btnShowCompleted);
mAddTask = (FloatingActionButton) findViewById(R.id.btnAddFab);
mlvToDoList = (ListView) findViewById(R.id.lvToDoList);
String[] bucket = new String[] {
};
final List<String> bucket_list = new ArrayList<String>(Arrays.asList(bucket));
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.list_item, bucket_list);
mlvToDoList.setAdapter(arrayAdapter);
mAddTask.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
View mView = getLayoutInflater().inflate(R.layout.dialog_input, null);
final EditText mTaskNameInput = (EditText) mView.findViewById(R.id.inputText);
Button mAdd = (Button) mView.findViewById(R.id.btnAdd);
mAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Add task
bucket_list.add(mTaskNameInput.toString());
arrayAdapter.notifyDataSetChanged();
mBuilder.setView(null);
}
});
mBuilder.setView(mView);
AlertDialog dialog = mBuilder.create();
dialog.show();
}
});
mShowComplete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (showComplete) {
//true
mShowComplete.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_enabled, 0, 0, 0);
showComplete = false;
} else {
//false
mShowComplete.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_disabled, 0, 0, 0);
showComplete = true;
}
}
});
}
private void saveList() {
}
@Override
public void onDestroy() {
super.onDestroy();
saveList();
}
}
答案 0 :(得分:0)
您可以使用getter和setter
创建一个类class Structure{
private String string;
private boolean bln;
// constructor
public Structure(String name, boolean bln) {
this.string = name;
this.bln= bln;
}
// getter
public String getString() { return string; }
public boolean getBln() { return bln; }
// setter
public void setString(String string) { this.string = string; }
public void setBln(boolean bln) { this.bln= bln; }
}
用法:
Structure struc = new Structure("", false);
struc.setBln(your_boolean);
boolean bln = struc.getBln();
struc.setString(your_string);
String s= struc.getString();
将其用作数组
Structure[] array = new Structure[10];
array[0] = new Structure();
array[0].setID("yourid");
String id = array[0].getID();
// with arraylist
ArrayList<Structure> arraylist = new ArrayList<Structure>();
arraylist.add(new YourCustomObject());
arraylist.get(0).setID("yourid");
String id = arraylist.get(0).getID();