0

我使用下面的代碼爲我的主要活動:爲什麼ListActivity不能顯示ArrayAdapter中的最後一項?

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

用戶應該能夠輸入值對話框,名稱將顯示在創建行程列表。但是,當列表中只有一個值時,似乎會出現一個錯誤,因爲該項目未顯示。我花了幾個小時,並無法弄清楚。

編輯: 這是我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

它看起來像問題是與你的TripsData類的職位,也嘗試登錄的長度刪除項目後,適配器和數據源。我猜測這兩個數字在某個地方不同步。

+0

發佈我的TripsData – yanki

+0

您是否嘗試過在每次出現時記錄適配器和數據源的長度以確保它們同步? –

相關問題