0

im very newbie in android & java and I have a hashtable that contains values. values are objects (task class that has 3 attributes code,title,state)

I'm trying to get the data of all tasks in hashtable and show them in list view

ListView list = (ListView)findViewById(R.id.output_list);

ArrayList<task> arr = new ArrayList<task>(tasklist.values());

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.list, arr );

list.setAdapter(arrayAdapter);

tasklist is hashtable name

task is an entity class

public class taskDAO {
    public static final Hashtable<Integer , task> tasklist = new Hashtable<Integer , task>();

    public static final boolean addTask (task t){

        tasklist.put(t.code,t);
        return true;

    }

Exception

E/AndroidRuntime: FATAL EXCEPTION: main java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

I tried many things but since i dont get how this works i can't figure out a solution, any help?

Achraf Khouadja
  • 6,119
  • 4
  • 27
  • 39
  • I am not seeing the reason for the `Hashtable`. [Start here](https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView#using-a-custom-arrayadapter) – OneCricketeer Nov 09 '16 at 18:30
  • bro i know u don't see a reason, but i have a reason , so the question is how to show a listview for that particular hashtable – Achraf Khouadja Nov 09 '16 at 18:32
  • You haven't told us what you want the adapter views to look like... – OneCricketeer Nov 09 '16 at 18:40
  • I have a linear layout with a Listview in it thats all! – Achraf Khouadja Nov 09 '16 at 18:43
  • Exclamation marks are not necessary. You need to understand what an Adapter actually does. It loads one "row view" for each "item" in the "array" into the "ListView". No part of your question explains what this "row view" should look like. – OneCricketeer Nov 09 '16 at 18:47
  • i implemented the toString in the entity class and it's now working, thank you mate – Achraf Khouadja Nov 09 '16 at 18:56

2 Answers2

1

Your error is telling you that the second argument to the Adapter needs to be a TextView layout, not a ListView layout (or whatever the list.xml file contains)

If you implement a toString method on your task class (which you should rename to Task because of Java naming conventions).

Then, you'll see data when you use

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
    this, 
    android.R.layout.simple_list_item_1, 
    arr);

For more details about that layout, see What is "android.R.layout.simple_list_item_1"?

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • very close man , but it shows entities.task@5555555 , how to show the title and status?(task properties) – Achraf Khouadja Nov 09 '16 at 18:48
  • You did not read the answer completely. You need to [implement `toString`](http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4#29140403) – OneCricketeer Nov 09 '16 at 18:49
0

change your adapter generic type String to your class name that is task.so that it look like

ArrayAdapter<task> arrayAdapter = new ArrayAdapter<task>(this,R.layout.list, arr );

And ofcourse make your class name start with capital letter like TaskDAO not taskDAO

Real73
  • 490
  • 4
  • 13
  • I know about conventions and best practices but i'm just learnin, i came from php so somestuff are wierd tbh, anyway this does not work, app crashes – Achraf Khouadja Nov 09 '16 at 18:22