mardi 22 mars 2016

Cursor adapter and SQLite solution with Fragments

I am a very novice Android developer and am working on a very simple application that reads entries from SQLite database and maps them to a ListView, displaying the CUSTOMER_NAME field, whenever the 'List All' button is clicked. I have been following a bunch of different tutorials but have been running into scope issues.

Edit:: How do I proceed with finishing the mapping from cursor to ListView?

Any help you can provide is much appreciated!

So far my DBHelper, getAllCustomer method looks like:

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;
}

My fragment_list.xml looks like (see 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>

And finally my 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" };

        }
    });
  }
}

Aucun commentaire:

Enregistrer un commentaire