我的疑问是我在一个片段中有一个活动我需要在片段数据的活动中填充listview,应该将它保存在我现在成功完成的共享偏好中我想要做的是当我点击listitems它必须去输入数据的来源,如编辑listview项目,我到目前为止如何做到这一点,我试过的是:
这是片段(from_data):
package first.service.precision.servicefirst;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
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;
Competitors competitors;
Context context;
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);
Bundle bundle=new Bundle();
String yoggs=bundle.getString("yogg");
editText_name.setText(yoggs);
editText_website.setText(yoggs);
editText_strength.setText(yoggs);
editText_weakness.setText(yoggs);
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.Activity;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class Competitors extends Activity implements NewCompetitors.CompetitorsData{
ListView lv;
String god;
FileOutputStream fos;
ObjectOutputStream out;
SharedPreferences sharedPreferences;
public String yog;
List<CompetitorsView> competitorsViews;
public CompetitorsListAdapter competitorsListAdapter;
static Context contexts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_competitors);
contexts=this;
lv=(ListView)findViewById(R.id.list_competitors);
competitorsViews=new ArrayList<CompetitorsView>();
;
SharedPreferences sharedPreferences=PreferenceManager.getDefaultSharedPreferences(contexts);
Gson gsong=new Gson();
final String yog=sharedPreferences.getString("yog","");
Type type=new TypeToken<ArrayList<CompetitorsView>>(){}.getType();
competitorsViews=gsong.fromJson(yog,type);
competitorsListAdapter=new CompetitorsListAdapter(this,R.id.list_competitors,competitorsViews);
lv.setAdapter(competitorsListAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
god =String.valueOf(id);
NewCompetitors fragment = new NewCompetitors();
Bundle bundle=new Bundle();
bundle.putString("yogg",god);
fragment.setArguments(bundle);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.competitors_frame, fragment);
ft.commit();
}
});
Button btn_competitors=(Button)findViewById(R.id.btn_new_competitors);
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) {
getMenuInflater().inflate(R.menu.main2, menu);
return true;
}
@Override
protected void onStop() {
contexts=this;
SharedPreferences sharedpref= PreferenceManager.getDefaultSharedPreferences(contexts);
SharedPreferences.Editor editor=sharedpref.edit();
Gson gson=new Gson();
String god=gson.toJson(competitorsViews);
editor.putString("yog",god);
editor.commit();
super.onStop();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/* public static CompetitorsView get_User_From_Shared_Prefs(Context context)
{
}*/
@Override
public void dataToCompetitorActivity(CompetitorsView competitorsView)
{
competitorsViews.add(competitorsView);
competitorsListAdapter.notifyDataSetChanged();
}
}
我的listview项目怎么可以编辑?