Answer the question
In order to leave comments, you need to log in
Dear comrades, how can SimpleCursorAdapter make its own markup?
I have a database, I made my own markup, I make a request from the database, something goes wrong ... please help me find what the error is)) The request passes, the data is pulled out, but not inserted into the markup ...
DBHelper.java:
package com .example.newadapterexample30;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
public static final String DB_NAME = "restaurant"; // database name
public static final int DB_VERSION = 1; // database version
public static final String TABLE_CONTACTS = "contacts";
public static final String KEY_ID = "ID";
public static final String KEY_NAME = "NAME";
public static final String KEY_DESCRIPTION= "DESCRIPTION";
public static final String KEY_IMAGE = "IMAGE";
public DBHelper(Context context) {
super(context,DB_NAME,null,DB_VERSION );
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_NAME + " TEXT,"
+ KEY_DESCRIPTION + " TEXT,"
+ KEY_IMAGE +" INTEGER" + ")");
cv.put(KEY_NAME, "Igor");
cv.put(KEY_DESCRIPTION, "[email protected]");
cv.put(KEY_IMAGE, R.drawable.americanosmall);
db.insert(TABLE_CONTACTS, null, cv);
cv.put(KEY_NAME, "Dima");
cv.put(KEY_DESCRIPTION, "[email protected]");
cv.put(KEY_IMAGE,R.drawable.blackcoffesmall);
db.insert(TABLE_CONTACTS, null, cv);
cv.put(KEY_NAME, "Alex");
cv.put(KEY_DESCRIPTION, "[email protected]");
cv.put(KEY_IMAGE,R.drawable.cappuccinosmall);
db.insert(TABLE_CONTACTS, null, cv);
}
@Override
public void onUpgrade(SQLiteDatabase db,
}
}
MainActivity.java:
package com.example.newadapterexample30;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public ListView listView;
private SQLiteDatabase db;
private Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView)findViewById(R.id.lv);
SQLiteOpenHelper FirstMealOpenHelper = new DBHelper(this);
db = FirstMealOpenHelper.getWritableDatabase();
try {
cursor = db.query(DBHelper.TABLE_CONTACTS,null,null,null,null,null,null);
Toast.makeText(this, Integer.toString(cursor.getCount()), Toast.LENGTH_SHORT).show();
Toast.makeText(this, Integer.toString(cursor.getColumnCount()), Toast.LENGTH_LONG).show();
String from [] = new String[]{DBHelper.KEY_NAME,DBHelper.KEY_DESCRIPTION,DBHelper.KEY_IMAGE};
int[] to = new int[] {R.id.tvName, R.id.tvEmail ,R.id.img};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.itam,cursor,from,to,0);
listView.setAdapter(adapter);
} catch (Exception e) {
Toast.makeText(this,"Error", Toast.LENGTH_LONG).show();
}
db.close();
}
}
main.xml:
itam.xml:
Thanks in advance to everyone who responds!)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question