顯示項目的一排按鈕到Android XML我想從我的SQL數據庫顯示,象這樣的數據中的每一行我想有一個新的行,如果有顯示的食物它可以向下滾動太多。該行將有一個圖片foodname和一個訂單按鈕。任何人有任何想法做到這一點?我想用java
我在我的數據庫中有三列食物。名稱,描述,價格,imagesrc
SQL數據庫的每一行都會有一個行對象。如果有超過5row超出手機頁面可以滾動下來,我想這樣做:
顯示項目的一排按鈕到Android XML我想從我的SQL數據庫顯示,象這樣的數據中的每一行我想有一個新的行,如果有顯示的食物它可以向下滾動太多。該行將有一個圖片foodname和一個訂單按鈕。任何人有任何想法做到這一點?我想用java
我在我的數據庫中有三列食物。名稱,描述,價格,imagesrc
SQL數據庫的每一行都會有一個行對象。如果有超過5row超出手機頁面可以滾動下來,我想這樣做:
爲了這個目的,你可以使用RecyclerView,你可以瞭解更多關於它在這裏:https://www.survivingwithandroid.com/2016/09/android-recyclerview-tutorial.html
1.)創建的主要活動內的列表視圖。 和每個列表項目的佈局。
2)創建SQLiteOpenHelper數據庫(在互聯網上這麼多的教程) 有變量:
INT的ImageSource, 字符串名稱, 字符串描述, INT價格,
使用該其他代碼我已經爲我的項目之一創建以供參考:
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "eventList.db";
public static final String TABLE_NAME = "event_table";
public static final String COL_ID = "_id";
public static final String COL_NAME = "EVENTNAME";
public static final String COL_UNIXTIME = "UNIXTIMESTAMP";
public static final String COL_PARTICIPANTS = "PARTICIPANTS";
public static final String COL_LOCATION = "LOCATION";
public static final String COL_LOCATIONNAME = "LOCATIONNAME";
public static final String COL_SUMMARY = "SUMMARY";
private Context context;
private static final String[] ALL_COLUMNS = new String[]{
COL_ID,COL_NAME,COL_UNIXTIME,COL_PARTICIPANTS,COL_LOCATION,COL_LOCATIONNAME,COL_SUMMARY
};
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1/**version**/);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String createTable = "CREATE TABLE " + TABLE_NAME + "(" +
COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL_NAME + " TEXT," +
COL_UNIXTIME + " INTEGER," +
COL_PARTICIPANTS + " INTEGER," +
COL_LOCATION + " TEXT," +
COL_LOCATIONNAME + " TEXT," +
COL_SUMMARY + " TEXT)";
sqLiteDatabase.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String event_name, long unixtime, int participants, LatLng location, String locationName,String summary){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
Gson gson = new Gson();
String gsonLocation = gson.toJson(location,LatLng.class);
contentValues.put(COL_NAME,event_name);
contentValues.put(COL_UNIXTIME, unixtime);
contentValues.put(COL_PARTICIPANTS, participants);
contentValues.put(COL_LOCATION, gsonLocation);
contentValues.put(COL_LOCATIONNAME, locationName);
contentValues.put(COL_SUMMARY, summary);
long result = db.insert(TABLE_NAME, null, contentValues);
if(result == -1){
return false;
}else{
return true;
}
}
public Cursor getAllEvents(){
SQLiteDatabase db = this.getReadableDatabase();
String where = null;
Cursor c = db.query(true, TABLE_NAME, ALL_COLUMNS,where,null,null,null,/** COL_NAME + " ASC"**/null,null);
return c;
}
3)創建一個CursorAdapter(再次,這麼多的在線教程): 僅供參考使用地雷:
public class EventListCursorAdapter extends CursorAdapter {
private LayoutInflater cursorInflater;
Calculations calculations = new Calculations();
Gson gson = new Gson();
Context AppContext;
public EventListCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
cursorInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
AppContext = context.getApplicationContext();
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return cursorInflater.inflate(R.layout.card_view, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView timeText = (TextView) view.findViewById(R.id.event_time);
TextView nameText = (TextView) view.findViewById(R.id.event_name);
TextView dateText = (TextView) view.findViewById(R.id.event_date);
TextView summaryText = (TextView) view.findViewById(R.id.event_summary);
TextView participantsText = (TextView) view.findViewById(R.id.event_participantNum);
TextView locationText = (TextView) view.findViewById(R.id.event_location);
final Cursor mCursor = cursor;
String date = calculations.UnixTimeConverter(
mCursor.getLong(mCursor.getColumnIndex(DatabaseHelper.COL_UNIXTIME)
))[0];
String time = calculations.UnixTimeConverter(
mCursor.getLong(mCursor.getColumnIndex(DatabaseHelper.COL_UNIXTIME))
)[1];
final LatLng location = gson.fromJson(mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.COL_LOCATION)),LatLng.class);
nameText.setText(mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.COL_NAME)));
dateText.setText(date);
timeText.setText(time);
participantsText.setText(mCursor.getInt(mCursor.getColumnIndex(DatabaseHelper.COL_PARTICIPANTS))+"");
summaryText.setText(mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.COL_SUMMARY)));
locationText.setText(mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.COL_LOCATIONNAME)));
locationText.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
final CameraPosition camLocation = CameraPosition.builder().
target(location).zoom(18).build();
MainActivity.mMap.animateCamera(CameraUpdateFactory.newCameraPosition(camLocation));
}
});
summaryText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater mLayoutInflator;
mLayoutInflator = LayoutInflater.from(AppContext);
final AlertDialog.Builder mBuilder = new AlertDialog.Builder(mContext);
View mView = mLayoutInflator.inflate(R.layout.summarydialog,null);
TextView textView = mView.findViewById(R.id.mainText);
textView.setText(
mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.COL_SUMMARY))
);
textView.setMovementMethod(new ScrollingMovementMethod());
mBuilder.setView(mView);
final AlertDialog dialog = mBuilder.create();
dialog.show();
}
});
}
}
請注意,您必須實現bindView和NewView的方法,並且必須創建一個構造函數,調用父類
最後,在你的主類,你「 attatch」 CursorAdapter的到ListView:
使用我的代碼以供參考:
final ListView contanctListView = (ListView) findViewById(R.id.contactListView);
final ContactsDatabaseHelper contactManager = new ContactsDatabaseHelper(context);
contactListCursor = contactManager.getAllContacts();
customAdapter = new ContactListCursorAdapter(
ContactActivity.this,
contactListCursor,
0);
contanctListView.setAdapter(customAdapter);
注意,在我的情況下,ID爲listview was eventlistview and for each card item,it was card_view.xml
到目前爲止,您嘗試了什麼? – Mandy8055