I am in the process of learning Android, so far I have started on creating a Contact List. The list is stored using SQLlight, but when I try to display the information in a ListView each contact shows in the same section.
The result I get 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 about MainActivity/ this fragment's parent activity
getActivity(),
//Id for contact item layout
R.layout.contact_list_item,
//gets the arraylist of contacts
getContacts()
);
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
//Get reference to the list view and then set the adapter to it.
listView = (ListView) rootView.findViewById(R.id.listView_contacts);
listView.setAdapter(adapter);
return rootView;
}
/*
Method to get contacts from the 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">