My activity :
package com.tutos.android.content.provider;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import com.tutos.android.content.provider.SharedInformation.*;
public class ContentProviderExempleActivity extends Activity {
SimpleCursorAdapter adapter;
ListView listview;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content_provider_exemple);
listview = (ListView) findViewById(R.id.listview1);
insertRecords();
displayContentProvider();
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void displayContentProvider() {
String ColumnsCur[] = new String[]{Cours._id,Cours.COURS_NAME,Cours.COURS_DESC};
int toViews[] = new int[]{R.id._id,R.id.Name,R.id.Desc};
Uri mContact = AndroidProvider.CONTENT_URI;
Cursor cur = this.getContentResolver().query(mContact, ColumnsCur, null, null, null);
adapter = new SimpleCursorAdapter(this,R.layout.simple_list_item_1,cur,ColumnsCur,toViews,0);
listview.setAdapter(adapter);
}
private void insertRecords() {
//On crée un ContentValue, puis on appelle la méthode getContentResolver pour récupérer
//une instance de ContentProvider, puis on appelle la méthode insert avec l’uri et les valeurs à insérer.
ContentValues contact = new ContentValues();
contact.put(Cours.COURS_NAME,"Android");
contact.put(Cours.COURS_DESC,"Introduction à la programmation sous Android");
getContentResolver().insert(AndroidProvider.CONTENT_URI,contact);
//on vide le contentValue avant de réinserrer
contact.clear();
contact.put(Cours.COURS_NAME,"Java");
contact.put(Cours.COURS_DESC,"Introduction à la programmation Java");
getContentResolver().insert(AndroidProvider.CONTENT_URI,contact);
contact.clear();
contact.put(Cours.COURS_NAME, "Iphone");
contact.put(Cours.COURS_DESC, "Introduction à l'objectif C");
getContentResolver().insert(AndroidProvider.CONTENT_URI, contact);
}
My Main Activity Layout :
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".ContentProviderExempleActivity">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listview1"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
This think is driving me crazy :) after launching my App , the ListView is empty, notice that my cursor is full and i did use Log.i to check his content and he contains the database.
I've looked up a lot but each tutorial has his own way to do it and in android documentation i found this example that i used but still nothing + there is so much change in Android API that i'm kind of lost :)
Can you help please ? :)
Aucun commentaire:
Enregistrer un commentaire