昨天我做了一个很好的listView来查看我的数据库,但今天我尝试在线添加到我的数据库,我收到一条错误消息,上面写着“数据库泄露.blabla关闭你的数据库”。之后,我无法再将行添加到我的数据库中。
但我重新启动了android studio,我可以添加新行(并且不再出错)!但我看不到我添加的新闻。我只看到我在错误之前添加的行(或者我可以看到我添加的那个导致我错误的行)。我用chrome查看了我的数据库,我看到了所有内容,所以我真的添加了新闻专栏!
我不知道是什么,我检查了几个班级的历史,但我没有做出任何不好的改变(我认为)。如何再次查看我的所有数据库? 我还使用ContentProvider访问我的数据库。
这是我的列表视图中的Fragment类(我在列表视图和一些TextView中使用了2个按钮):
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();
}
}
});
}
}
然后我的listView布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relativeList"
android:descendantFocusability="blocksDescendants">
<TextView
android:text="@string/difficult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView8"
android:layout_below="@+id/textView7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="29dp" />
<TextView
android:text="@string/r_ponse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView7"
android:layout_marginTop="20dp"
android:layout_below="@+id/textView6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/idList"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/themeList"
android:layout_alignStart="@+id/themeList"
tools:ignore="HardcodedText" />
<TextView
android:text="Question :"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView6"
android:layout_marginTop="14dp"
android:layout_below="@+id/textView4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
tools:ignore="HardcodedText" />
<TextView
android:text="@string/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView3"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/textView4"
android:layout_alignEnd="@+id/textView4" />
<TextView
android:text="Thème :"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView4"
android:layout_below="@+id/textView3"
android:layout_alignRight="@+id/textView6"
android:layout_alignEnd="@+id/textView6"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView6"
android:id="@+id/questionList"
android:layout_toRightOf="@+id/themeList"
android:layout_toEndOf="@+id/themeList" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView8"
android:layout_alignLeft="@+id/questionList"
android:layout_alignStart="@+id/questionList"
android:id="@+id/reponseList" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView8"
android:layout_alignRight="@+id/reponseList"
android:layout_alignEnd="@+id/reponseList"
android:id="@+id/difficultList"
android:layout_toEndOf="@+id/reponseList"
android:layout_toRightOf="@+id/reponseList" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/themeList"
android:layout_marginLeft="33dp"
android:layout_marginStart="33dp"
tools:ignore="HardcodedText"
android:layout_below="@+id/idList"
android:layout_toRightOf="@+id/textView8"
android:layout_toEndOf="@+id/textView8" />
<Button
android:text="@string/supprimerb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/buttonDelete"/>
<Button
android:text="@string/modifierB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonModifier"
android:layout_alignTop="@+id/questionList"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignLeft="@+id/buttonDelete"
android:layout_alignStart="@+id/buttonDelete" />
</RelativeLayout>
我的ContentProvider类:
public class CardContentProvider extends ContentProvider {
private StockCard stock;
public static String authority = "com.example.jean.cartememoire.CardContentProvider";
private static String path ="Cartes_table";
public static final String _ID = "_id";
public static final String THEME = "THEME";
public static final String QUESTION = "QUESTION";
public static final String REPONSE = "REPONSE";
public static final String DIFFICULTE = "DIFFICULTE"; //# = un chiffre
public static final String STOCK_TABLE = "Cartes_table";
private static final int ID_STOCK_TABLE = 1;
private static final int ID_THEME = 2;
private static final int ID_QUESTION = 3;
private static final int ID_REPONSE = 4;
private static final int ID_DIFFICULTE = 5;
private static final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
matcher.addURI(authority, STOCK_TABLE, ID_STOCK_TABLE);
matcher.addURI(authority, STOCK_TABLE+"/*", ID_STOCK_TABLE);
}
@Override
public boolean onCreate() {
stock = StockCard.getInstance(getContext());
return true;
}
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = stock.getReadableDatabase();
int code = matcher.match(uri);
long id;
Cursor cursor;
switch (code)
{
case ID_STOCK_TABLE:
cursor = db.query(STOCK_TABLE, projection, selection,
selectionArgs, null, null, sortOrder);
break;
default:
Log.d("Uri provider =", uri.toString());
throw new UnsupportedOperationException("Pas encore implémenté");
}
return cursor;
}
@Nullable
@Override
public String getType(Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = stock.getWritableDatabase();
int code = matcher.match(uri);
long id;
Uri.Builder builder = new Uri.Builder();
switch(code)
{
case ID_STOCK_TABLE:
System.out.println(values.toString());
id = db.insert(STOCK_TABLE, null, values);
builder.appendPath(STOCK_TABLE);
break;
default:
throw new UnsupportedOperationException("Pas encore implémenté");
}
builder.authority(authority);
builder = ContentUris.appendId(builder, id);
return builder.build();
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = stock.getWritableDatabase();
int code = matcher.match(uri);
int i;
switch(code)
{
case ID_STOCK_TABLE:
long id = ContentUris.parseId(uri);
i = db.delete(STOCK_TABLE, "_id=" + id, null);
break;
default:
throw new UnsupportedOperationException("Pas encore implémenté");
}
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
return 0;
}
}
感谢您的帮助。
编辑: 我尝试添加一个光标关闭,但它没有改变任何东西......但是我有一个按钮删除我的数据库中的一行...如果我删除一行,那么我在listView中看不到该行但是我可以看到我之前添加但之前看不到的一行...我想我只能在listView中显示3行,所以我觉得这是listView本身的一个问题。我正在查看listView的文档,看看我想念的是什么......
编辑2:这绝对是一个设计问题,我只有几个项目显示,因为我无法滚动浏览所有项目。这是我的新片段布局,其中包含我的listView:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="layout.ViewCardEditor"
android:background="@android:color/darker_gray">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView" />
</FrameLayout>
之前,layout_height设置为500dp。但是现在我的listView很少,我可以滚动但是我的listView中的所有Text View都被窃听,它们的内容显示来自其他TextView的内容并在我滚动时再次更改。就像textView中的内容在我的其他TextView中滚动一样。
编辑3: 我投票删除我的问题,因为我没有更多的错误,但我的listView有一个新问题,它有一个有限的滚动:我可以看到3项和4项的顶部,我可以滚动一点点位...
答案 0 :(得分:1)
您需要使用ContentResolvers.notifyChange(...)方法通知Cursors
内容已更改。更新数据时,您希望在自定义ContentProvider中执行此操作。此调用允许注册更改通知的游标刷新其数据。