您好我是Android编程的初学者,我在刷新listView时遇到问题。
我正在使用CURSORADAPTER
来显示listView
中的数据,我的问题是刷新光标以显示新插入的数据!!!
我在StackOverflow上阅读了很多类似的问题,我看到其他人谈论swapCursor()
或changeCursor()
,或adapter.notifyDataSetChanged();
或requery()
- 已弃用,但我真的不知道该怎么做。
我现在正在努力解决这个问题五天了,我真的试图在网上找到一些例子,但我现在还没有找到任何我理解的东西。
然后我制作了另一个只使用两个片段db和listView
的简单app。
在第一个片段(FragmentA)
上,我唯一要做的就是在我的数据库中插入一些数据,在第二个片段``(FragmentB)`我有一个listView,显示来自我的数据库的数据。
当我在我的FragmentA
上的数据库中插入数据,然后转到第二个片段FragmentB
时,我的listView
中没有从db中新插入项目,我明白为什么,因为来自onCreateView
的android生命周期fragmentB
方法在我启动应用程序时已被调用一次,并且游标已从db中加载数据,当我关闭并重新启动应用程序时,我从db获取新数据因为当再次调用onCreateView
时光标被“重新加载”。我拥有所有这些覆盖方法:onCreateView
,onAttach
,onCreate
,onActivityCreated
,onStart
,onResume
,onPause
, onStop
,onDestroyView
,onDestroy
,onDetach
但是当我只使用2个片段时,这些方法都没有被调用,这也是我理解为什么会发生的事情,所以我使用viewPager.addOnPageChangeListener
方法在加载FragmentB
时尝试刷新光标。
我还发现了一些关于这个主题的未解答的问题,所以我想,也许我的问题的答案可以在将来为StackOverflow社区的其他人做出贡献。
非常感谢你!
这是我的代码:
MainActivity:
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
DatabaseAdapter databaseAdapter;
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
FragmentManager fragmentManager = getSupportFragmentManager();
final MyFragmentAdapter myFragmentAdapter = new MyFragmentAdapter(fragmentManager);
viewPager.setAdapter(new MyFragmentAdapter(fragmentManager));
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
Log.d(TAG, "onPageScrolled position " + position); ////THIS IS WHERE I THINK CURSOR SHOULD BE REFRESHED SOMEHOW
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
FragmentA:
公共类FragmentA扩展了Fragment {
EditText editText;
Button button;
DatabaseAdapter databaseAdapter;
private static final String TAG = FragmentA.class.getSimpleName();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.d(TAG, "onCreateView 1");
View view = inflater.inflate(R.layout.fragment_a, container, false);
editText = (EditText) view.findViewById(R.id.editText_insert);
button = (Button) view.findViewById(R.id.button_insert);
databaseAdapter = new DatabaseAdapter(getContext());
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String tableName = "profit";
double price = 2;
String insTxt = String.valueOf(editText.getText());
Log.d(TAG, "button click = " + insTxt);
add(tableName);
}
});
return view;
}
//for insert into db
public void add(String tableName){
long id = databaseAdapter.insertData(tableName);
if (id < 0){
Log.d(TAG, "BAD INSERT");
}else {
Log.d(TAG, "INSERT OK!!!!");
}
}
}
FragmentB
公共类FragmentB扩展了Fragment {
private static final String TAG = FragmentB.class.getSimpleName();
ListView listView;
EditText editText;
Button button;
//list view PART
MyCustomAdapter myCustomAdapter;
DatabaseAdapter databaseAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_b, container, false);
Log.d(TAG, " FragmentB onCreateView 1");
databaseAdapter = new DatabaseAdapter(getContext());
listView = (ListView) view.findViewById(R.id.listView);
Cursor cursor = databaseAdapter.cursorLoadingData(); // populate cursor
myCustomAdapter = new MyCustomAdapter(getContext(), cursor);
listView.setAdapter(myCustomAdapter);
return view;
}
}
DatabaseAdapter:
public class DatabaseAdapter {
.....
//part for taking all data from db...
public Cursor cursorLoadingData(){
SQLiteDatabase db = databaseHelper.getWritableDatabase();
String myQuery = " select * from profit order by _id desc";
Cursor cursor = db.rawQuery(myQuery, null);
return cursor;
}
.....
}
MyCustomAdapter - CursorAdapter类:
public class MyCustomAdapter extends CursorAdapter {
private static final String TAG = MyCustomAdapter.class.getSimpleName();
public MyCustomAdapter(Context context, Cursor c) {
super(context, c);
}
public MyCustomAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.custom_list_item, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView textViewId = (TextView) view.findViewById(R.id.textView_id);
long ExtractedId = cursor.getLong(cursor.getColumnIndexOrThrow("_id"));
textViewId.setText(String.valueOf(ExtractedId));
}
}