Unable to separate items when displayed to listview android studio -
i in process of learning android, far have started on creating contact list. list stored using sqllight, when try display information in listview each contact shows in same section.
the result is..
contact 1
contact 2
contact 3
.......................
instead of..
contact 1
.........................
contact 2
.........................
contact 3
.........................
public class mainactivityfragment extends fragment { databasehelper mydb; arrayadapter<string> adapter; private static listview listview; public mainactivityfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { mydb = new databasehelper(getactivity()); adapter = new arrayadapter<>( //gets information mainactivity/ fragment's parent activity getactivity(), //id contact item layout r.layout.contact_list_item, //gets arraylist of contacts getcontacts() ); view rootview = inflater.inflate(r.layout.fragment_main, container, false); //get reference list view , set adapter it. listview = (listview) rootview.findviewbyid(r.id.listview_contacts); listview.setadapter(adapter); return rootview; } /* method contacts database */ public list<string> getcontacts(){ cursor result = mydb.getdata(); list<string> namelist = new arraylist<string>(); if(result.getcount() == 0){ namelist.add("no contacts available"); return namelist; } stringbuffer buffer = new stringbuffer(); while(result.movetonext()){ //buffer.append("id :" + result.getstring(0)+"\n"); buffer.append("name :" + result.getstring(1)+"\n"); buffer.append("phone :" + result.getstring(2)+"\n"); buffer.append("email :" + result.getstring(3)+"\n"); buffer.append("b-day :" + result.getstring(4) + "\n"); } namelist.add(buffer.tostring()); return namelist; }
}
activity_main.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment" android:name="com.example.jak.contacts.mainactivityfragment" tools:layout="@layout/fragment_main" android:layout_width="match_parent" android:layout_height="match_parent" />
fragment_main.xml
<framelayout 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" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivityfragment" android:orientation="vertical"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="add contact" android:id="@+id/button_add_contact" android:layout_gravity="center_horizontal|bottom" /> <listview android:layout_width="match_parent" android:layout_height="410dp" android:id="@+id/listview_contacts" android:layout_gravity="right" />
contact_list_item.xml
<?xml version="1.0" encoding="utf-8"?> <textview xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:minheight="?android:attr/listpreferreditemheight" android:gravity="center_vertical" android:id="@+id/contact_list_item_textview">
Comments
Post a Comment