我想使用java显示一行带有按钮的项目到android xml

时间:2017-08-29 10:38:24

标签: android sqlite

我想显示我的SQL数据库中的食物,并显示我希望有一个新行的数据的每一行,如果有太多,可以向下滚动。该行将有一个图片foodname和一个订单按钮。有人有任何想法吗?

我的数据库中有三列食物。名称,说明,价格,imagesrc

SQL数据库的每一行都有一个行对象。如果超过电话页面超过5个,可以向下滚动,我想这样做:

2 个答案:

答案 0 :(得分:0)

为此您可以使用RecyclerView,您可以在此处了解更多信息:https://www.survivingwithandroid.com/2016/09/android-recyclerview-tutorial.html

答案 1 :(得分:0)

1。)在主活动中创建一个列表视图。 以及每个列表项的布局。

2。)创建一个SQLiteOpenHelper数据库(在互联网上有这么多的教程) 有变量:

int imagesource, 字符串名称, 字符串描述, int price,

使用我为其中一个项目创建的其他代码供参考:

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方法,并且必须创建一个构造函数,调用超类

最后,在您的主要课程中,您将“cursoradapter”“附加”到列表视图中:

使用我的代码作为参考:

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);

请注意,就我而言,listview的id是eventlistview 对于每个卡片项目,它是card_view.xml