我怀疑我有一个活动,一个片段需要在片段数据的活动中填充listview。但是,当我从活动列表视图中按回按钮数据丢失时,我怀疑如何永久存储数据我已经阅读了有关共享首选项的信息,我们可以永久存储和检索数据但是当我使用它时,我不会永久存储listview值它仍然是错过了这个值,因为我对Android很新,到目前为止我对此感到困惑的是:
这是我的片段:
package first.service.precision.servicefirst;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
/**
* Created by 4264 on 16-12-2015.
*/
public class NewCompetitors extends Fragment {
CompetitorsData competitorsData;
CompetitorsView competitorsView = new CompetitorsView();
EditText editText_name,editText_website,editText_strength,editText_weakness;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.new_competitors_fragment, container, false);
editText_name = (EditText) rootView.findViewById(R.id.competitors_name);
editText_website = (EditText) rootView.findViewById(R.id.competitors_website);
editText_strength = (EditText) rootView.findViewById(R.id.competitors_strength);
editText_weakness = (EditText) rootView.findViewById(R.id.competitors_weakness);
Button btn_save = (Button) rootView.findViewById(R.id.btn_save);
btn_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editTextcompetitor();
sendData(competitorsView);
getActivity().getFragmentManager().popBackStack();
}
});
return rootView;
}
public void editTextcompetitor(){
competitorsView.setCompetitor_name(editText_name.getText().toString());
competitorsView.setCompetitor_website(editText_website.getText().toString());
competitorsView.setCompetitor_strength(editText_strength.getText().toString());
competitorsView.setCompetitor_weakness(editText_weakness.getText().toString());
}
public void sendData(CompetitorsView competitorsView){
competitorsData.dataToCompetitorActivity(competitorsView);
}
@SuppressWarnings("deprecation")
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
competitorsData = (CompetitorsData) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()+ "");
}
}
public interface CompetitorsData{
void dataToCompetitorActivity(CompetitorsView competitorsView);
}
}
这是我的活动,其中我填充listview:
package first.service.precision.servicefirst;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class Competitors extends Main2Activity implements NewCompetitors.CompetitorsData{
ListView lv;
SharedPreferences sharedPreferences;
List<CompetitorsView> competitorsViews;
CompetitorsListAdapter competitorsListAdapter;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_competitors);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.activity_competitors, null, false);
mDrawerLayout.addView(contentView, 0);
lv=(ListView)findViewById(R.id.list_competitors);
competitorsViews=new ArrayList<CompetitorsView>();
competitorsListAdapter=new CompetitorsListAdapter(this,R.id.list_competitors,competitorsViews);
lv.setAdapter(competitorsListAdapter);
Button btn_competitors=(Button)findViewById(R.id.btn_new_competitors);
SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
SharedPreferences.Editor edit=sharedPreferences.edit();
Gson gson=new Gson();
String yog=sharedPreferences.getString("go","");
Type type=new TypeToken<List<NewCompetitors>>(){}.getType();
List<CompetitorsView>competitorsViews=gson.fromJson(yog,type);
edit.commit();
btn_competitors.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NewCompetitors fragment = new NewCompetitors();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.competitors_frame, fragment);
ft.addToBackStack(null);
ft.commit();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_competitors, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onDestroy() {
super.onDestroy();
Gson gson = new Gson();
String jsonCurProduct = gson.toJson(competitorsViews);
SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("yog", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("go", jsonCurProduct);
editor.commit();
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
//ArrayList<CompetitorsView>cc=savedInstanceState.getParcelableArrayList("yog");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
public void dataToCompetitorActivity(CompetitorsView competitorsView)
{
competitorsViews.add(competitorsView);
competitorsListAdapter.notifyDataSetChanged();
}
@Override
protected void onRestart() {
super.onRestart();
}
}
这是我的customlistadapter:
package first.service.precision.servicefirst;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;
/**
* Created by 4264 on 16-12-2015.
*/
public class CompetitorsListAdapter extends ArrayAdapter<CompetitorsView> {
private List<CompetitorsView> listData;
private LayoutInflater layoutInflater;
public CompetitorsListAdapter(Context context, int resource, List<CompetitorsView> listdata) {
super(context, resource, listdata);
this.listData = listdata;
layoutInflater = LayoutInflater.from(context);
}
@Override
public void add(CompetitorsView object) {
super.add(object);
}
@Override
public int getCount() {
return listData.size();
}
@Override
public CompetitorsView getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View mview = convertView;
if (mview == null) {
convertView = layoutInflater.inflate(R.layout.list_competitors, null);
// TextView req = (TextView) convertView.findViewById(R.id.txt);
//req.setText("Select Lead Information");
holder = new ViewHolder();
holder.competitors_name = (TextView) convertView.findViewById(R.id.company_grp);
holder.competitors_website = (TextView) convertView.findViewById(R.id.company_name);
holder.competitors_strength = (TextView) convertView.findViewById(R.id.addrsline);
holder.competitors_weakness = (TextView) convertView.findViewById(R.id.stateid);
// add.lv.setVisibility(View.INVISIBLE);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// add.lv.setVisibility(View.VISIBLE);
holder.competitors_name.setText("Competitors Name: " + listData.get(position).getCompetitor_name());
holder.competitors_website.setText("Website: " + listData.get(position).getCompetitor_website());
holder.competitors_strength.setText("Competitors Strength: " + listData.get(position).getCompetitor_strength());
holder.competitors_weakness.setText("Competitors Weakness:" + listData.get(position).getCompetitor_weakness());
convertView.setBackgroundColor(Color.parseColor("#ffffff"));
return convertView;
}
static class ViewHolder {
public TextView competitors_name;
public TextView competitors_website;
public TextView competitors_strength;
public TextView competitors_weakness;
}
}
它正在运作,但它没有存储价值!