私のアプリはViewPager
で、Fragments
でタブの上部にあります。 私は4つのセクションがあり、それらのうち2つはint値を共有し、SharedPreferencesにはfloatが格納されています。ListViewの項目が削除されたときにフラグメントをリフレッシュする
このフラグメントのうちの1つでは、DBからデータを取り込み、ListView
に表示します! 私はデータを追加するときに問題はないが、sharedpreferencesが変更され、TextView
が新しい値で設定される。
ListView
からアイテムを削除すると問題が表示されます。 このListView
には、行を削除するための「X」内のボタンがあります。削除すると、新しい平均を再計算してSharedPreferencesに保存しますが、TextView
の値は新しい結果に変更されません。ここで
public class VotiEsamiAdapter extends ArrayAdapter<VotiEsami> {
public MySQLiteHelper db;
private List<VotiEsami> searchArrayList;
private List<VotiEsami> esami_tot;
SharedPreferences sharedPref;
public VotiEsamiAdapter(Context context, int textViewResourceId, List objects){
super(context, textViewResourceId, objects);
searchArrayList = objects;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listview_voti_line, null);
db = new MySQLiteHelper(getContext());
final TextView nome = (TextView)convertView.findViewById(R.id.text_line_voto_nome);
final TextView cfu = (TextView)convertView.findViewById(R.id.text_line_voto_cfu);
TextView voto = (TextView)convertView.findViewById(R.id.text_line_voto_valore);
VotiEsami m = getItem(position);
// inflate other items here :
ImageButton delete_row_button = (ImageButton)convertView.findViewById(R.id.delete_button_line_voti);
delete_row_button.setOnClickListener(
new Button.OnClickListener() {
@Override
public void onClick(View v) {
searchArrayList.remove(position);
db.deleteSelectedVotoEsame(nome.getText().toString());
notifyDataSetChanged();
//Carico la media pesata e i cfu totale con sharedpreferences
sharedPref = getContext().getSharedPreferences("USR_DATA", Context.MODE_PRIVATE);
//Aggiorno media e CFU
//Get all exams inside database
esami_tot = db.getVotiEsami();
float MEDIA_PONDERATA;
float media_parziale;
float media_sommata_parziale = 0;
int CFU_SOMM = 0;
if (esami_tot.isEmpty()) {
CFU_SOMM = 0;
MEDIA_PONDERATA = 0;
Toast.makeText(getContext(), "EMPTY!!", Toast.LENGTH_LONG).show();
} else {
//Calcolare media totale e CFU totale
for (VotiEsami exams : esami_tot) {
int voto_temp = exams.get_voto();
if (voto_temp == 31) voto_temp = 30;
media_parziale = (voto_temp) * (exams.get_cfu());
CFU_SOMM += exams.get_cfu();
media_sommata_parziale += media_parziale;
}
MEDIA_PONDERATA = media_sommata_parziale/CFU_SOMM;
}
//Salvo i dati
SharedPreferences.Editor editor = sharedPref.edit();
editor.putFloat("MEDIA", MEDIA_PONDERATA);
editor.putInt("CFU", CFU_SOMM);
}
}
);
nome.setText(m.get_nome_esame());
cfu.setText("CFU: " + String.valueOf(m.get_cfu()));
if(m.get_voto() == 31) {
voto.setText("Voto: " + String.valueOf(m.get_voto()-1) + "L");
}else{
voto.setText("Voto: " + String.valueOf(m.get_voto()));
}
//Se la media è inferiore al 6 lo scrivo in rosso
//altrimenti in verde
if(m.get_voto() <= 21) {
voto.setTextColor(getContext().getResources().getColor(R.color.rosso));
}else if ((m.get_voto() <= 25) && (m.get_voto() >= 22)){
voto.setTextColor(getContext().getResources().getColor(R.color.giallo));
}else if((m.get_voto() >= 25) && (m.get_voto() <=31)){
voto.setTextColor(getContext().getResources().getColor(R.color.verde));
}
return convertView;
}
}
私はListView
に1つの鉱石2行を削除する場合
public class ThreeFragment extends Fragment {
SharedPreferences sharedPref;
public MySQLiteHelper db;
public List<VotiEsami> esami;
public VotiEsamiAdapter adapter; //Adapter per caricare la listview di voti
public ThreeFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db = new MySQLiteHelper(getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_three, container, false);
//Carico la media pesata e i cfu totale con sharedpreferences
sharedPref = getActivity().getSharedPreferences("USR_DATA", Context.MODE_PRIVATE);
int defaultValueInt = 0;
float SharedMedia = sharedPref.getFloat("MEDIA", defaultValueInt);
int SharedCFU = sharedPref.getInt("CFU", defaultValueInt);
TextView media_top = (TextView) view.findViewById(R.id.text_media);
TextView cfu_top = (TextView) view.findViewById(R.id.text_tot_cfu);
media_top.setText("Media: " + String.valueOf(SharedMedia));
cfu_top.setText("CFU TOT: " + String.valueOf(SharedCFU));
//Find the listview
final ListView listView = (ListView)view.findViewById(R.id.voti_listView);
//Get all exams inside database
esami = db.getVotiEsami();
adapter = new VotiEsamiAdapter(getContext(), R.layout.listview_voti_line, esami);
listView.setAdapter(adapter);
float MEDIA_PONDERATA;
float media_parziale;
float media_sommata_parziale = 0;
int CFU_SOMM = 0;
if(!esami.isEmpty()) {
//Calcolare media totale e CFU totale
for (VotiEsami exams : esami) {
int voto_temp = exams.get_voto();
if (voto_temp == 31) voto_temp = 30;
media_parziale = (voto_temp) * (exams.get_cfu());
CFU_SOMM += exams.get_cfu();
media_sommata_parziale += media_parziale;
}
MEDIA_PONDERATA = media_sommata_parziale/CFU_SOMM;
}else{
MEDIA_PONDERATA = 0;
}
//Salvo i dati
SharedPreferences.Editor editor = sharedPref.edit();
editor.putFloat("MEDIA", MEDIA_PONDERATA);
editor.putInt("CFU", CFU_SOMM);
//TODO gestire cancellazione del voto dal DB e dalla listview
Button nuovo_voto = (Button) view.findViewById(R.id.button_add_new_voto);
nuovo_voto.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Start Dialog for input the new vote
Intent myIntent = new Intent(getActivity(), DialogAddVotoEsame.class);
ThreeFragment.this.startActivityForResult(myIntent, 1);
}
});
return view;
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
List<VotiEsami> esami_per_media;
if(requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
String result_nome = data.getStringExtra("result_name_esame"); //Take the materia from Dialog
int result_cfu = data.getIntExtra("result_cfu_esame", 1); //Take the materia from Dialog
int result_voto = data.getIntExtra("result_voto_esame", 1); //Take the materia from Dialog
//Add exam with vote to DB
db.addEsameVoto(new VotiEsami(result_nome, result_cfu, result_voto));
esami_per_media = db.getVotiEsami();
float MEDIA_PONDERATA;
float media_parziale;
float media_sommata_parziale = 0;
int CFU_SOMM = 0;
//TODO calcolare media totale e CFU totale
for (VotiEsami exams : esami_per_media) {
int voto_temp = exams.get_voto();
if(voto_temp == 31) voto_temp = 30;
media_parziale = (voto_temp) * (exams.get_cfu());
CFU_SOMM += exams.get_cfu();
media_sommata_parziale += media_parziale;
}
MEDIA_PONDERATA = media_sommata_parziale/CFU_SOMM;
//Salvo i dati
SharedPreferences.Editor editor = sharedPref.edit();
editor.putFloat("MEDIA", MEDIA_PONDERATA);
editor.putInt("CFU", CFU_SOMM);
editor.commit();
TextView media_top = (TextView) getActivity().findViewById(R.id.text_media);
TextView cfu_top = (TextView) getActivity().findViewById(R.id.text_tot_cfu);
media_top.setText("Media: " + String.valueOf(MEDIA_PONDERATA));
cfu_top.setText("CFU TOT: " + String.valueOf(CFU_SOMM));
//Refresh list
List<VotiEsami> newesame = db.getVotiEsami();
//Aggiorno la Listview dell'activity con il nuovo inserimento
esami.clear();
esami.addAll(newesame);
adapter.notifyDataSetChanged();
}
}
}//onActivityResult
}
がどのように私はTextView
の値を変更することができ、このデータを管理し、表示されフラグメント? Adapter
のFragments
のTextView
をfindViewById
できないため、削除すると大きな問題が発生します。 これを実行する方法はありますか? Adapter
からFragment
の中にTextView
が見つかりますか?
try adapter.notify(); – Fakher
どこで、断片ですか? – Dario