我有一个片段类,在单击选项卡时会调用它。将数据插入数据库后,片段中的listview不会自动刷新。列表视图仅在重新打开应用程序时刷新,或仅在切换其他选项卡后才打开当前选项卡。帮我自动刷新当前片段的列表视图。
SocialFragment.java
package layout;
import android.content.Context;
import android.database.Cursor;
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.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import com.example.kiran.herau.NewDatabaseAdapter;
import com.example.kiran.herau.R;
public class SocialFragment extends Fragment {
protected BaseAdapter mAdapter;
private SimpleCursorAdapter dataAdapter;
NewDatabaseAdapter vivzHelper;
TextView text;
Context context;
@Nullable
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.activity_fifth, null);
ListView lb = (ListView)view.findViewById(R.id.listView1);
context = getActivity().getBaseContext();
vivzHelper = new NewDatabaseAdapter(getActivity());
Cursor cursor = vivzHelper.fetchAllData();
// The desired columns to be bound
String[] columns = new String[]{
NewDatabaseAdapter.NewDatabaseHelper.UID,
NewDatabaseAdapter.NewDatabaseHelper.TEACHER_NAME,
NewDatabaseAdapter.NewDatabaseHelper.SUBJECT_NAME,
NewDatabaseAdapter.NewDatabaseHelper.START_TIME,
NewDatabaseAdapter.NewDatabaseHelper.END_TIME,
};
// the XML defined views which the data will be bound to
int[] to = new int[]{
R.id.uid,
R.id.date,
R.id.time,
R.id.subject,
R.id.end_time,
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
context, R.layout.routinerow,
cursor,
columns,
to,
0);
lb.setEmptyView(view.findViewById(R.id.empty));
dataAdapter.notifyDataSetChanged();
// Assign adapter to ListView
lb.setAdapter(dataAdapter);
return view;
}
}
答案 0 :(得分:0)
如果您有代码告诉您数据库何时更新,您只需调用
即可adapter.notifyDataSetChanged();
喜欢 -
public void methodThatIsCalledWhenDatabaseUpdates(Datatype data){
adapter.setData(data);
adapter.notifyDataSetChanged();
}