我正在学习Android,到目前为止我已经开始创建联系人列表了。该列表使用SQLlight存储,但是当我尝试在ListView中显示信息时,每个联系人都显示在同一部分中。
我得到的结果是..
联系1
联络2
联系3
.......................
而不是..
联系1
.........................
联络2
.........................
联系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">