我喜欢制作按钮以便在页面中排列。
但是按钮的数量取决于数据库中存储的数据的数量。
为此,每个按钮都有一个存储在数据库中的标题。
按钮在页面中显示良好,但我无法使它们可点击。
private void displayGridView() {
final Cursor cursor = MessagesBDD.fetchAllMessage();
// The desired columns to be bound
String[] columns = new String[]{MessagesBDD.COL_TITRE,};
final GridView gridview = (GridView) findViewById(R.id.fragboutton);
int[] to = new int[]{R.id.button9,};
final SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.fragment_button, cursor, columns, to, 0);
gridview.setAdapter(adapter);
}
在OnCreate中:
//Création d'une instance de ma classe MessageBDD
MessagesBDD messageBdd = new MessagesBDD(this);
//On ouvre la base de données pour écrire dedans
messageBdd.open();
//Generate ListView from SQLite Database
displayGridView();
//Récupère le fragment permettant d'accèder à la liste des contacts
我的数据库:
public class Message {
private int id;
private String titre;
private String contenu;
public Message(){}
public Message(String titre, String contenu){
this.titre = titre;
this.contenu = contenu;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitre() {
return titre;
}
public void setTitre(String titre) {
this.titre = titre;
}
public String getContenu() {
return contenu;
}
public void setContenu(String contenu) {
this.contenu = contenu;
}
public String toString(){
return "ID : "+id+"\ntitre : "+titre+"\nContenu : "+contenu;
}
Methode fetchAllMessage:
public static Cursor fetchAllMessage() {
Cursor mCursor = bdd.query(TABLE_MESSAGES, new String[] {COL_ID, COL_TITRE, COL_CONTENU}, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
感谢您的帮助
答案 0 :(得分:0)
在活动中创建方法
public ArrayList<Message> fetchAllMessage()
{
ArrayList<Message> mArrayList = new ArrayList<>();
Cursor mCursor = bdd.query(TABLE_MESSAGES, new String[]{COL_ID, COL_TITRE, COL_CONTENU}, null, null, null, null, null);
if (mCursor != null)
{
mCursor.moveToFirst();
mCursor.moveToFirst();
while (!mCursor.isAfterLast())
{
Message message = new Message();
message.setTitre(mCursor.getString(mCursor.getColumnIndex(COL_TITRE)));
message.setId(mCursor.getInt(mCursor.getColumnIndex(COL_ID)));
message.setContenu(mCursor.getString(mCursor.getColumnIndex(COL_CONTENU)));
mArrayList.add(message);
mCursor.moveToNext();
}
}
return mArrayList;
}
添加set gridview adapter
ButtonAdapter adapter = new ButtonAdapter(this,fetchAllMessage);
gridview.setAdapter(适配器);
ButtonAdapter.java
public class ButtonAdapter extends BaseAdapter
{
private Context mContext;
ArrayList<Message> list;
// Gets the context so it can be used later
public ButtonAdapter(Context c, ArrayList<Message> list) {
mContext = c;
this.list = list;
}
// Total number of things contained within the adapter
public int getCount() {
return list.size();
}
// Require for structure, not really used in my code.
public Object getItem(int position) {
return null;
}
// Require for structure, not really used in my code. Can
// be used to get the id of an item in the adapter for
// manual control.
public long getItemId(int position) {
return position;
}
public View getView(int position,
View convertView, ViewGroup parent) {
Button btn;
if (convertView == null) {
// if it's not recycled, initialize some attributes
btn = new Button(mContext);
btn.setLayoutParams(new GridView.LayoutParams(100, 55));
btn.setPadding(8, 8, 8, 8);
}
else {
btn = (Button) convertView;
}
btn.setText(list.get(position).getTitre());
// filenames is an array of strings
btn.setTextColor(Color.WHITE);
btn.setBackgroundResource(R.drawable.button);
btn.setId(position);
return btn;
}
}