我在ViewPager中有一个包含两个片段的程序,一个常规片段和一个ListFragment。我在常规片段中弹出一个AlertDialog,将数据添加到SQLite数据库中。但是,我想使用常规Fragment中的数据刷新ListFragment中的列表。经过几天的挣扎,我放弃了,想在这里发布我的代码,看看是否有人可以帮我解决这个问题。提前感谢您的任何回复。
这是我的ListFragment类:
import java.util.ArrayList;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public class ClassListFragment extends ListFragment {
private ArrayList<Long> ids = new ArrayList<Long>();
private ArrayList<String> classes = new ArrayList<String>();
private ArrayList<Double> currentGrades = new ArrayList<Double>();
private ArrayList<Double> desiredGrades = new ArrayList<Double>();
private ArrayList<Double> finalGrades = new ArrayList<Double>();
private ClassListAdapter adapter;
private DataHandler dh;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.class_list, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dh = new DataHandler(getActivity().getBaseContext());
dh.open();
adapter = new ClassListAdapter(getActivity().getBaseContext(), classes, currentGrades, desiredGrades, finalGrades);
setListAdapter(adapter);
}
@Override
public void onStart() {
super.onStart();
refreshList();
adapter.notifyDataSetChanged();
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
dh.deleteRow(ids.get(position));
adapter.notifyDataSetChanged();
}
private void refreshList() {
adapter.clear();
ids.clear();
Cursor data = dh.returnData();
if (data.moveToFirst()) {
do {
ids.add(data.getLong(0));
classes.add(data.getString(1));
currentGrades.add(data.getDouble(2));
desiredGrades.add(data.getDouble(3));
finalGrades.add(data.getDouble(4));
}while(data.moveToNext());
}
}
}
这是我的Fragment类,删除问题不需要自定义方法:
import java.text.DecimalFormat;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
public class QuickCalc extends Fragment {
private TextView tv;
private DataHandler dh;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.quickcalc_layout, container, false);
tv = (TextView) v.findViewById(R.id.tv);
Button button = (Button) v.findViewById(R.id.calculateButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clicked();
}
});
return v;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dh = new DataHandler(getActivity().getBaseContext());
}
public void clicked() {
EditText currentGradeBox = (EditText) getView().findViewById(R.id.currentGrade);
EditText desiredGradeBox = (EditText) getView().findViewById(R.id.desiredGrade);
RadioGroup rg = (RadioGroup) getView().findViewById(R.id.radioGroup);
int rbID = rg.getCheckedRadioButtonId();
final Double currentGrade = Double.parseDouble(currentGradeBox.getText().toString());
final String desiredGrade = desiredGradeBox.getText().toString();
Object[] values = getValues(rbID, desiredGrade);
if (values != null) {
final String answer = doCalculations(values, currentGrade);
if (Double.parseDouble(answer) <= 100.0) {
if (String.valueOf(values[0]).contains("A")) {
tv.setText("You need to get a " + answer + "% in order to get an " + values[0] + " (" + values[1] + "%) in this class.\n \nWould you like to save this to your class list?");
}
else {
tv.setText("You need to get a " + answer + "% in order to get a " + values[0] + " (" + values[1] + "%) in this class.\n \nWould you like to save this to your class list?");
}
Button addButton = (Button) getView().findViewById(R.id.addClass);
addButton.setVisibility(Button.VISIBLE);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle("Add Class");
final EditText className = new EditText(getActivity());
className.setHint("Class Name");
alert.setView(className);
alert.setPositiveButton("Create", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dh.open();
dh.insertData(className.getText().toString(), currentGrade, Double.parseDouble(desiredGrade), Double.parseDouble(answer));
dh.close();
}
});
alert.setNegativeButton("Cancel", null);
AlertDialog dialog = alert.create();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
dialog.show();
}
});
}
else {
tv.setText("The grade that you wish to achieve is impossible.");
}
}
else {
tv.setText("ERROR in one of the text boxes. Please fix it.");
}
}
答案 0 :(得分:0)
我看不到你在哪里调用refreshList(),你只从onStart调用吗?在这种情况下它将无法工作..你需要onRefresh