我有不同的listview一样但现在我有问题。我想创建一个保存收藏夹的方法。很抱歉这个简单的问题,但我是android和java的新手。我知道这些数据是静态的,但我不知道是否可以共享首选项来保存这些数据。
import android.content.Intent;
import android.graphics.Color;
import android.renderscript.Sampler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ammunition extends AppCompatActivity {
String[] listaammunition = new String[]{"Arrow Zigzag","Arrow, Alchemist’s Fire(1)"};
EditText inputSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ammunition);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, listaammunition) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setTextColor(Color.BLACK);
return view;
}
};
ListView listViewammunition = (ListView) findViewById(R.id.listViewammunition);
listViewammunition.setAdapter(adapter);
inputSearch = (EditText) findViewById(R.id.inputSearchmunizioni);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable s) {
}
});
listViewammunition.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String Arrowalchemistsfire = adapter.getItem(position);
if (Arrowalchemistsfire.equals("Arrow, Alchemist’s Fire(1)")) {
Intent Arrowalchemistsfire1 = new Intent(getApplicationContext(), dettagli_armi.class);
Arrowalchemistsfire1.putExtra("costo", "75 gp");
Arrowalchemistsfire1.putExtra("dannis", "1D4");
Arrowalchemistsfire1.putExtra("dannim", "1D4");
Arrowalchemistsfire1.putExtra("crit", "x2");
Arrowalchemistsfire1.putExtra("rangeinc", "30 ft.");
Arrowalchemistsfire1.putExtra("weight", "1/5 lb.");
Arrowalchemistsfire1.putExtra("type", "fire");
Arrowalchemistsfire1.putExtra("dettagli", "Each of these projectiles carries a deadly load of alchemist's fire in its hollow shaft.\n" +
" When it strikes a target, the arrow's shaft shatters, releasing the alchemist's fire directly onto the target. One round after impact, the alchemist's fire ignites, dealing 1d4 points of damage.\n" +
"\n" +
" The target can use a full-round action to attempt to extinguish the flames before taking this damage. It takes a successful Reflex saving throw (DC 15) to extinguish the flames.\n" +
" Rolling on the ground earns the target a +2 bonus on the save. Submerging (such as by leaping into a lake) or magically extinguishing the flames automatically kills the flames.\n");
Arrowalchemistsfire1.putExtra("Source", "Dragon #349");
startActivity(Arrowalchemistsfire1);
}
String Arrowalchemistsfrost = adapter.getItem(position);
if (Arrowalchemistsfrost.equals("Arrow, Alchemist’s Frost(1)")) {
Intent Arrowalchemistsfrost1 = new Intent(getApplicationContext(), dettagli_armi.class);
Arrowalchemistsfrost1.putExtra("costo", "75 gp");
Arrowalchemistsfrost1.putExtra("dannis", "1D4");
Arrowalchemistsfrost1.putExtra("dannim", "1D4");
Arrowalchemistsfrost1.putExtra("crit", "x2");
Arrowalchemistsfrost1.putExtra("rangeinc", "30 ft.");
Arrowalchemistsfrost1.putExtra("weight", "1/5 lb.");
Arrowalchemistsfrost1.putExtra("type", "cold");
Arrowalchemistsfrost1.putExtra("dettagli", "Similar in designed to the Arrow alchemist's fire, this arrow instead carrier an alchemical compound that creates a spray of intense cold.\n" +
" If the arrow strikes a target, the arrow immediately shatters and deals 1d4 points of cold damage.\n");
Arrowalchemistsfrost1.putExtra("Source", "Dragon #349");
startActivity(Arrowalchemistsfrost1);
}
});
}
}
答案 0 :(得分:0)
使用tinyDB,你需要只添加一个类。
https://github.com/kcochibili/TinyDB--Android-Shared-Preferences-Turbo
以下是代码段
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ecart);
//Fetch data from DB
GlobaDataHolder.getGlobaDataHolder().setShoppingList(
new TinyDB(getApplicationContext()).getListObject(
PreferenceHelper.MY_CART_LIST_LOCAL, Product.class));
}
@Override
protected void onPause() {
super.onPause();
// Store Shopping Cart in DB
new TinyDB(getApplicationContext()).putListObject(
PreferenceHelper.MY_CART_LIST_LOCAL, GlobaDataHolder
.getGlobaDataHolder().getShoppingList());
}
有关完整代码,请参阅