为什么ListActivity不显示ArrayAdapter中的最后一项?

时间:2014-02-22 04:37:01

标签: android android-activity android-arrayadapter android-alertdialog listactivity

我正在使用以下代码进行主要活动:

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

用户应该能够在对话框中输入值,并且名称将显示在已创建的Trips列表中。但是,当List中只有一个值时,似乎存在一个错误,因为该项未显示。我花了好几个小时就想不出来了。

修改 这是我的TripsData代码

公共类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;
}

1 个答案:

答案 0 :(得分:1)

看起来问题出在您的TripsData类上,发布后,在删除项目后还尝试并记录适配器和数据源的长度。我猜这两个号码在某个地方变得不同步。