昨天我做了一个很好的列表视图来查看我的数据库,但是今天我试图在我的数据库中添加一个online,我收到了一条错误消息,上面写着“数据库泄漏”。关闭你的数据库。之后,我不能再添加行到我的数据库。
但我重新启动了android工作室,我可以添加新的行(不再出错)!但我看不到我加的新闻台词。我只看到我在错误之前添加的行(或者我可以看到我添加的导致错误的行)。我用chrome检查了我的数据库,我看到了所有的东西,所以我真的添加了新闻行!
我不知道发生了什么事,我查了好几节课的历史,但我没有做任何不好的改变(我想)。如何再次查看所有数据库?我还使用contentprovider访问我的数据库。
下面是我的带有列表视图的片段类(我在列表视图和一些文本视图的元素中使用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();
}
}
});
}
}
然后我的列表视图布局:
<?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>
以前,布局高度设置为500dp左右。但是现在我的listview很小,我可以滚动,但是我的listview中的所有文本视图都被窃听了,它们的内容显示来自其他textview的内容,并且在我滚动时再次更改。这就像文本视图中的内容与我在其他文本视图中滚动一样。
编辑3:我投票删除我的问题,因为我没有更多的错误,但我有一个新的问题,我的listview,它有一个有限的滚动:我可以看到3个项目和4个项目的顶部,我可以滚动一点点。。。
1条答案
按热度按时间93ze6v8z1#
你需要通知我
Cursors
使用contentresolvers.notifychange(…)方法更改了内容。更新数据时,您将希望在自定义contentprovider中执行此操作。此调用允许注册更改通知的游标刷新其数据。