मैं एक एंड्रॉइड ऐप प्रोग्रामिंग कर रहा हूं जिसे डेटा स्टोर करने और इसे पढ़ने के लिए डेटाबेस का उपयोग करना चाहिए। this tutorial (on archive.org) का उपयोग करके मुझे एक डेटाबेस बनाने के लिए ऐप मिला और मैं नई प्रविष्टियां बनाने में सक्षम हूं, हालांकि, मुझे नहीं पता कि सूची सूची में संग्रहीत डेटा प्राप्त करने के लिए डेटाबेस को कैसे पढ़ा जाए। मुझे पता है कि इस वेबसाइट पर कई समान प्रश्न हैं लेकिन ऐसा लगता है कि उनमें से कोई भी रास्ते पर लागू नहीं होता है, ट्यूटोरियल से डेटाबेस काम करता है।एंड्रॉइड: डेटाबेस से डेटा देखने के लिए SimpleCursorAdapter का उपयोग
कोड:
import java.util.Calendar;
import maturarbeit.nicola_pfister.studenttools.database.DBAdapter;
import android.app.AlertDialog.Builder;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;
public class Marks extends ListActivity {
DBAdapter db = new DBAdapter(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.marks);
}
@Override
protected void onPause() {
super.onPause();
db.close();
}
@Override
protected void onResume() {
super.onResume();
db.open();
getData();
}
@SuppressWarnings("deprecation")
private void getData() {
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
db.getAllMarks(),
new String[] { "value" },
new int[] { android.R.id.text1 });
ListView listView = (ListView) findViewById(R.id.marks_list);
listView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.marks, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_add:
Calendar cal = Calendar.getInstance();
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH);
final String date = day + "." + month;
Builder builder = new Builder(this);
builder
.setTitle(R.string.dialog_addmarks)
.setItems(R.array.markslist, new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
@SuppressWarnings("unused")
long id;
String selection = getResources().getStringArray(R.array.markslist)[which];
id = db.insertMark(date, "Default", selection);
}
})
.show();
getData();
break;
case R.id.menu_delete:
//Deleting function yet to be implemented.
break;
}
return super.onOptionsItemSelected(item);
}
}
संपादित करें: सूची: के बाद से यह होने के लिए एंड्रॉयड था ListView आईडी गलत था।
धन्यवाद। मुझे लगता है कि मैं समझता हूं, आप क्या समझाने की कोशिश कर रहे हैं। मैंने आपके एडाप्टर का उपयोग किया लेकिन मुझे अभी भी एक अपवाद मिलता है जो मेरे ऐप को क्रैश करता है। मुझे लगता है कि त्रुटि कहीं और है? – BlueHazard
लॉगकैट कॉपी और पेस्ट करें (अपने प्रश्न के निचले बाएं हिस्से में संपादन लिंक का उपयोग करें)। मैं एक नजर मार लूगां। जब आपने यह किया है तो मुझे एक टिप्पणी में टैग करें ("@ सैम" के साथ)। – Sam
मैंने अपने प्रश्न में लॉगकैट जोड़ा। मेरी सहायता करने के लिए धन्यवाद। – BlueHazard