1

I have a horizontal Listview. It works fine. My ArrayList eList will show a ListView going across the screen. That's great. My problem is that eList has multiple rows meaning eList might be planes, cars, and boats, or an infinite number of objects. Currently this Horizontal ListView will show only all the kinds of planes OR cars OR boats in the Database. How do show multiple or an infinite number of hListViews going down the screen vertically based upon how many object types(planes,cars,boats,tacos,people).

IN ACTIVITY

HorizontalListView hListView = (HorizontalListView) findViewById(R.id.hlistview1);
    hListView.setAdapter(new ItemAdapter());
    ArrayList<HashMap<String, String>> eList = controller.getAllts();
    ListAdapter adapter = new SimpleAdapter(Su.this,eList,R.layout.view_m_ts, images, ins);
hListView.setAdapter(adapter);

IN DATABASE

public ArrayList<HashMap<String, String>> getAllts() {
    ArrayList<HashMap<String, String>> List3;
    List3 = new ArrayList<HashMap<String, String>>();
    String selectQuery3 = "SELECT DISTINCT * FROM INV where p2 IS NOT NULL ORDER BY p2 COLLATE NOCASE ASC";
    SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor3 = database.rawQuery(selectQuery3, null);
    HashMap<String, String> map = new HashMap<String, String>();
    if (cursor3.moveToFirst()) {
        do {

            map.put("iImageL", cursor3.getString(13));
            map.put("p2", cursor3.getString(2));
            map.put("se2", cursor3.getString(10));
            map.put("te", cursor3.getString(17));
            List3.add(map);
        } while (cursor3.moveToNext());
    }
    close();
    return List3;
}

IN XML

 <LinearLayout
    android:id="@+id/LinearViewa"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight=".1"
    android:orientation="vertical" >

   <com.devsmart.android.ui.HorizontalListView
       android:id="@+id/hlistview1"
       android:layout_width="fill_parent"
       android:layout_height="10dp"
       android:layout_weight=".1"
       android:background="#000000" />

</LinearLayout>

enter image description here

SmulianJulian
  • 801
  • 11
  • 33
  • My bad. My suggested edit was wrong. I didn't notice this line --> `List3.add(map);`. Instead of using an ArrayList of HashMaps, why don't you create a custom object and use an ArrayList of objects? Or is there any special reason that you are using an ArrayList of HashMaps? – iRuth Jan 27 '15 at 15:30
  • Basically I have people, places, and things. I can populate across the screen with people which can be blue people, green people etc.. or places or things. My problem is I want a row of people across, then a row of places under that and a row of places under that and so on. I see that I can probably do this by using the idnumber from INV then telling the Activity to create another HorizontalListView base on the int idnumber but there must be easier way. – SmulianJulian Jan 27 '15 at 15:58
  • If I understand you correctly, your current implementation is a listview that scrolls horizontally, correct? And you want that list view to have 3 rows, each of which scrolls horizontally, correct? – iRuth Jan 27 '15 at 16:38
  • No, I want multiple vertical instances of that HorizontalListView(HLV) based upon the number of rows in a column . Name of column would be stuff...................................................................................................... 1)First column row would be HLV people............................. .............................................2) row would HLV places........ ..... ...............................................................3) row would be HLV things. – SmulianJulian Jan 27 '15 at 22:10
  • I'm sorry, I don't understand your description. Could you create an image of what you currently have and what you want? – iRuth Jan 27 '15 at 23:12

1 Answers1

2

I'm going to assume that all objects of different types are in one SQLite table, with the type stored in one of the columns as a string, and you don't have a different table for each type.

Change your getAllts() function to return a HashMap of ArrayLists of HashMaps instead of an ArrayList of HashMaps, so that you can create multiple HorizontalListViews, one for each ArrayList of HashMaps, each one for a particular type. As you iterate through the rows returned in the cursor, get the type and check if there is already an ArrayList for it in your big HashMap, if not then create one and if so then add to the existing one:

public HashMap<String, ArrayList<HashMap<String, String>>> getAllts() {
    HashMap<String, ArrayList<HashMap<String, String>>> hashMap = new HashMap<String, ArrayList<HashMap<String, String>>>();                

    String selectQuery3 = "SELECT DISTINCT * FROM INV where p2 IS NOT NULL ORDER BY p2 COLLATE NOCASE ASC";
    SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor3 = database.rawQuery(selectQuery3, null);

    int typeColumnIndex = 18; // <- You need to set the correct column here, possibly using cursor3.getColumnIndexOrThrow()

    if (cursor3.moveToFirst()) {
        do {
            // create this HashMap inside the loop because we need a new one each time:
            HashMap<String, String> map = new HashMap<String, String>();
            ArrayList<HashMap<String, String>> List3;

            // get the type of this entry as a string       
            String typename = cursor3.getString(typeColumnIndex);
            // check if there already is an ArrayList for this type:
            if(hashMap.containsKey(typename))
            {
                // get existing ArrayList for this type:
                List3 = hashMap.get(typename);
            }
            else
            {
                // create new ArrayList for this type:
                List3 = new ArrayList<HashMap<String, String>>();
                hashMap.put(typename, List3);
            }
            map.put("iImageL", cursor3.getString(13));
            map.put("p2", cursor3.getString(2));
            map.put("se2", cursor3.getString(10));
            map.put("te", cursor3.getString(17));
            List3.add(map);
        } while (cursor3.moveToNext());
    }
    close();
    return hashMap;
}

Change your XML so that you have an empty LinearLayout in your activity XML:

<LinearLayout
    android:id="@+id/LinearViewa"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight=".1"
    android:orientation="vertical" >

</LinearLayout>

and create a separate XML for the horizontal list view that you can create multiple times and add to your LinearLayout dynamically, in horiz_listview.xml or something like that. You can set up your own custom layout with the type name as a TextView header etc:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

<!-- Add type name here if you like-->

    <com.devsmart.android.ui.HorizontalListView
        android:id="@+id/hlistview1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#000000" />

</LinearLayout>

Then in your activity, get the massive hash map containing all the ArrayLists and iterate through it creating a HorizontalListView for each one and add it to the LinearLayout:

LinearLayout layout = (LinearLayout)findViewById(R.id.LinearViewa);
HashMap<String, ArrayList<HashMap<String, String>>> eHash = controller.getAllts();
LayoutInflater inflater = getLayoutInflater();
for(Map.Entry<String, ArrayList<HashMap<String, String>>> entry : eHash.entrySet())
{
    // get the type name and ArrayList of Hashmaps for this type:
    String typename = entry.getKey();
    ArrayList<HashMap<String, String>> eList = entry.getValue();

    // inflate a horizonal list view and add it to the layout:
    View view = inflater.inflate(R.layout.horiz_listview, null, false);
    layout.addView(view);

    // set up the horizontal list view like before:
    HorizontalListView hListView = (HorizontalListView) view.findViewById(R.id.hlistview1);
    ListAdapter adapter = new SimpleAdapter(Su.this,eList,R.layout.view_m_ts, images, ins);
    hListView.setAdapter(adapter);
}

If you have more HorizontalListViews than you can fit on screen then you might need to wrap your main LinearLayout in a ScrollView, however be warned that you might run into trouble with that, see this question.

Community
  • 1
  • 1
samgak
  • 23,944
  • 4
  • 60
  • 82
  • The Horizontal ListView is working except it is showing the same entry in all the Horizontal Listviews. The LOG shows the correct information is in the LisfViews but what is showing on screen is the same information for all entries. I mean "John" is showing in every entry though the LOG shows John first Listview, then the correct car, city etc on each ListView. I think I need to get typename into my SimpleAdapter or change my getAllts() to getAllts(typename) but if I do that I can't get the entry.getKey because it will not be resolved – SmulianJulian Jan 31 '15 at 12:54
  • in getAllts(), try creating HashMap map inside the loop (see edit) – samgak Jan 31 '15 at 18:05