1

I would like to display the data in an array of string as the Subitems of the listview.

How exactly could I do it?

ngrashia
  • 9,869
  • 5
  • 43
  • 58
Vaironl
  • 69
  • 1
  • 1
  • 4

3 Answers3

0

Is this what you are trying to achieve?

This is part of my code from an ap that uses fragments. One fragment is a list fragment.

import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Category List extends ListFragment {

final static String[] CATEGORIES = {"String1","String2",
    "String3","String4"};

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
        android.R.layout.simple_list_item_activated_1,
        CATEGORIES));

Hope it helps.

Gary
  • 69
  • 1
  • 9
0

Download the api demos from the Android Development site or open the SDK Manager and ensure you have downloaded Samples for your current SDK.

Then navigate to the samples folder and take a look at expandable lists. There are a few fully working examples there.

On my WinXP comp the files are located here:

C:\Program Files\Android\android-sdk\samples\android-16\ApiDemos\src\com\example\android\apis\view then look for the expandable lists.jar files

Matt
  • 690
  • 4
  • 10
0

You need to create an Adapter class. Here is your solution: First create a private class in your MainActivity class like this:

private class Adapter extends BaseAdapter
{
    ArrayList<String> list;
    public Adapter(ArrayList<String> list)
    {
        this.list=list;
    }
    @Override
    public int getCount() 
    {
        return list.size();
    }
    @Override
    public String getItem(int index) 
    {
        return list.get(index);
    }
    @Override
    public long getItemId(int arg0) 
    {
        return 0;
    }
    @Override
    public View getView(int index, View view, ViewGroup arg2) 
    {
        TextView tv_text = new TextView(MainActivity.this);
        tv_text.setText(list.get(index));
        tv_text.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
        @SuppressWarnings("deprecation")
        AbsListView.LayoutParams params = 
                new AbsListView.LayoutParams(AbsListView.LayoutParams.FILL_PARENT,
                        AbsListView.LayoutParams.FILL_PARENT);
        tv_text.setLayoutParams(params);
        tv_text.setHeight(60);
        tv_text.setTextSize(18);
        return tv_text;
    }       
}

then you need to put the Strings in ArrayList<String> :

ArrayList<String> list = new ArrayList<String>();
for(int i=0;i<yourArray.length(); i++) list.add(yourArray[i]);

after that you need to create an instance of the Adapter class:

Adapter adapter = new Adapter(list);

then you need to set the adapter as the main adapter of your ListView :

ListView lv_list = (ListView)findViewById(R.id.listView1);
lv.setAdapter(adapter);

D

KhalidTaha
  • 453
  • 1
  • 5
  • 11