0

I created a list view with one line of text for each textView in the list, and was wondering how i can create two lines of text for each item in the list view.

Im using an ArrayAdapter but i couldn't find any helpful documents on the android dev. site on how to create two lines. Additionally, I haven't seen any info. on how to be flexible with the list view- e.g. how do i make editText's inside the listView, or how do i change background color of list items. Can someone please help me bc ive been looking all over but to no avail. Thanks!!

Izak
  • 909
  • 9
  • 24
  • have you tried [googling the answer? this was the first item](http://www.survivingwithandroid.com/2014/08/android-listview-with-multiple-row.html) – panini Apr 19 '15 at 23:30
  • 1
    possible duplicate of [Android custom Row Item for ListView](http://stackoverflow.com/questions/15832335/android-custom-row-item-for-listview) – Oleg Vaskevich Apr 19 '15 at 23:30
  • ive seen this in the design on the android dev site but it dsnt explain how to do it. – Izak Apr 19 '15 at 23:34
  • By default, the listview can only accept one string to the passed as output but you can overwrite the toString() function of the listView, although it is still one line but you can use the new line character for another line. you can display a text of two lines with this method – Ibukun Muyide Apr 19 '15 at 23:36
  • where do i overwrite this method? - and newline is created how? – Izak Apr 19 '15 at 23:40

2 Answers2

1

here is a general answer ..that allows you to create your own list view items..

first of all you have to create your listview item for exemple my item is:

medecinListItem.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#dddddd"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/bg_parent_rounded_corner"
        android:orientation="vertical"
        android:paddingBottom="20dp"
        android:paddingTop="20dp" >
        <RelativeLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView3"
                android:maxWidth="40dp"
                android:maxHeight="40dp"
                android:minHeight="40dp"
                android:minWidth="40dp"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginLeft="15dp" />
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingLeft="10dp"
                android:layout_weight="4.08"
                android:layout_toLeftOf="@+id/imageView"
                android:layout_toStartOf="@+id/imageView"
                android:layout_toRightOf="@+id/imageView3"
                android:layout_toEndOf="@+id/imageView3">
                <TextView
                    android:id="@+id/NomMed"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="15dp"
                    android:textStyle="bold" />
                <TextView
                    android:id="@+id/SpeMed"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#a0a3a7"
                    android:textSize="13dp" />
            </LinearLayout>
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView"
                android:scaleType="fitXY"
                android:src="@drawable/ic_action_navigation_more_vert"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true" />
        </RelativeLayout>
            <TextView
                android:id="@+id/NumMed"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"
                android:textColor="#0a80d1"
            android:gravity="right" />
        <TextView
                android:id="@+id/AdreMed"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingBottom="5dp"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"
            android:gravity="right" />
</LinearLayout>
</LinearLayout>

screenshot of the listitem : the list itemsthe list item when clicking on imageview and use custom list adapter that allow you to inflate your items in the listview :

(here is my listAdapter)

 public class MedecinListAdapter extends BaseAdapter {
           private Activity activity;
        private LayoutInflater inflater;
        private List<MedecinItem> MedecinItems;

        public MedecinListAdapter(Activity activity, List<MedecinItem> MedecinItems) {
            this.activity = activity;
            this.MedecinItems = MedecinItems;
        }

        @Override
        public int getCount() {
            return MedecinItems.size();
        }

        @Override
        public Object getItem(int location) {
            return MedecinItems.get(location);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (inflater == null)
                inflater = (LayoutInflater) activity
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (convertView == null)
                convertView = inflater.inflate(R.layout.medecin_list_item, null);


            TextView nom = (TextView) convertView.findViewById(R.id.NomMed);
            TextView specialite = (TextView) convertView
                    .findViewById(R.id.SpeMed);
            TextView adresse = (TextView) convertView
                    .findViewById(R.id.AdreMed);
            TextView numero = (TextView) convertView.findViewById(R.id.NumMed);

            ImageView imageMed = (ImageView) convertView.findViewById(R.id.imageView3);

            ImageView image = (ImageView) convertView.findViewById(R.id.imageView);
            final  MedecinItem item = MedecinItems.get(position);

            nom.setText(item.getNom());
            numero.setText(getString(R.string.numero)+":"+item.getNumero());
            adresse.setText(getString(R.string.adresse)+":"+item.getAdresse());
            if(Locale.getDefault().getLanguage().equals("ar"))
                specialite.setText(avoirSpeEnArabe(item.getSpecialite()));
            else
                specialite.setText(item.getSpecialite());
            String spe=avoirSpeEnFrancais(item.getSpecialite());

            System.out.println("spe '"+spe+"'");
            int  id = getResources().getIdentifier(avoirSpe2(spe).toLowerCase(), "drawable", getPackageName());

            imageMed.setImageResource(id);
            image.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    PopupMenu popup = new PopupMenu(MainActivity.context, v);
                    popup.getMenuInflater().inflate(R.menu.medecin_list_menu,
                            popup.getMenu());
                    popup.show();
                    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                        @Override
                        public boolean onMenuItemClick(MenuItem item2) {
                            line2=item.getNumero();
                            Emailm=avoirEmail(line2);
                            switch (item2.getItemId()) {
                                case R.id.Appeler:
                                Call(item.getNumero());

                                    break;
                                case R.id.EnvoyerMsg:
                                    msg(Emailm);

                                    break;
                                case R.id.AfficherDet:

                                    menuItem = "3";
                                    Vider();
                                    telecharger();

                                    break;
                                case R.id.Afficher:

                                    String Lat;
                                    String Lon;
                                    Cursor medecin = MainActivity.db.lireMedecin();
                                    while (medecin.getPosition() < medecin.getCount()) {
                                        if (medecin.getString(4).equals(line2)) {
                                            Lat = medecin.getString(5);
                                            Lon = medecin.getString(6);
                                            Mapfrag2.map.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(Lat), Double.parseDouble(Lon)))
                                                    .title(item.getNom())
                                                    .snippet(line2).icon(BitmapDescriptorFactory
                                                            .fromResource(Icone(medecin.getString(7).charAt(0)))));
                                            MainActivity.vp.setCurrentItem(1, true);
                                            CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
                                            CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(Double.parseDouble(Lat), Double.parseDouble(Lon)));
                                            Mapfrag2.map.moveCamera(center);
                                            Mapfrag2.map.animateCamera(zoom);
                                        }
                                        medecin.moveToNext();
                                    }

                                    break;
                                case R.id.AvoirRdv:
                                    telecharger();
                                    menuItem = "2";

                                    break;

                                default:
                                    break;
                            }

                            return true;
                        }
                    });

                }
            });
            if (!MedecinItems.get(position).anime){
                Animation animation = AnimationUtils.loadAnimation(MainActivity.context, R.anim.fade_in);
                convertView.startAnimation(animation);
                MedecinItems.get(position).anime=true;}

            return convertView;
        }

    }

and this is the MedecinItem:

public class MedecinItem {
    private String id,nom, numero, adresse, specialite;
    public boolean anime=false;


    public MedecinItem(String id, String nom, String numero, String adresse,
                       String specialite) {
        super();
        this.id = id;
        this.nom = nom;
        this.numero = numero;
        this.specialite = specialite;
        this.adresse = adresse;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getSpecialite() {
        return specialite;
    }

    public void setSpecialite(String specialite) {
        this.specialite = specialite;
    }

    public String getNumero() {
        return numero;
    }

    public void setNumero(String numero) {
        this.numero = numero;
    }

    public String getAdresse() {
        return adresse;
    }

    public void setAdresse(String adresse) {
        this.adresse = adresse;
    }

}

use it like this:

List<MedecinItem> medecinItems=new ArrayList<MedecinItem>();
//here you add items to your list
 MedecinListAdapter mla=new MedecinListAdapter(MainActivity.this,medecinItems);
        ((ListView) findViewById(R.id.listView)).setAdapter(mla);

and here is the simplest way to create the listView items with two lines..
first declare your list to store two lines items

List<Map<String, String>> list= new ArrayList<Map<String, String>>();

after that create two lines item and add it to that list like this :

Map<String, String> item= new HashMap<String, String>(2);
                item.put("Line1", "Name : Charaf");
                item.put("Line1","Phone number: 1234567");
                list.add(item);

now show the previous items in the listview :

SimpleAdapter adapter = new SimpleAdapter(this, list,
                    android.R.layout.simple_list_item_2, 
                    new String[] {"Line1", "Line2" }, 
                    new int[] {android.R.id.text1, android.R.id.text2 });
listView.setAdapter(Adapter);

to read data from listView ... for example when clickin on item.. add this:

Map<String, String> item= new HashMap<String, String>(2);
 item=listview.getAdapter().getItem(position);
  String name=item.get("Line1");
Charaf Eddine Mechalikh
  • 1,248
  • 2
  • 10
  • 20
  • try to create your own layout , it is the best way, it helps you to customise your list and theming it – Charaf Eddine Mechalikh Apr 20 '15 at 00:10
  • Your example is not helpful. The screenshot you posted does not represent the xml code you written. Please try posting a well formed and constructed example. – HShbib Jun 10 '16 at 10:28
  • @HShbib well that xml is the same with the screenshot and you can see that my answer has two parts , first the general way that you can apply for "n" lines ,and the second part is 2 lines liste item , so read carefully before you comment – Charaf Eddine Mechalikh Jun 12 '16 at 22:36
0

You will definitely need to subclass ArrayAdapter and override getView().

ArrayAdapter only knows how to handle one TextView per row . That is what i know as how i understood your question..

Madona wambua
  • 1,408
  • 1
  • 21
  • 37
  • yes thank you im getting a better understanding on how to do this. What is the best way i can learn how to subclass ArrayAdapter? – Izak Apr 19 '15 at 23:58