How to create a List and SubList?

2

I want to populate a ListView based on two tables, one called category and the other subcategory, and I would like it to look something like this:

When I typed a Category name in EditText and clicked the Add button, I added it to the category table and it already appeared dynamically as a table item, and when I clicked the button to add a subcategory open a dialog to enter a Subcategory name and then, when I clicked a button to add it, I inserted it into the subcategory table and dynamically appeared below the related category.

OnCreate of the CategoryActivity

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


    CategoriaDAO categoria = new CategoriaDAO(this);
    List<Categoria> list = categoria.getLista();
    ListView listView = (ListView) findViewById(android.R.id.list);
    listView.setAdapter(new CategoriaAdapter(this, list, listView));



}
    
asked by anonymous 21.01.2015 / 15:42

1 answer

1

Look here:

import java.util.ArrayList;
import java.util.List;

public class SubListaExemplo{
  public static void main(String[] args) {
    ArrayList arrayList = new ArrayList();
// Os elementos vão aqui ser add
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    arrayList.add("4");
    arrayList.add("5");

//Criando a sublista:
    List lst = arrayList.subList(1,3);


//display elements of sub list.
System.out.println("Sub list contains : ");
for(int i=0; i< lst.size() ; i++)
  System.out.println(lst.get(i));

Or, as I used in my project, you use the ListView tag in xml so it already appears in sub items:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ListView
        android:id="@+id/listView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </ListView>

</LinearLayout>
    
21.01.2015 / 20:53