Java 安卓SQLite,can';在我的listView中不再查看数据
昨天我制作了一个很好的列表视图来查看我的数据库,但今天我试图在线添加到我的数据库,我收到一条错误消息,上面写着“数据库泄漏。blabla关闭你的数据库”。在那之后,我再也不能向我的数据库添加行了 但我重新启动了android studio,我可以添加新的行(并且不再出错)!但我看不到我添加的新闻行。我只看到我在错误之前添加的行(或者我可以看到我添加的导致错误的行)。我用chrome检查了我的数据库,我看到了一切,所以我真的添加了新闻行 我不知道发生了什么事,我检查了好几节课的历史,但我没有做出任何不好的改变(我想)。如何再次查看所有数据库? 我还使用ContentProvider访问我的数据库 下面是带有列表视图的片段类(我在列表视图和一些文本视图的元素中使用2个按钮): 然后,我的listView布局:Java 安卓SQLite,can';在我的listView中不再查看数据,java,android,sql,listview,android-contentprovider,Java,Android,Sql,Listview,Android Contentprovider,昨天我制作了一个很好的列表视图来查看我的数据库,但今天我试图在线添加到我的数据库,我收到一条错误消息,上面写着“数据库泄漏。blabla关闭你的数据库”。在那之后,我再也不能向我的数据库添加行了 但我重新启动了android studio,我可以添加新的行(并且不再出错)!但我看不到我添加的新闻行。我只看到我在错误之前添加的行(或者我可以看到我添加的导致错误的行)。我用chrome检查了我的数据库,我看到了一切,所以我真的添加了新闻行 我不知道发生了什么事,我检查了好几节课的历史,但我没有做出任
您需要使用方法通知
游标
内容已更改。更新数据时,您将希望在自定义ContentProvider中执行此操作。此调用允许注册更改通知的游标刷新其数据。请查看。似乎你也有类似的问题。
public class ViewCardEditor extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
public static final String authority = "com.example.jean.cartememoire.CardContentProvider";
public String[] from;
public final int[] to = {R.id.idList, R.id.themeList, R.id.questionList, R.id.reponseList, R.id.difficultList};
StockCard stock;
ViewGroup container;
ListView listView;
MySimpleCursorAdapter adapter;
private ArrayList<String> data = new ArrayList<String>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup c,
Bundle savedInstanceState) {
container = c;
View view = inflater.inflate(R.layout.fragment_view_card_editor, container, false);
stock = StockCard.getInstance(container.getContext());
from = new String[]{stock._ID,
stock.THEME,
stock.QUESTION,
stock.REPONSE,
stock.DIFFICULTE};
// Inflate the layout for this fragment
if (container != null) {
container.removeAllViews();
}
databaseView(view);
return view;
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Uri.Builder builder = new Uri.Builder();
Uri uri = builder.scheme("content").authority(authority)
.appendPath(stock.STOCK_TABLE).build();
return new CursorLoader(container.getContext(), uri, from,
null, null, null);
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
adapter.swapCursor(data);
}
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}
public void databaseView(View view)
{
ArrayList<String> list;
Cursor cursor = stock.getData();
listView = (ListView) view.findViewById(R.id.listView);
adapter = new MySimpleCursorAdapter(container.getContext(), R.layout.card_stock, null, from, to,0);
listView.setAdapter(adapter);
LoaderManager manager = getLoaderManager();
manager.initLoader(0, null, this);
}
public void deleteOneCard(int id)
{
Uri.Builder builder = new Uri.Builder();
builder.scheme("content").authority(authority).appendPath(stock.STOCK_TABLE);
ContentUris.appendId(builder, id);
Uri uri = builder.build();
ContentResolver resolver = container.getContext().getContentResolver();
resolver.delete(uri, null, null);
}
private class MySimpleCursorAdapter extends SimpleCursorAdapter
{
ViewHolder vh;
public MySimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
public View newView(Context _context, Cursor _cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) _context.getSystemService(_context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.card_stock, parent, false);
vh = new ViewHolder();
vh.idList = (TextView) view.findViewById(R.id.idList);
vh.themeList = (TextView) view.findViewById(R.id.themeList);
vh.questionList = (TextView) view.findViewById(R.id.questionList);
vh.reponseList = (TextView) view.findViewById(R.id.reponseList);
vh.difficulteList = (TextView) view.findViewById(R.id.difficultList);
vh.Supprimer = (Button) view.findViewById(R.id.buttonDelete);
vh.Modifier = (Button) view.findViewById(R.id.buttonModifier);
view.setTag(vh);
return view;
}
public void bindView(View view, Context Context, Cursor cursor) {
vh.idList.setText(cursor.getString(cursor.getColumnIndex(stock._ID)));
vh.themeList.setText(cursor.getString(cursor.getColumnIndex(stock.THEME)));
vh.questionList.setText(cursor.getString(cursor.getColumnIndex(stock.QUESTION)));
vh.reponseList.setText(cursor.getString(cursor.getColumnIndex(stock.REPONSE)));
vh.difficulteList.setText(cursor.getString(cursor.getColumnIndex(stock.DIFFICULTE)));
vh.Supprimer.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
View parentView = (View)v.getParent();
TextView idList = (TextView) parentView.findViewById(R.id.idList);
/*TextView themeList = (TextView) parentView.findViewById(R.id.themeList);
TextView questionList = (TextView) parentView.findViewById(R.id.questionList);
TextView reponseList = (TextView) parentView.findViewById(R.id.reponseList);
TextView difficulteList = (TextView) parentView.findViewById(R.id.difficultList);*/
int id = Integer.parseInt(idList.getText().toString());
deleteOneCard(id);
Toast.makeText(container.getContext(), "Suppression de "+id, Toast.LENGTH_SHORT).show();
}
});
vh.Modifier.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
View parentView = (View)v.getParent();
TextView idList = (TextView) parentView.findViewById(R.id.idList);
TextView themeList = (TextView) parentView.findViewById(R.id.themeList);
themeList.setFocusable(true);
themeList.requestFocus();
/*TextView questionList = (TextView) parentView.findViewById(R.id.questionList);
TextView reponseList = (TextView) parentView.findViewById(R.id.reponseList);
TextView difficulteList = (TextView) parentView.findViewById(R.id.difficultList);*/
int id = Integer.parseInt(idList.getText().toString());
Toast.makeText(container.getContext(), "bouton Modifier pour "+id, Toast.LENGTH_SHORT).show();
}
});
}
}
public class ViewHolder
{
Button Supprimer, Modifier;
TextView idList, themeList, questionList, reponseList, difficulteList;
}
}
public class AddCardEditor extends Fragment {
EditText editTheme, editQuest, editRep;
RatingBar ratingBar;
Button buttonCreateCard;
CardContentProvider stock;
ViewGroup container;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup c,
Bundle savedInstanceState) {
View view;
view = inflater.inflate(R.layout.fragment_add_card_editor, container, false);
// Inflate the layout for this fragment
container = c;
if (container != null) {
container.removeAllViews();
}
editTheme =(EditText) view.findViewById(R.id.editTheme);
editQuest =(EditText) view.findViewById(R.id.editQuest);
editRep =(EditText) view.findViewById(R.id.editRep);
ratingBar =(RatingBar) view.findViewById(R.id.ratingBar);
buttonCreateCard =(Button) view.findViewById(R.id.buttonCreateCard);
stock = new CardContentProvider();
addData(container);
return view;
}
public void addData(final ViewGroup container)
{
buttonCreateCard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try
{
ContentValues cv = new ContentValues();
CardContentProvider cp = new CardContentProvider();
cv.put(stock.THEME, editTheme.getText().toString());
cv.put(stock.QUESTION, editQuest.getText().toString());
cv.put(stock.REPONSE, editRep.getText().toString());
cv.put(stock.DIFFICULTE, ratingBar.getRating());
ContentResolver resolver = container.getContext().getContentResolver();
Uri.Builder builder = new Uri.Builder();
System.out.println("addDATA DEBUG");
builder.scheme("content").authority(stock.authority).appendPath(stock.STOCK_TABLE);
System.out.println("addDATA DEBUG2");
Uri uri = builder.build();
uri = resolver.insert(uri,cv);
long id = ContentUris.parseId(uri);
System.out.println("addDATA DEBUG3 : "+id);
Toast.makeText(container.getContext(), "Carte créé avec succès !", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(container.getContext(), "Erreur, echec de création de la carte", Toast.LENGTH_SHORT).show();
}
}
});
}
}