I have two activities. One to receive the data (about the customer) from the user. This data is stored in SQLite db. The other activity contains the ListView to display the customer data stored in db.
The code in the MainActivity is called using a method populateListView() inside the onCreate. Here goes the code
public void populateListView(){
Cursor cursor = vivzHelper.getAllRows();
String[] customerArray=new String[]{vivzHelper.name()};
int[] customerNameTextView=new int[]{R.id.customerNameItem};
final SimpleCursorAdapter myCursorAdapter;
myCursorAdapter=new SimpleCursorAdapter(getBaseContext(),R.layout.single_row_customer_name,cursor,customerArray,customerNameTextView,0);
ListView CustomerList= (ListView) findViewById(R.id.customer_listview);
CustomerList.setAdapter(myCursorAdapter);
//Enables Search filtering for the contents of the given ListView
CustomerList.setTextFilterEnabled(true);
CustomerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), "You have selected " + ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
EditText customer_search = (EditText) findViewById(R.id.customer_search);
customer_search.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
myCursorAdapter.getFilter().filter(s.toString());
}
});//End: Enables Search filtering for the contents of the given ListView
}
HelperClass Code:
public String name(){
SQLiteDatabase db=helper.getWritableDatabase();
String name=VivzHelper.COLUMN_CUSTOMER_NAME;
return name;
}
public Cursor getAllRows(){
SQLiteDatabase db=helper.getWritableDatabase();
//Customer Name from TABLE_NAME_CUSTOMER
String[] ALL_KEYS={VivzHelper.COLUMN_CUSTOMER_NAME};
Cursor cursor=db.query(VivzHelper.TABLE_NAME_CUSTOMER,ALL_KEYS,null,null,null,null,null);
if (cursor != null){
cursor.moveToFirst();
}
return cursor;
}
On executing the code, I am still able to save new customer data into SQLite. However, when opening the activity containing the ListView to display the customer name, the app force closes. No errors are displayed on logcat. Can't understand what's happening.
Aucun commentaire:
Enregistrer un commentaire