1

I like making so that buttons be arranged in the page.

But the number of button depends on the number of datum stored in the database.

To do it, every button has a title stored in the database.

Buttons display well in the page, but I do not manage to make them clickable.

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

}

In the 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

My Database :

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

thanks for your help guys

Steven Lnds
  • 49
  • 1
  • 7
  • What exactly are you asking ? i cant understand your problem . – Giteeka Sawlani Jun 15 '16 at 09:55
  • I want where you put the buton you take the title and send this by SMS. – Steven Lnds Jun 15 '16 at 10:09
  • But now When I click on the button there is no action. – Steven Lnds Jun 15 '16 at 10:10
  • You have to set onClickListener to your button and in onClick method you can put your send sms code. Can you please upload SimpleCursorAdapter class so i can suggest more info. – Giteeka Sawlani Jun 15 '16 at 10:14
  • I dont have CimpleCursorAdapter class in my project – Steven Lnds Jun 15 '16 at 10:24
  • ok. so you can set onItemClickListener to gridview and on that onItemClick method you can put your send sms code like this :: gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { // send sms } }); – Giteeka Sawlani Jun 15 '16 at 10:29
  • I have make this : gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, "Le sms :", Toast.LENGTH_LONG).show(); but dont make the toast when I click in the button – Steven Lnds Jun 15 '16 at 10:46
  • so gridview item click is not working , so you have to work on gridview item click or make custom BaseAdapter and in that code you put button click listener as earlier i suggested .. for custom BaseAdpter , see http://stackoverflow.com/questions/20129337/android-gridview-with-custom-baseadapter-get-clicked-view-at-position – Giteeka Sawlani Jun 15 '16 at 10:53
  • I have try this method : http://www.stealthcopter.com/blog/2010/09/android-creating-a-custom-adapter-for-gridview-buttonadapter/ but dont say to change filesnames by the title in the database – Steven Lnds Jun 15 '16 at 11:31
  • yes that's correct example, so whats problem ? – Giteeka Sawlani Jun 15 '16 at 11:37
  • I don't say How to put the title of buttons according to the data in the database – Steven Lnds Jun 15 '16 at 11:44
  • for exemple in this exemple you have the data : public String[] filesnames = { "File 1", "File 2", "Roflcopters" }; but me it is stocked in the database. – Steven Lnds Jun 15 '16 at 11:46
  • you can make array list like this and pass this arraylist to adapter ArrayList mArrayList = new ArrayList(); mCursor.moveToFirst(); while(!mCursor.isAfterLast()) { mArrayList.add(mCursor.getString(mCursor.getColumnIndex(COL_NAME))); //add the item mCursor.moveToNext(); } – Giteeka Sawlani Jun 15 '16 at 11:56
  • I do not see making too much how what you say:s – Steven Lnds Jun 15 '16 at 11:59

1 Answers1

0

create method in activity

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;

 }

Add set gridview adapter

ButtonAdapter adapter = new ButtonAdapter(this,fetchAllMessage);

gridview.setAdapter(adapter);

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;
 }
}
Giteeka Sawlani
  • 465
  • 2
  • 8
  • Oh thank's you, I am blocked in there during 3 day xD you safe me :p – Steven Lnds Jun 15 '16 at 12:33
  • And for this method : public void onClick(View v) { // Preform a function based on the position Log.d("CONFIRMATION", String.valueOf(this.position)); } i need to return the Contenu :3 – Steven Lnds Jun 15 '16 at 12:38
  • glad i could help , and you can upvote my answer if it helped – Giteeka Sawlani Jun 15 '16 at 12:38
  • remove this line btn.setOnClickListener(new MyOnClickListener(position)); and add btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("CONFIRMATION",list.get(position).getContenu()); } }); – Giteeka Sawlani Jun 15 '16 at 12:42