androidlistviewbuttonmatrixcursor

Button showing Internal address of button rather than setText text


Every row in my listview has 2 buttons. The text to be shown depends on some characteristics, and hence I want to change the text on the Button. However, I am not only not able to change the text, but the text that appears is like the internal address of the button, i.e. "ANDROID.WIDGET.BUTTON{175F420 VFED..C.. ....I. 0,0-0,0#7F0C00083 APP:ID/READ} Here is my code:

import android.widget.Button;
....

public void ShowArticles(){

ArrayList<ArticleSelection> articleSelectionList = helper
            .GetArticleListForResource(globalVariable.getCategoryIndex());


    String[] columnNames = {"_id", "Column1", "Column2", "Column3",
            "Column4", "Column5", "Column6", "Column7"};
    MatrixCursor matrixCursor = new MatrixCursor(new String[]{"_id",
            "Column1", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7" });

    int[] to = new int[]{R.id.id, R.id.imageButton, R.id.title, R.id.year, R.id.authors,
            R.id.favs, R.id.read, R.id.readOnLine};


for (ArticleSelection articleItem : articleSelectionList) {

     Button btn1 = (Button) ThisView.findViewById(R.id.read);
     Button btn2 = (Button) ThisView.findViewById(R.id.readOnLine);

     if (Downloaded == "Y"){
         btn1.setText("Read");
     }
     else btn1.setText("Download");

     matrixCursor.addRow(new Object[]{k, imgFile,Title, Year , Authors, favs , btn1, btn2 });
}

SimpleCursorAdapter menuItems = null;
menuItems = new SimpleCursorAdapter(
        this, R.layout.article_row, matrixCursor, columnNames, to);

listView.setAdapter(menuItems);
}

My XML for the buttons:

   <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/read"
    android:backgroundTint="#00000000"
    android:text="Download"
    android:layout_toRightOf="@+id/favs"
    android:layout_alignBottom="@+id/imageButton"
    android:layout_marginStart="11dp"
    android:layout_weight="1"
    />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id = "@+id/readOnLine"
    android:backgroundTint="#00000000"
    android:layout_toRightOf="@+id/read"
    android:text="Read Online"
    android:layout_alignBottom="@+id/imageButton"
    android:layout_alignParentEnd="true"
    android:layout_weight="1"
    />

Thank you. The answer is probably really easy, as I have done this before, but I don't think I have done it within a rows of list views. I have seen other answers about adding text to the button, and those I have tried.


Solution

  • Try changing your add row to

    matrixCursor.addRow(new Object[]{k, imgFile,Title, Year , Authors, favs , btn1.getText(), btn2.getText() })