问题
I am using a custom list adapter for my ListView. My each list item has 4 items. which are come from a listArray.
Here is the code which pull the list items from List Array.
import android.app.Fragment;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.rupomkhondaker.sonalibank.adapter.PhoneListAdapter;
import com.rupomkhondaker.sonalibank.model.ContactItem;
import java.util.ArrayList;
public class GMOFragment extends Fragment {
public GMOFragment(){}
private boolean database;
private static final String DB_NAME = "SBLdata.db";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_gmo, container, false);
final ArrayList<ContactItem> listData = getListData();
final ListView listView = (ListView) rootView.findViewById(R.id.gmolistView);
listView.setAdapter(new PhoneListAdapter(getActivity(), listData));
DataBaseHelper dataOpen = new DataBaseHelper((MainActivity) getActivity(), DB_NAME);
//Trying to open the DataBase
database = dataOpen.openDataBase();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
ContactItem newsData = (ContactItem) listView.getItemAtPosition(position);
Intent intent = new Intent(getActivity(), ContactDetail.class);
intent.putExtra("DATA_KEY", newsData);
startActivity(intent);
}
});
return rootView;
}
private ArrayList<ContactItem> getListData() {
ArrayList<ContactItem> listMockData = new ArrayList<ContactItem>();
String sqlQuery = "select BR_NAME,PHONE,MOBILE,EMAIL from SBL_Contact";
Cursor contactsCursor = database.rawQuery(sqlQuery, null);
ContactItem newsData = null;
while (contactsCursor.moveToNext()) {
newsData = new ContactItem();
newsData.setName(contactsCursor.getString(0));
newsData.setPhone(contactsCursor.getString(1));
newsData.setMobile(contactsCursor.getString(2));
newsData.setEmail(contactsCursor.getString(3));
listMockData.add(newsData);
}
return listMockData;
}
}
But Now I have created the same list in my SBLdata (databse_name) in SBL_Contact (table name) Column 1.NAME, 2.PHONE, 3.MOBILE , 4.EMAIL
Now please help me to pull the data and set them to getListData()
回答1:
You can create a sql query e.g.:
String sqlQuery = "select name,phone,mobile,email from SBL_Contact";
Then use it with a cursor to get the results:
Cursor contactsCursor = database.rawQuery(sqlQuery, null);
and then iterate through your cursor:
ContactItem newsData = null;
while (contactsCursor.moveToNext()) {
newsData = new ContactItem();
newsData.setName(contactsCursor.getString(0));
newsData.setPhone(contactsCursor.getString(1));
newsData.setMobile(contactsCursor.getString(2));
newsData.setEmail(contactsCursor.getString(3));
listMockData.add(newsData);
}
来源:https://stackoverflow.com/questions/32824558/how-to-pull-data-from-database-and-view-them-as-a-listview