我创建了一个包含名称,日期,问题和答案的类历史记录。 然后我创建了一个包含ListView的activity_history,以及一个包含4个textViews的activity_adapter,类适配器:
public class MyAdapter extends ArrayAdapter<history> {
private LayoutInflater mInflat;
private ArrayList<history> hist;
private int mVRessId;
public MyAdapter (Context context, int ressId, ArrayList<history> hists){
super(context,ressId,hists);
this.hist =hist;
mInflat = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mVRessId =ressId;
}
public View getView(int position, View convertedView, ViewGroup parents){
convertedView = mInflat.inflate(mVRessId,null);
History histor = hist.get(position);
if (histor != null){
TextView name = (TextView) convertedView.findViewById(R.id.hname);
TextView quest = (TextView) convertedView.findViewById(R.id.hques);
TextView ans = (TextView) convertedView.findViewById(R.id.hans);
TextView date = (TextView) convertedView.findViewById(R.id.hdate);
if (name != null){
name.setText(histor.getNom());
}
if (quest != null){
quest.setText(histor.getQuest());
}
if (ans != null){
name.setText(histor.getRep());
}
if (date != null){
name.setText(histor.getDate().toString());
}
}
return convertedView;
}
}
在DatabaseHelper中,我创建了一个函数,使用我的SQL查询返回一个Cursor:
public Cursor getTableHistoAsCursor() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor data = db.rawQuery(" SELECT Q." + KEY_QUESTION + " , U." + KEY_NOM + " , U." + KEY_PRENOM + " , A." + KEY_DATE + " , A. " + KEY_REPONSE + " from " + TABLE_QUESTION + " Q, " + TABLE_USER + " U, " + TABLE_ANSWER + " A WHERE Q." + KEY_ID_QUESTION + " = A." + KEY_ID_QUESTION + " AND A." + KEY_MATRICULE + " = U." + KEY_MATRICULE, null);
return data;
}
历史课,我写了这段代码:
public class Historian extends AppCompatActivity {
ModelHelper openhelper; //<<<< Note 1
SimpleCursorAdapter mSCA;
SQLiteDatabase db;
Cursor mCsr;
ListView mListView;
ArrayList<Historique> histList;
Historique histo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_historique);
mListView = (ListView) findViewById(R.id.lsthisto); //<<<< Note 3
openhelper = new ModelHelper(this);
db = openhelper.getReadableDatabase();
histList = new ArrayList<>();
mCsr = openhelper.getTableHistoAsCursor();
int rows = mCsr.getCount();
if (rows == 0 ){
Toast.makeText(Historian.this, "No histo", Toast.LENGTH_SHORT).show();
} else {
while (mCsr.moveToNext()){
histo = new Historique(mCsr.getString(0),mCsr.getString(1), mCsr.getString(2),mCsr.getString(3));
histList.add(histo);
}
MyAdapter adapter = new MyAdapter (this, R.layout.adapter_view_layout,histList);
}
问题在于,当我执行时,没有问题或错误编译,只是列表视图总是为空。 谢谢你的帮助。
答案 0 :(得分:0)
问题在于,当我执行时,没有问题或错误 编译时,只是列表视图总是空的。
你错过了setAdapter到listView。在行
之后添加mListView .setAdapter(adapter);
MyAdapter adapter = new MyAdapter (this, R.layout.adapter_view_layout,histList);