1

Here's the code with the problem:

//declarations:
private ArrayAdapter<String> listAdapter;
private ArrayAdapter<String> listAdapter2;
String [] nomi=null;
String[] famiglia=null;
private ListView mainListView;

// other code bla bla bla...

mainListView = (ListView) findViewById(R.id.listView);

// other code bla bla bla...


nomi = new String[res.size()];
for (int i = 0; i < res.size(); i++) {
    nomi[i] = res.get(i).getNomignolo();
}


famiglia= new String[res.size()];
for(int i=0; i<res.size();i++){
    famiglia[i] = res.get(i).getFamiglia();
}


ArrayList<String> listaNomi = new ArrayList<String>();
listaNomi.addAll(Arrays.asList(nomi));
ArrayList<String> listaFamiglie = new ArrayList<String>();
listaFamiglie.addAll(Arrays.asList(famiglia));





listAdapter = new ArrayAdapter<String>(HomeActivity.this, R.layout.row, R.id.button3, listaNomi);
listAdapter2 = new ArrayAdapter<String>(HomeActivity.this, R.layout.row, R.id.button6, listaFamiglie);
mainListView.setAdapter(listAdapter);
mainListView.setAdapter(listAdapter2);

It works, but only in part, because when i start the app i can find only the result of the second setAdapter method. How can i achieve also the result of all the setAdapter methods? Thanks.

Daniele Oriana
  • 67
  • 1
  • 2
  • 9

4 Answers4

0

This is not possible, whenever you are setting second adapter to listview, your first adapters data get replaced with second.

LISTVIEW CAN DISPLAY SINGLE ADAPTER DATA AT SINGLE TIME.

If you want to display data from multiple sources to ListView then first merge all all data and then use single adapter. Do something like below

dataList1 (From source A)
datalist2 (From source B)

datalist3 = dataList1 + dataList2;

setDapter(dataList3) 
Ganesh AB
  • 4,652
  • 2
  • 18
  • 29
  • Yes, first collect all data (merge data of 2 sources) and then set to listview by using single adapter. – Ganesh AB Oct 08 '15 at 10:02
0

try to add objects of second list in first on

   nomi = new String[res.size()];
   for (int i = 0; i < res.size(); i++) {
     nomi[i] = res.get(i).getNomignolo() + " " +res.get(i).getFamiglia();
    }

  //list of object with name and family

create adapter

listAdapter = new ArrayAdapter<String>(HomeActivity.this, R.layout.row, R.id.button3, listaNomi);
 mainListView.setAdapter(listAdapter);

you cannot set multiple adapter for one listview.

How to create custom array adapter:

  public class CustomAdapter extends ArrayAdapter<Person>{
  private final Activity context;

  private ArrayList<Person> Items;
  public CustomAdapter (Activity context, int layout,ArrayList<Person> persons) {
  super(context, layout, carriers);
 // TODO Auto-generated constructor stub

 this.context = context;
 this.Items = persons;

 }

 static class ViewHolder {
  public Button name;
  public Button family;


   }

@Override
 public View getView(int position, View convertView, ViewGroup parent) {
  final ViewHolder holder;
  // Recycle existing view if passed as parameter
  // This will save memory and time on Android
  // This only works if the base layout for all classes are the same
  View rowView = convertView;
  if (rowView == null) {
    LayoutInflater inflater = context.getLayoutInflater();
    rowView = inflater.inflate(R.layout.item, null, true);

    holder = new ViewHolder();
    holder.name= (TextView) rowView.findViewById(R.id.name);
     holder.family= (TextView) rowView.findViewById(R.id.family);
    rowView.setTag(holder);
   } else {
    holder = (ViewHolder) rowView.getTag();
   }

   holder.name.setText(Items.get(position).getName());
  holder.family.setText(Items.get(position).getFamily());


  return rowView;

 }

}

and create PErson Object:

public class PErson{ public String name; public String family;
                   public Person();
                  public String getName(){ return name;}
                  public String getFamily(){return family;}
                }

and this is item xml

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

<Button
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="wkjdhk"
    android:textColor="@color/green"
    android:textSize="12sp" />

<Button
    android:id="@+id/family"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text="wkjdhk"
    android:textColor="@color/green"
    android:textSize="12sp" />
  </RelativeLayout>

this is how to call from activity:
adapter = new MyCustomAdapter(CarrierSummaryActivity.this,nomi,R.layout.item); mainListView.setAdapter(adapter);

Amalo
  • 772
  • 3
  • 13
  • 32
  • yes, but the data of the first list have to go in the button3 field, while the data of the second list have to go in the button6 field... how can i do that? – Daniele Oriana Oct 08 '15 at 10:04
  • you must create a cutsom array adapter not the standard one – Amalo Oct 08 '15 at 10:06
0

Can you please do like this way ?

First List:

ArrayList<String> a = new ArrayList<String>();
a.add("a");
a.add("b");
a.add("c");

Second List:

ArrayList<String> b = new ArrayList<String>();
b.add("d");
b.add("e");
b.add("f");

Add a to b:

b.addAll(a);

Merge both list:

ArrayList<String> union = new ArrayList<String>(a);
union.addAll(b);

Set Adapter on ListView:

listAdapter = new ArrayAdapter<String>(HomeActivity.this, R.layout.row, R.id.button3, union);
mainListView.setAdapter(listAdapter);

Hope this will help you.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
  • yes, but the data of the first list have to go in the button3 field, while the data of the second list have to go in the button6 field... how can i do that? – Daniele Oriana Oct 08 '15 at 10:04
  • @DanieleOriana, Can you please refer http://stackoverflow.com/questions/17693578/android-how-to-display-2-listviews-in-one-activity-one-after-the-other/28713754#28713754 one, for multiple listview. – Hiren Patel Oct 08 '15 at 10:05
0

You should implement your custom adapter so it can draw the list just like you want.

class MyAdapter extends BaseAdapter
{
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      //Here you can return a View with any data in any way
    }
}

Hope this helps.

Nanoc
  • 2,381
  • 1
  • 20
  • 35