Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从AlertDialog刷新列表视图_Android_Android Contentprovider_Android Alertdialog_Simplecursoradapter_Listactivity - Fatal编程技术网

Android 从AlertDialog刷新列表视图

Android 从AlertDialog刷新列表视图,android,android-contentprovider,android-alertdialog,simplecursoradapter,listactivity,Android,Android Contentprovider,Android Alertdialog,Simplecursoradapter,Listactivity,我需要使用ContentProvider(当前为数据库表)中的新数据刷新ListView。逻辑从AlertDialog开始,该对话框允许使用自定义ContentProvider添加字符串(当前是硬编码的时间戳),该自定义ContentProvider在表上执行CRUD任务。添加时,从AlertDialog对象开始运行setPositiveButton,它解析ContentProvider以执行数据库插入。之后(正确添加)需要使用最近添加的新字符串刷新ListView 我遵循安卓网站的指南并混合了

我需要使用ContentProvider(当前为数据库表)中的新数据刷新ListView。逻辑从AlertDialog开始,该对话框允许使用自定义ContentProvider添加字符串(当前是硬编码的时间戳),该自定义ContentProvider在表上执行CRUD任务。添加时,从AlertDialog对象开始运行
setPositiveButton
,它解析ContentProvider以执行数据库插入。之后(正确添加)需要使用最近添加的新字符串刷新ListView

我遵循安卓网站的指南并混合了一些代码:

  • 我正在使用LoaderManager.LoaderCallbacks接口
  • 使用(仅用于学习)内容提供者
在尝试使用ArrayAdapter对象和ListView刷新之前,请确定。但是自从我添加了SimpleCorsOrAdapter和Loader之后,我就不能让它刷新了

我已经尝试了(几乎,我认为)一切,比如:

  • 在插入后添加方法
    mAdapter.notifyDataSetNotifyChanged()
    ,但不起作用
  • 添加runOnUiThread(新的Runnable(){mAdapter.notifyDataSetNotifyChanged();})如建议,但不起作用
  • ((SimpleCursorAdapter)this.getListAdapter()).swap(…)
    ((SimpleCursorAdapter)this.getListAdapter()).notifyDataSetNotifyChanged()
    思考可能是一个参考问题,但不起作用
测试物品:

  • 添加
    getContext().getContentResolver().notifyChange(Uri,ContentObserver)但不知道如何传递第二个参数
适用于我的代码是每次用户单击肯定按钮时使用
getLoaderManager().restartLoader()
。但对我来说(语义上)是不正确的,因为适配器使用观察者模式,但我不能使它和通知逻辑一起工作

如您所见,这是一个使用ListActivity、SimpleCursorAdapter、ContentProviders和LoaderManager的非常简单的示例。如果你有我期望的代码(使用观察员通知),请分享。仍然认为这些课程一定很好,但我做错了

提前谢谢


ListViewTest.java ListTextViewDbHelper.java
目前正在处理此问题,但仍不知道原因:
getContext().getContentResolver().notifyChange(“content://cl.jago.listviewtest/“,空)
after insert()方法。
package cl.jago.listviewtest;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.LoaderManager;
import android.content.ContentValues;
import android.content.Context;
import android.content.CursorLoader;
import android.content.DialogInterface;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SimpleCursorAdapter;

import java.util.Date;


public class ListActivityTest extends ListActivity implements LoaderManager.LoaderCallbacks<Cursor> {

    // This is the Adapter being used to display the list's data
    SimpleCursorAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    this.setContentView( R.layout.layout_listado );

    // Tutorial: Api Guides > ListViews

    // For the cursor adapter, specify which columns go into which views
    String[] fromColumns = { "nombre"};
    int[] toViews = {android.R.id.text1}; // The TextView in simple_list_item_1

    // Create an empty adapter we will use to display the loaded data.
    // We pass null for the cursor, then update it in onLoadFinished()
    mAdapter = new SimpleCursorAdapter( this, android.R.layout.simple_list_item_1, null,
                        fromColumns, toViews, 0 );
    setListAdapter(mAdapter);

    // Prepare the loader.  Either re-connect with an existing one,
    // or start a new one.
    getLoaderManager().initLoader( 0, null, this );
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_list_activity_test, menu);
    return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.agregar) {

        // 1. Instantiate an AlertDialog.Builder with its constructor
        AlertDialog.Builder builder = new AlertDialog.Builder( this );

        // Add the buttons
        builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {

            ContentValues cv = new ContentValues();
            cv.put("nombre", (new Date()).getTime());
            getContentResolver().insert(ListViewContentProvider.uri, cv);
        }
        } );

        builder.setNegativeButton( R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // User cancelled the dialog
        }
        } );

        // 2. Chain together various setter methods to set the dialog characteristics
        builder.setMessage(R.string.dialog_message).setTitle(R.string.dialog_title);

        // 3. Get the AlertDialog from create()
        AlertDialog dialog = builder.create();
        dialog.show();

        return true;
    }

    return super.onOptionsItemSelected(item);
    }

    // Called when a new Loader needs to be created
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {

    // Now create and return a CursorLoader that will take care of
    // creating a Cursor for the data being displayed.
    // return new CursorLoader(this, ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null, null);
    return new CursorLoader( this, ListViewContentProvider.uri, new String[]{"_id", "nombre"}, null, null, null);
    }

    // Called when a previously created loader has finished loading
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    // Swap the new cursor in.  (The framework will take care of closing the
    // old cursor once we return.)
    mAdapter.swapCursor(data);
    }

    // Called when a previously created loader is reset, making the data unavailable
    public void onLoaderReset(Loader<Cursor> loader) {
    // This is called when the last Cursor provided to onLoadFinished()
    // above is about to be closed.  We need to make sure we are no
    // longer using it.
    mAdapter.swapCursor(null);
    }
}
package cl.jago.listviewtest;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class ListViewContentProvider extends ContentProvider {

    public static  Uri uri = Uri.parse( "content://cl.jago.listviewtest/listview" );
    private ListViewDbHelper mOpenHelper;

    @Override
    public boolean onCreate() {

    mOpenHelper = new ListViewDbHelper( getContext() );
    return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    SQLiteDatabase db = mOpenHelper.getReadableDatabase();
    Cursor c = db.query( "listview", new String[] { "_id", "nombre" }, null, null, null, null, null );
    return c;
    }

    @Override
    public String getType(Uri uri) {
    return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {

    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    long id = db.insertOrThrow( "listview", null, values );
    return Uri.parse( "content://cl.jago.listviewtest/listview/"+id );
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
    return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    return 0;
    }
}
package cl.jago.listviewtest;

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class ListViewDbHelper  extends SQLiteOpenHelper {

    public ListViewDbHelper( Context context ) {
        super(context, "listview", null, 1 );
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table listview ( _id integer primary key, nombre text ) " );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}