java - Fragments getting added on top of each other? -


this may duplicate, quite frankly, none of other questions helping me.

so have problem. making contacts android application. when press contact view it's number, 1 number shows. fine, because people don't have multiple numbers 1 contact. when do, want see of numbers. , problem. want have multiple numbers displayed, not work.

only 1 number shows, debugging, know fact actual number variables there, not being displayed correctly. has led me believe 2 fragments getting added on top of each other. , yes, layout being added linearlayout.

so, why heck fragments being added on top of each other? using support fragments , support fragment manager btw. have been trying figure out couple days. hate straight ask, have no idea i'm doing wrong time. if me out here, sweet.

here code:

contactactivity.java

//create intent intent intent = getintent(); bundle extras = intent.getextras();  //get extras string contact_title = extras.getstring("contact_title"); string[] contact_numbers = extras.getstringarray("contact_numbers");  ... ...  //add numbers if(contact_numbers != null) {     fragmentmanager fragmentmanager = getsupportfragmentmanager();     fragmenttransaction trans = fragmentmanager.begintransaction();      //add fragments     for(int = 0; < contact_numbers.length; i++) {         trans.add(r.id.contact_numbers, contactnumberfragment.newinstance("contact_number", contact_numbers[i]), "id: " + integer.tostring(i));     }      trans.commit(); } 

contactnumberfragment.java

//adds public static contactnumberfragment newinstance(string name, string text) {     contactnumberfragment fragment = new contactnumberfragment();      //add extras     bundle bundle = new bundle();     bundle.putstring(name, text);     fragment.setarguments(bundle);      return fragment; }  @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {     view v = inflater.inflate(r.layout.fragment_contact_number, container, false);     bundle extras = getarguments();      string number = extras.getstring("contact_number");     log.i("number", number);      contactnumber_textview = (textview)v.findviewbyid(r.id.contactnumber_textview);      //set number text     if(contactnumber_textview != null) {         contactnumber_textview.settext(number);          log.i("text view text", contactnumber_textview.gettext().tostring());     }      return v; } 

activity_contact.xml

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent">      <include         layout="@layout/toolbar"         android:id="@+id/toolbar" />      <linearlayout         android:orientation="vertical"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:id="@+id/contact_numbers"         android:layout_below="@id/toolbar">          <textview             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:text="@string/contactnumbertitle_title"             android:textcolor="@color/textdarklight"             android:textsize="@dimen/contactnumbertitle_textsize"             android:paddingstart="@dimen/mediumpadding"             android:paddingleft="@dimen/mediumpadding"             android:paddingright="@dimen/mediumpadding"             android:paddingtop="@dimen/smallpadding"             android:paddingbottom="@dimen/smallpadding" />     </linearlayout> </relativelayout> 

fragment_contact_number.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context="contactnumberfragment">      <textview         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:id="@+id/contactnumber_textview"         android:text=""         android:textcolor="@color/textdark"         android:textsize="@dimen/contactnumber_textsize"         android:padding="@dimen/smallpadding" /> </linearlayout> 

instead of using fragment here suggest using custom viewgroup. example have used linearlayout:

public class phoneview extends linearlayout {      private textview numberview;     private imageview phoneicon;      public phoneview(context context) {this(context, null);}     public phoneview(context context, attributeset attrs) {this(context, attrs, 0);}     public phoneview(context context, attributeset attrs, int defstyleattr) {         super(context, attrs, defstyleattr);         init();     }      private void init() {          setorientation(horizontal);          layoutinflater inflater = layoutinflater.from(getcontext());         view view = inflater.inflate(r.layout.view_phone, this, true);          numberview = (textview) view.findviewbyid(r.id.contact_number);         phoneicon = (imageview) view.findviewbyid(r.id.phone_icon);          phoneicon.setonclicklistener(new onclicklistener() {             @override             public void onclick(view v) {                 callnumber();             }         });     }      public void setcontactnumber(string number) {         numberview.settext(number);     }      private void callnumber() {         //...     } } 

make matching layout file view_phone.xml:

<?xml version="1.0" encoding="utf-8"?> <merge     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent">      <textview         android:id="@+id/contact_number"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         />      <imageview         android:id="@+id/phone_icon"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:src="@drawable/icon_phone"         />  </merge> 

then in activity need like:

public void addnumbers(list<string> contactnumbers) {      linearlayout numberlayout = (linearlayout) findviewbyid(r.id.contact_numbers);      (string number : contactnumbers) {          phoneview phoneview = new phoneview(this);         phoneview.setcontactnumber(number);         numberlayout.addview(phoneview);     } } 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -