prathik
prathik

Reputation: 129

Binding multiple array into single list view in android

I am new to android and I was working in the android list view&array.I had certain string arrays

try{
    String subString=colValue.text();
    String[] strArray1=subString.split("//certain regex");
    String[] strArray2=subString.split("//certain regex");
    String[] strArray3=subString.split("//certain regex");
    return strArray1;
}

I have the list view like this

@Override
    protected void onPostExecute(String[] result) {            
        ListView list=(ListView)findViewById(R.id.listShow);
        ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_list_item_1,result);
        list.setAdapter(arrayAdapter);
        mProgressDialog.dismiss();
    }

my listview.xml

<ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listShow"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/btnData" />

I have added the list view.But I cannot access the string array variable

try{
       List<String> list = new ArrayList<String>(Arrays.asList(strArray1));
       list.addAll(Arrays.asList(strArray2));
       String [] outResult= (String[]) listString.toArray();
       return outResult;


        } catch (IOException e) {
            e.printStackTrace();
        }

How to bind the array values into single list view .help me to solve this

Upvotes: 0

Views: 803

Answers (2)

parithi
parithi

Reputation: 270

Try this

try{
            List<String> listString = new ArrayList<String>(Arrays.asList(resultArray));
            listString.addAll(Arrays.asList(resultArray1));
            String [] outResult= new String[listString.size()];
            int i=0;
            for(String str: listString){
                outResult[i]=str;
                i++;
            }
            return outResult;

        } catch (Exception e) {
            e.printStackTrace();
        }

Upvotes: 1

Treeline
Treeline

Reputation: 485

If I understand you correctly, you have the arrays strArray1, strArray2 and strArray3, but can only figure out how to pass one of them to the arrayAdapter. Is this correct?

If that is the case, maybe you could append the arrays to each other, so that you only have one array - Which you can then pass to the adapter?

Alternatively, you could return the arrays as a List, and make an Adapter that takes a list of strings?

EDIT: I imagined something like this:

try{
    String subString = colValue.text();
    List<String> entries = new List<String>();
    entries.addAll(subString.split("//certain regex"));
    entries.addAll(subString.split("//certain regex"));
    entries.addAll(subString.split("//certain regex"));
    return entries;
}

,

@Override
    protected void onPostExecute(List<String> result) {            
        ListView list = (ListView) findViewById(R.id.listShow);
        ListAdapter<String> listAdapter = new ListAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, result);
        list.setAdapter(listAdapter);
        mProgressDialog.dismiss();
    }

Upvotes: 0

Related Questions