- Forum-Beiträge: 23
07.02.2014, 16:38:30 via Website
07.02.2014 16:38:30 via Website
ich haben mich nach längerer Zeit entschlossen meine APP auf den LOADER in Verbindung mit dem Content Provider umzustellen, leider habe ich nun bei der Übergabe von einer Activity zur anderen Activity Probleme. Ich bekomme einfach keine ID zum ContentProvider übergeben , um die Datenbankabfrage zu machen. Vielleicht habt ihr einen Tipp für mich.
Meine erste Activity , dort wird in einer ListView Kategorien angezeigt, das geht, von da aus , sollen passende Fragen anhand der übergebenden ID in der zweiten Activity in einer Liestview angezeigt werden, die ID wird nicht an die Abfrage im Content übergeben ( Das ist mein Problem
2
3
4 @Override
5 public void onItemClick(AdapterView<?> arg0, View view, int arg2,
6 long arg3) {
7 // Auswahl eines Lernfeldes
8
9 // Übergabe der album_id an die Fragen Activity
10 //String album_id = ((TextView) view.findViewById(R.id.album_id)).getText().toString();
11
12 Intent i = new Intent(getApplicationContext(), AntwortActivity.class);
13 //i.putExtra("album_id", album_id);
14
15 Uri selectedUri = Uri.parse(Customer.CONTENT_FRAGE_TYPE + "/" + arg3);
16 i.putExtra(Customer.CONTENT_FRAGE_TYPE_ID, selectedUri);
17
18
19 Log.d("Questions", "Start! "+i);
20 startActivity(i);
21
22
23 }
24 public void onNothingSelected(AdapterView<?>arg0){
25 }});
26
27 }
Mein Content Provider
2
3 public static final String PROVIDER_NAME = "com.mobimaler.mobi.helper.customer";
4
5 //Tabelle Lernfelder
6 public static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER_NAME + "/customers" );
7 //Tabelle Fragen
8 public static final Uri CONTENT_URI2 = Uri.parse("content://" + PROVIDER_NAME + "/fragen" );
9 //Tabelle Antworten
10 //public static final Uri CONTENT_URI3 = Uri.parse("content://" + PROVIDER_NAME + "/customers3" );
11
12 /** Constants to identify the requested operation */
13
14 public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE +"/customers";
15 public static final String CONTENT_ITEM_TYPE_ID = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/customer";
16
17 public static final String CONTENT_FRAGE_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/fragen";
18 public static final String CONTENT_FRAGE_TYPE_ID = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/frage";
19
20
21 private static final int CUSTOMERS = 1;
22 private static final int CUSTOMERS_ID = 2;
23
24 private static final int FRAGEN = 3;
25 private static final int FRAGEN_ID = 4;
26
27 //private static final int CUSTOMERS3 = 1;
28
29 private static final UriMatcher uriMatcher ;
30 static {
31 //Tabelle Lernfelder
32 uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
33 //Lernfelder
34 uriMatcher.addURI(PROVIDER_NAME, "customers", CUSTOMERS);
35 uriMatcher.addURI(PROVIDER_NAME, "customers/#", CUSTOMERS_ID);
36
37 uriMatcher.addURI(PROVIDER_NAME, "fragen", FRAGEN);
38 uriMatcher.addURI(PROVIDER_NAME, "fragen/#", FRAGEN_ID);
39
40 }
41
42 /** This content provider does the database operations by this object */
43 LernfelderDbAdapter mLernfelderDB;
44 FragenDbAdapter mFragenDB;
45
46 /** A callback method which is invoked when the content provider is starting up */
47 @Override
48 public boolean onCreate() {
49 mLernfelderDB = new LernfelderDbAdapter(getContext());
50 mFragenDB = new FragenDbAdapter(getContext());
51 return true;
52 }
53
54
55 @Override
56 public String getType(Uri uri) {
57 return null;
58 }
59
60
61 /** A callback method which is by the default content uri */
62 @Override
63 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
64
65 // Objekt-ID:
66 String id;
67
68
69 int uriType = uriMatcher.match(uri);
70 switch (uriType) {
71 case CUSTOMERS:
72 mLernfelderDB.open();
73 //id = uri.getLastPathSegment();
74 return mLernfelderDB.getAllFelder();
75
76
77 //break;
78 case FRAGEN:
79 mFragenDB.open();
80 id = uri.getLastPathSegment();
81 // id wird hier leider nicht übergeben, das ist mein Problem
82 return mFragenDB.getTitle(id);
83
84 //break;
85 }
86 getContext().getContentResolver().notifyChange(uri, null);
87 return null;
88
89 }
90
91
92
93 private String addId(String selection, int id) {
94 // TODO Automatisch generierter Methodenstub
95 return null;
96 }
97
98
99 private Cursor getIntent() {
100 // TODO Automatisch generierter Methodenstub
101 return null;
102 }
103
104
105 @Override
106 public int delete(Uri uri, String selection, String[] selectionArgs) {
107 // TODO Auto-generated method stub
108 return 0;
109 }
110
111
112 @Override
113 public Uri insert(Uri uri, ContentValues values) {
114 // TODO Auto-generated method stub
115 return null;
116 }
117
118
119 @Override
120 public int update(Uri uri, ContentValues values, String selection,
121 String[] selectionArgs) {
122 // TODO Auto-generated method stub
123 return 0;
124 }
125
126
127}
Die zweite Activity
2 ListView lListView;
3
4 private Uri selectedUri;
5
6 @Override
7 protected void onCreate(Bundle savedInstanceState) {
8 super.onCreate(savedInstanceState);
9 setContentView(R.layout.activity_fragen);
10
11
12
13 Bundle extras = getIntent().getExtras();
14 selectedUri = (savedInstanceState == null) ? null : (Uri) savedInstanceState
15 .getParcelable(Customer.CONTENT_FRAGE_TYPE_ID);
16
17 if (extras != null) {
18 selectedUri = extras
19 .getParcelable(Customer.CONTENT_FRAGE_TYPE_ID);
20
21 }
22
23 String i = selectedUri.getLastPathSegment();
24
25
26 //int i = Integer.parseInt(getIntent().getExtras().getString("album_id"));
27
28 Log.d("Antwort", "LastPathSegment! "+i);
29 lListView = (ListView) findViewById(R.id.list);
30
31
32
33 lAdapter = new SimpleCursorAdapter(getBaseContext(),
34 R.layout.activity_fragen_list,
35 null,
36 new String[] { FragenDbAdapter.TAG_ID_FELD, FragenDbAdapter.TAG_ID_FRAGE, FragenDbAdapter.TAG_COUNT, FragenDbAdapter.TAG_NAME, FragenDbAdapter.TAG_ALBUM },
37 new int[] { R.id.album_id, R.id.song_id, R.id.track_no, R.id.album_name, R.id.song_duration }, 0);
38
39 lListView.setAdapter(lAdapter);
40
41 getSupportLoaderManager().initLoader(0, null, this);
42
43
44
45
46 }
47
48
49 @Override
50 public boolean onCreateOptionsMenu(Menu menu) {
51 // Inflate the menu; this adds items to the action bar if it is present.
52 getMenuInflater().inflate(R.menu.activity_fragen, menu);
53 return true;
54 }
55
56 @Override
57 public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
58 Uri uri = Customer.CONTENT_URI2;
59 return new CursorLoader(this, uri, null, null, null, null);
60 }
61
62 @Override
63 public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
64 lAdapter.swapCursor(arg1);
65 }
66
67 @Override
68 public void onLoaderReset(Loader<Cursor> arg0) {
69 lAdapter.swapCursor(null);
70 }
71
72}
Ich hoffe ihr könnt mir das weiterhelfen.
Grüße Ralf
P.S. Habe ein wenig weiter probiert, leider aber nicht weitergekommen
In meiner zweiten Aktivity wirf er mir im Log den Richtigen Pfad aus
LastPathSegment! content://com.mobimaler.mobi.helper.customer/fragen/vnd.android.cursor.dir/fragen/5
Diesen Code verwende ich da
2 selectedUri = (savedInstanceState == null) ? null : (Uri) savedInstanceState
3 .getParcelable(Customer.CONTENT_ITEM_TYPE);
4
5 if (extras != null) {
6 selectedUri = extras
7 .getParcelable(Customer.CONTENT_ITEM_TYPE);
8
9 }
10
11 //String i = selectedUri.getLastPathSegment();
12 Uri uri = Uri.parse(Customer.CONTENT_URI2 + "/" + selectedUri);
13 String i = null;
14 getContentResolver().query(uri, null, null, null, i);
15
16 Log.d("Antwort", "LastPathSegment! "+uri);
Nur leider bei meinem Provider kommt nichts mehr von der ID da an, verstehe nicht wieso
Dort habe ich folgenden Code
2 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
3
4 // Objekt-ID:
5 long id;
6 int uriType = uriMatcher.match(uri);
7 switch (uriType) {
8 case CUSTOMERS:
9 mLernfelderDB.open();
10 //id = uri.getLastPathSegment();
11 return mLernfelderDB.getAllFelder();
12 //mLernfelderDB.getAllFelder();
13
14 //break;
15 case FRAGEN:
16 mFragenDB.open();
17 id = Long.parseLong(uri.getLastPathSegment()); /// Hier kommt einfach nichts an ,letzter Wert ist das Wort fragen, es sollte allerdings die die id 5 sein
18
19 return mFragenDB.getTitle(id);
20
21 }
22 getContext().getContentResolver().notifyChange(uri, null);
23 return null;
24
25 }
Die Sufu hat leider noch kein Licht ins dunkle gebracht
— geändert am 07.02.2014, 21:27:11
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.