首页 > 解决方案 > ListView 未使用 Cursoradapter 填充

问题描述

我有一个输入页面,它将 4 个字符串提交到帮助程序中。助手应该获取数据并将其放入 listView。

当我插入我的数据时,应用程序不会崩溃,但它会引导我进入没有行膨胀的列表视图页面。4 个数据输入都是字符串,通过button. 我不太确定我的代码有什么问题。不胜感激一些建议:)

列表视图代码

public class MyActivitiesPage extends AppCompatActivity {
    private Cursor model = null;
    private ListView list;
    private dbHandler helper = null;
    private activityAdapter adapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activities_page);

        helper = new dbHandler(this);
        list = findViewById(R.id.listView);
        model = helper.getall();
        adapter = new activityAdapter(this, model, 0);
        list.setAdapter(adapter);
    }
    
    public static class activityHolder {
        private TextView activitytext;
        private TextView datetext;
        private TextView timetext;
        private TextView addresstext;

        activityHolder(View row) {
            activitytext = row.findViewById(R.id.activitytext);
            datetext = row.findViewById(R.id.datetext);
            timetext = row.findViewById(R.id.timetext);
            addresstext = row.findViewById(R.id.addresstext);
        }

        void populateFrom(Cursor c, dbHandler helper){
            activitytext.setText(helper.getActi(c));
            datetext.setText(helper.getDate(c));
            timetext.setText(helper.getTime(c));
            addresstext.setText(helper.getAddr(c));
        }
    }

    class activityAdapter extends CursorAdapter {
        activityAdapter(Context context, Cursor cursor, int flags) {
            super(context, cursor, flags);
        }

        public void bindView(View view, Context context, Cursor cursor) {
            activityHolder holder = (activityHolder) view.getTag();
            holder.populateFrom(cursor, helper);
        }
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            LayoutInflater inflater = getLayoutInflater();
            View row = inflater.inflate(R.layout.row, parent, false);
            activityHolder holder = new activityHolder(row);
            row.setTag(holder);
            return (row);
        }
    }

帮助代码

public class dbHandler extends SQLiteOpenHelper {
        private Context context;
    private static final String DATABASE_NAME = "handler.db";
    private static final int SCHEMA_VERSION = 1;

    public dbHandler(@Nullable Context context){
        super(context, DATABASE_NAME, null, SCHEMA_VERSION);
        this.context = context;
    }

    public void onCreate(SQLiteDatabase db) {
        //Will be called ones when the database is not created
        db.execSQL("CREATE TABLE dbTable ( _id INTEGER PRIMARY KEY AUTOINCREMENT, acti TEXT, date TEXT, time TEXT, addr TEXT );");
    }

    public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
        //Will not be called upon until SCHEMA_VERSION increases
        //For future upgrades
    }

    public Cursor getall() {
        return (getReadableDatabase().rawQuery("SELECT * FROM dbTable", null));
    }


    public Cursor getById(String id) {
        String[] args = {id};
        return (getReadableDatabase().rawQuery("SELECT _id, acti, date, time, addr FROM dbTable WHERE _ID = ?", args));
    }

    public void insert(String acti, String date, String time, String addr) {
        ContentValues cv = new ContentValues();

        cv.put("acti", acti);
        cv.put("date", date);
        cv.put("time", time);
        cv.put("addr", addr);

        long result = getWritableDatabase().insert("dbTable", "firstvalue", cv);

        if(result == -1){
            Toast.makeText(context, "Failed", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(context, "Added Successfully!", Toast.LENGTH_SHORT).show();
        }
    }

    public void updateDetails(String id, String acti, String date, String time, String addr) {
        ContentValues cv = new ContentValues();
        String[] args = {id};

        cv.put("acti", acti);
        cv.put("date", date);
        cv.put("time", time);
        cv.put("addr", addr);

        getWritableDatabase().update("dbTable", cv, " _ID = ?", args);
    }

    public String getID(Cursor c) { return (c.getString(0));}

    public String getActi(Cursor c) { return (c.getString(1));}

    public String getDate(Cursor c) { return (c.getString(2));}

    public String getTime(Cursor c) { return (c.getString(3));}

    public String getAddr(Cursor c) { return (c.getString(4));}
}```

标签: javaandroidhelperpopulateandroid-cursoradapter

解决方案


推荐阅读