0

I am using the following code for my main activity:

private TripsData datasource;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 datasource = new TripsData(this);
 datasource.open();
 List values = datasource.getAllTrips();
 ArrayAdapter adapter = new ArrayAdapter(this,
 android.R.layout.simple_list_item_1, values);
 setListAdapter(adapter);
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu; this adds items to the action bar if it is present.
 getMenuInflater().inflate(R.menu.main, menu);
 return true;
 }
 // Will be called via the onClick attribute
 // of the buttons in main.xml
 public void onClick(View view) {
 @SuppressWarnings("unchecked")
 ArrayAdapter adapter = (ArrayAdapter) getListAdapter();
 Trip trip = null;
 //Trip trip_temp;
 switch (view.getId()) {
 case R.id.add:
 trip=newTrip(view);
 //trip.setId(trip_temp.getId());
 //trip.setName(trip_temp.getName());
 adapter.add(trip);
 break;
 case R.id.delete:
 if (getListAdapter().getCount()> 0) {
 trip = (Trip) getListAdapter().getItem(0);
 datasource.deleteTrip(trip);
 adapter.remove(trip);
 }
 break;
 }
 adapter.notifyDataSetChanged();
 }
 @Override
 protected void onResume() {
 datasource.open();
 super.onResume();
 }
 @Override
 protected void onPause() {
 datasource.close();
 super.onPause();
 }
 public Trip newTrip(View view){
 final Trip trip=new Trip();
 //create DialogBox
 final Dialog dialog = new Dialog(this);
 //modify features BEFORE setting content view
 //dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
 dialog.setContentView(R.layout.project_dialog);
 //Create EditBoxes for Dialog
 final EditText nameEdit=(EditText) dialog.findViewById(R.id.dialog_name_text);
 final EditText descEdit=(EditText) dialog.findViewById(R.id.dialog_type_text);
 //define button's text
 View dialogButton=dialog.findViewById(R.id.dialog_button_create);
 TextView text=(TextView) dialogButton;
 text.setText("Create");
 //Button Creation
 Button createButton = (Button) dialogButton;
 // if button is clicked, close the custom dialog
 createButton.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 Trip trip_temp = datasource.createTrip(nameEdit.getText().toString());
 //String[] trips = new String[] { "Cool", "Very nice", "Hate it" };
 //int nextInt = new Random().nextInt(3);
 // save the new comment to the database
 trip.setId(trip_temp.getId());
 trip.setName(trip_temp.getName());
 //trip = datasource.createTrip(nameEdit.getText().toString());
 dialog.dismiss();
 }
 });
 dialog.show();
 return trip;
 }

The user should be able to input values in the dialog box and the Name would be displayed in the list of created Trips. However, there seems to be a bug when there is only one value in the List because that item is not displayed. I've spent hours on this and can't figure it out.

EDIT: This is my TripsData code

public class TripsData {

private SQLiteDatabase database;
private TripsDB dbHelper;
private String[] allTrips = { TripsDB.TRIP_COLUMN_ID,
 TripsDB.TRIP_COLUMN_TYPE};
public TripsData(Context context){
 dbHelper = new TripsDB(context);
}
public void open() throws SQLException{
 database = dbHelper.getWritableDatabase();
}
public void close(){
 dbHelper.close();
}
public Trip createTrip(String type){
 ContentValues values = new ContentValues();
 values.put(TripsDB.TRIP_COLUMN_TYPE, type);
 long insertId = database.insert(TripsDB.TABLE_TRIPS, null, values);
 Cursor cursor = database.query(TripsDB.TABLE_TRIPS, 
 allTrips, TripsDB.TRIP_COLUMN_ID + " = " + insertId, null, null, null, null);
 cursor.moveToFirst();
 Trip newTrip = cursorToTrip(cursor);
 cursor.close();
 return newTrip;
}
public void deleteTrip(Trip trip){
 long id = trip.getId();
 System.out.println("Project deleted with id: " + id);
 database.delete(TripsDB.TABLE_TRIPS, TripsDB.TRIP_COLUMN_ID
 + " = " + id, null);
}
public List<Trip> getAllTrips(){
 List<Trip> trips = new ArrayList<Trip>();
 Cursor cursor = database.query(TripsDB.TABLE_TRIPS,
 allTrips, null, null, null, null, null);
 cursor.moveToFirst();
 while(!cursor.isAfterLast()){
 Trip trip = cursorToTrip(cursor);
 trips.add(trip);
 cursor.moveToNext();
 }
 cursor.close();
 return trips;
}
private Trip cursorToTrip(Cursor cursor){
 Trip trip = new Trip();
 trip.setId(cursor.getLong(0));
 trip.setName(cursor.getString(1));
 return trip;
}
Vadim Kotov
8,2848 gold badges50 silver badges63 bronze badges
asked Feb 22, 2014 at 4:37

1 Answer 1

1

It looks like the issue is with your TripsData class, post that, also try and log the length of the adapter and the data source after you delete an item. I am guessing that those two numbers are getting out of sync somewhere.

answered Feb 22, 2014 at 4:57

1 Comment

have you tried logging the lengths of the adapter and the datasource at each occurrence to make sure they are in sync?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.