使用内容提供商填充Listview

时间:2017-09-16 19:07:11

标签: android listview

我正在使用以下Content Provider来填充Listview,但Listview中没有填充任何内容。我不确定如何使用主活动中的光标来填充Listview。

内容提供商的代码如下:

public class DataProvider extends ContentProvider{

DBHelper alarsmDB;
private static final int ALARMS = 100;
private static final int ALARMS_ID = 110;
public static final String PROVIDER_NAME = "com.domain.app.DataProvider";
public static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER_NAME + "/alarms" );
static final String NAME = "name";

private static final UriMatcher sURIMatcher = new UriMatcher(
        UriMatcher.NO_MATCH);
static {
    sURIMatcher.addURI(PROVIDER_NAME, "alarms", ALARMS);
    sURIMatcher.addURI(PROVIDER_NAME, "alarms" + "/#", ALARMS_ID);
}

@Override
public String getType(Uri uri) {
    return "vnd.android.cursor.dir/vnd.com.domain.app.provider.alarms";
}

@Override
public boolean onCreate() {
    Context context = getContext();
    alarsmDB = new DBHelper(context);
    return true;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection,
                    String[] selectionArgs, String sortOrder) {
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setTables(alarsmDB.ALARMS_TABLE_NAME);

    int uriType = sURIMatcher.match(uri);
    switch (uriType) {
        case ALARMS_ID:
            queryBuilder.appendWhere(alarsmDB.ALARMS_COLUMN_NAME + "="
                    + uri.getLastPathSegment());
            break;
        case ALARMS:
            // no filter
            break;
        default:
            throw new IllegalArgumentException("Unknown URI");
    }

    Cursor cursor = queryBuilder.query(alarsmDB.getReadableDatabase(),
            projection, selection, selectionArgs, null, null, sortOrder);
    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    return cursor;
}

@Override
public Uri insert(Uri uri, ContentValues values) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public int update(Uri uri, ContentValues values, String selection,
                  String[] selectionArgs) {
    // TODO Auto-generated method stub
    return 0;
}

虽然主要活动的代码如下:

public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {
private ListView alarmlv;
DBHelper mydb;

private static final String PROVIDER_NAME = "com.domain.app.alarms";
private static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER_NAME + "/alarms");

SimpleCursorAdapter dataAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mydb = new DBHelper(this);
    Cursor cursor = getContentResolver().query(CONTENT_URI, null, null, null, "name");
    alarmlv = (ListView)findViewById(R.id.listViewAlarms);

    dataAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.listview_row, null,
            new String[]{ mydb.ALARMS_COLUMN_NAME }, new int[]{R.id.alarm_name}, 0);

    alarmlv.setAdapter(dataAdapter);
    refreshValuesFromContentProvider();

   alarmlv.setOnItemClickListener(new OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                long arg3) {
            String itemVal =(String) arg0.getItemAtPosition(arg2);

            Bundle dataBundle = new Bundle();
            dataBundle.putString("name", itemVal);
            Intent intent = new
                    Intent(getApplicationContext(),DisplayAlarm.class);
            intent.putExtras(dataBundle);
            startActivity(intent);
        }
    });

}

谢谢。

约瑟夫

1 个答案:

答案 0 :(得分:0)

我将在此后发布最初发布的功能代码,以防其他人在使用Content Provider填充ListView时出现问题。感谢pskink和Nabin在subsidiary question

提供的帮助

内容提供商代码:

public class DataProvider extends ContentProvider{
DBHelper alarsmDB;
private static final int ALARMS = 1;
public static final String PROVIDER_NAME = "com.domain.app.alarms";
public static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER_NAME + "/alarms" );
static final String NAME = "name";

private static final UriMatcher URIMatcher = new UriMatcher(
        UriMatcher.NO_MATCH);
static {
    URIMatcher.addURI(PROVIDER_NAME, "alarms", ALARMS);
}

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

@Override
public boolean onCreate() {
    Context context = getContext();
    alarsmDB = new DBHelper(context);
    return true;
}

public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

    if(URIMatcher.match(uri)==ALARMS){
        return alarsmDB.getAllAlarms();
    }else{
        return null;
    }
}

@Override
public Uri insert(Uri uri, ContentValues values) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public int update(Uri uri, ContentValues values, String selection,
                  String[] selectionArgs) {
    // TODO Auto-generated method stub
    return 0;
}

}

主要活动代码:

public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {
private ListView alarmlv;
DBHelper mydb;

private static final String PROVIDER_NAME = "com.domain.app.alarms";
private static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER_NAME + "/alarms");

SimpleCursorAdapter dataAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mydb = new DBHelper(this);

    Cursor cursor = getContentResolver().query(CONTENT_URI, null, null, null, "name");

    alarmlv = (ListView)findViewById(R.id.listViewAlarms);

    dataAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.listview_row, cursor,
            new String[]{ mydb.ALARMS_COLUMN_NAME }, new int[]{R.id.alarm_name}, 0);
    alarmlv.setAdapter(dataAdapter);
    getSupportLoaderManager().initLoader(0, null, this);

   alarmlv.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Cursor cursor = (Cursor) alarmlv.getItemAtPosition(position);
            String itemVal = cursor.getString(cursor.getColumnIndex(mydb.ALARMS_COLUMN_NAME));

            Bundle dataBundle = new Bundle();
            dataBundle.putString("name", itemVal);
            Intent intent = new
                    Intent(getApplicationContext(),DisplayAlarm.class);
            intent.putExtras(dataBundle);
            startActivity(intent);
        }
    });
}