Wednesday, January 30, 2019

android - Why does ListActivity not display last item in ArrayAdapter?

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 getAllTrips(){
List trips = new ArrayList();


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

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...