我是一名非常熟练的Android开发人员,我正在研究一个非常简单的应用程序,该应用程序从SQLite数据库中读取条目并将它们映射到ListView,只要单击“全部列出”按钮,就会显示CUSTOMER_NAME字段。我一直在关注一些不同的教程,但已经遇到了范围问题。
编辑:如何完成从光标到ListView的映射?
非常感谢您提供的任何帮助!
到目前为止,我的DBHelper,getAllCustomer方法看起来像:
public Cursor getAllCustomers(){
List<Customer> customers=new LinkedList<Customer>();
String query="SELECT * FROM "+TABLE_NAME;
SQLiteDatabase db=this.getWritableDatabase();
Cursor cursor= db.rawQuery(query, null);
return cursor;
}
我的fragment_list.xml看起来像(参见ListView):
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="List Customers"
android:id="@+id/textView3"
android:layout_gravity="center_horizontal" />
<ListView
android:layout_width="wrap_content"
android:layout_height="381dp"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:id="@+id/listView"
android:layout_above="@+id/btnAdd" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List All"
android:id="@+id/btnGetAll"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/btnAdd" />
</LinearLayout>
最后是我的CustomersFragment.java
public class CustomersFragment extends Fragment {
public CustomersFragment() {
}
public static CustomersFragment newInstance() {
CustomersFragment fragment = new CustomersFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_search, container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button bGetAll = (Button) getActivity().findViewById(R.id.btnGetAll);
bGetAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CustomerrDBSetup db = new CustomerrDBSetup(getActivity());
Cursor cursor = db.getAllCustomers();
String[] from = new String[] { "customer_name" };
}
});
}
}
答案 0 :(得分:0)
你的功能应该是这样的
public List<Customer> getAllCustomers(){
List<Customer> customers = new LinkedList<Customer>();
String query="SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db=this.getWritableDatabase();
Cursor cursor= db.rawQuery(query, null);
if (cursor.moveToFirst()) {
do {
Customer customer = customerFromCursor(cursor);
customers.add(cutomer);
} while (cursor.moveToNext());
}
//don't forget to close cursor after use it
if (cursor != null) {
cursor.close();
}
return customers;
}
public Customer customerFromCursor(Cursor cursor){
Customer ret = new Customer();
//put your getter here
//ex:
ret.setName(cursor.getString(cursor.getColumnIndex(yourColumName)));
//.....
return ret;
}
通过这种方式,您将获得listview的所有数据