-1

I'm trying to get all ID and IncomeCategoryName from DB and put them into a listview.

The problem is that Package Name. ClassName@hexcode is displayed instead of ID and IncomeCategory Name.

Class File

public class IncomeCategoriesClass {
    private Integer CATEGORY_ID;
    private String CATEGORY_NAME;

    public IncomeCategoriesClass(Integer CATEGORY_ID, String CATEGORY_NAME) {
        this.CATEGORY_ID = CATEGORY_ID;
        this.CATEGORY_NAME = CATEGORY_NAME;
    }

    public IncomeCategoriesClass() {
        this.CATEGORY_NAME = CATEGORY_NAME;
    }

    public Integer getCATEGORY_ID() {
        return CATEGORY_ID;
    }

    public void setCATEGORY_ID(Integer CATEGORY_ID) {
        this.CATEGORY_ID = CATEGORY_ID;
    }

    public String getCATEGORY_NAME() {
        return CATEGORY_NAME;
    }

    public void setCATEGORY_NAME(String CATEGORY_NAME) {
        this.CATEGORY_NAME = CATEGORY_NAME;
    }
}

Database helper snippet

public List get_AllIncomeCategories(){ List AllIncomeCategories = new LinkedList<>(); String queryString = "Select * FROM " + INCOME_CATEGORIES_TABLE; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(queryString,null);

    IncomeCategoriesClass incomeCategoriesClass;
    if(cursor.moveToFirst()){
        do {
            incomeCategoriesClass = new IncomeCategoriesClass();
            incomeCategoriesClass.setCATEGORY_ID(Integer.parseInt(cursor.getString(0)));
            incomeCategoriesClass.setCATEGORY_NAME(cursor.getString(1));
            AllIncomeCategories.add(incomeCategoriesClass);
        }while (cursor.moveToNext());
        }
    cursor.close();
    db.close();
    return AllIncomeCategories;
}

AddEditincomeCategoriesActivity

public class AddEditIncomeCategoriesActivity extends AppCompatActivity {

    //Define Screen Fields And Variables
    ListView listviewAllIncomeCategories;


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

        //Initialize Screen Field Variables
        btn_ViewAllIncomeCategory = findViewById(R.id.btn_ViewAllIncomeCategory);
        listviewAllIncomeCategories = findViewById(R.id.listviewAllIncomeCategories);


        //View All Income Categories on Button Click
        btn_ViewAllIncomeCategory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DataBaseHelper dataBaseHelper = new DataBaseHelper(AddEditIncomeCategoriesActivity.this);
                List<IncomeCategoriesClass> AllIncomeCategories;
                AllIncomeCategories = dataBaseHelper.get_AllIncomeCategories();
                ArrayAdapter adapter = new ArrayAdapter(AddEditIncomeCategoriesActivity.this,android.R.layout.simple_list_item_1,AllIncomeCategories);
                listviewAllIncomeCategories.setAdapter(adapter);
            }
        });

        //Add Income Category on Button Click
    }
}

It is outputing data but not the data I am looking for, it is Packagename.Class@HexCode instead of 1 Cash, 2 Credit Cards, etc.

What am I doing wrong and how can I do this properly?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Shawn Mulligan
  • 103
  • 1
  • 8
  • So I see 2 solutions here. If I go with your solution it will get the output I want, however will I be able to reference the ID? Eventually in the listview I will want to beable to manupulate the data. Not just see the results, although seeing the results right now is the main objective. – Shawn Mulligan May 11 '21 at 19:52

2 Answers2

1

Simple ArrayAdapter can only inflate flat values like strings or integers. For a list of objects, You need a Custom Adapter to render your views.

See ListView Android for the implementation details.

Maddy Blacklisted
  • 1,190
  • 1
  • 7
  • 17
  • Here are some [examples](https://stackoverflow.com/questions/8166497/custom-adapter-for-list-view) – javdromero May 11 '21 at 19:36
  • I am going to look into your guidance as what you say makes 100% sense. And by going this route I will beable to access the data to beable to manipulate it later. End result will beable to edit the data in the listview from the listview. – Shawn Mulligan May 11 '21 at 19:55
  • Just out of curiosity. Would it make sense to do the following: Use the suggestions above by overriding the toString and create a new Array and then populate the listview from the new array? – Shawn Mulligan May 13 '21 at 16:31
  • Overriding toString can do the thing for sure. But your data would be flattened to a single string. You would have to split the string to parts in order to parse the data depending upon how you implement toString – Maddy Blacklisted May 14 '21 at 10:01
  • So the listview is only to be used to show the user all the categories and then if they click on one they will be able to edit and or delete it from the database. So I am up in the air as to whether I want to create an array and parse it or do I want to create a custom adapter. Creating the custom adapter would be like me going in to no mans land where creating an array and parsing it seems easiest. It's just a matter of what would be more proper. The user using the app really will not care as they will no no difference. – Shawn Mulligan May 14 '21 at 21:47
0

You're seeing the output of Object#toString().

You can override toString() in your IncomeCategoriesClass to produce the string output you'd like to be displayed for that item.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • If I override it, I will get the string output and see the results that I want, but in the end I will need to beable to use the results from the listview to beable to edit the data. I am not sure if going this route would be the best route for the long haul. For the short term it would work because then I can visually see the results. – Shawn Mulligan May 11 '21 at 19:54