我正在为ListView使用自定义列表适配器。我的每个列表项都有4个项目。它们来自listArray。
以下是从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;
}
}
但是现在我在SBL_Contact(表名)列1.NAME,2.PHONE,3.MOBILE,4.EMAIL
中的SBLdata(databse_name)中创建了相同的列表现在请帮我拉取数据并将它们设置为getListData()
答案 0 :(得分:0)
您可以创建一个SQL查询,例如:
String sqlQuery = "select 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);
}