2014-05-06 109 views
0

我正在做我的應用程序的UI重新設計,我試圖添加對象到listView,但他們似乎並沒有添加。我創建了一個自定義類對象習慣在這裏:Android - 自定義ListView適配器不顯示對象

public class Habit { 

    private int _id, day_count; 
    private String habit_name, date_started, end_date; 

    public Habit(){ 
    } 

    public Habit(int id, String name, String startDate, String endDate, int dayCount){ 
     this._id = id; 
     this.habit_name = name; 
     this.date_started = startDate; 
     this.end_date = endDate; 
     this.day_count = dayCount; 
    } 

    public Habit(String name, String startDate, String endDate, int dayCount){ 
     this.habit_name = name; 
     this.date_started = startDate; 
     this.end_date = endDate; 
     this.day_count = dayCount; 
    } 

    public int getID() 
    { 
     return this._id; 
    } 

    public int setID(int id) 
    { 
     return this._id; 
    } 

    public int getDayCount() 
    { 
     return this.day_count; 
    } 

    public int setDayCount(int dayCount) 
    { 
     return this.day_count; 
    } 

    public String getName() 
    { 
     return this.habit_name; 
    } 

    public void setName(String name) 
    { 
     this.habit_name = name; 
    } 

    public String getStartDate() 
    { 
     return this.date_started; 
    } 

    public void setStartDate(String startDate) 
    { 
     this.date_started = startDate; 
    } 

    public String getEndDate() 
    { 
     return this.end_date; 
    } 

    public void setEndDate(String endDate) 
    { 
     this.end_date = endDate; 
    } 

然後在我的主要活動,我從該數據庫中所有對象通過調用我的getAllHabits的方法,我在我的數據庫幫手寫道從displayHabits方法我的主要活動。

主要活動:

public class fourtyMain extends Activity 
{ 
    private HabitDbHelper    mDB; 
    private ListView     mList; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fourty_main); 

     mList = (ListView)findViewById(R.id.habit_list); 
     mDB = new HabitDbHelper(this); 

     getActionBar().setDisplayShowTitleEnabled(false); 
    } 

    //Populate Listview 
    @Override 
    protected void onResume() { 
     displayData(); 
     super.onResume(); 
    } 

    private void displayData() 
    { 
     ArrayList<Habit> habitList = mDB.getAllHabits(); 

     HabitAdapter disadpt = new HabitAdapter(fourtyMain.this, R.layout.fragment_start_habit_item, habitList); 
     mList.setAdapter(disadpt); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.fourty_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
     switch(item.getItemId()) 
     { 

      case R.id.action_settings: 
       return true; 
      case R.id.action_add: 
       Intent i = new Intent(getApplicationContext(), 
         addHabitActivity.class); 
       startActivity(i); 

     } 
     return false; 
    } 
}` 

,並檢索在我的數據庫助手都習慣對象的方法:

public ArrayList<Habit> getAllHabits() { 
     ArrayList<Habit> habitList = new ArrayList<Habit>(); 
     // Select All Query 
     String selectQuery = "SELECT * FROM " + TABLE_HABITS; 

     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 

     // looping through all rows and adding to list 
     if (cursor.moveToFirst()) { 
      do { 
       Habit habit = new Habit(); 
       habit.setID(Integer.parseInt(cursor.getString(cursor.getColumnIndex(KEY_ID)))); 
       habit.setName(cursor.getString(cursor.getColumnIndex(KEY_NAME))); 
       habit.setStartDate(cursor.getString(cursor.getColumnIndex(KEY_STARTDATE))); 
       habit.setEndDate(cursor.getString(cursor.getColumnIndex(KEY_ENDDATE))); 
       habit.setDayCount(Integer.parseInt(cursor.getString(cursor.getColumnIndex(KEY_DAYCOUNT)))); 

       // Adding contact to list 
       habitList.add(habit); 
      } while (cursor.moveToNext()); 
     } 
     return habitList; 
    } 

最後我的適配器類:

public class HabitAdapter extends BaseAdapter { 

    private List<Habit> habits; 
    private Context context; 
    private int layoutId; 

    public HabitAdapter(Context c, int LayoutId,ArrayList<Habit> habits) { 
     this.context = c; 
     this.layoutId = LayoutId; 
     this.habits = habits; 
    } 

    @Override 
    public int getCount() { 
     return 0; 
    } 

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    public View getView(int pos, View child, ViewGroup parent) { 
     Holder mHolder; 
     LayoutInflater layoutInflater; 
     Habit habit = habits.get(pos); 
     if (child == null) { 
      layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      child = layoutInflater.inflate(R.layout.fragment_start_habit_item, null); 
      mHolder = new Holder(); 
      mHolder.title = (TextView)child.findViewById(R.id.fragment_title); 
      mHolder.dayCount = (TextView)child.findViewById(R.id.fragment_days_left); 
      mHolder.startDate = (TextView)child.findViewById(R.id.fragment_start_date); 
      child.setTag(mHolder); 
     } else { 
      mHolder = (Holder) child.getTag(); 
     } 
     mHolder.title.setText(habit.getName()); 
     mHolder.dayCount.setText(habit.getDayCount()); 
     mHolder.startDate.setText(habit.getStartDate()); 
     return child; 
    } 

    public class Holder { 
     TextView title; 
     TextView dayCount; 
     TextView startDate; 
    } 

} 

現在我對於Android開發來說還是比較新的,但我似乎無法弄清楚我在哪裏出錯了。當我添加並反對數據庫時,適配器似乎沒有找到它並用它填充Listview。我相信我要麼從數據庫中錯誤地獲取習慣對象,要麼在適配器類的某個地方出錯。如果有人能指出我要去的地方錯還是給我的技巧來解決這個

回答

0

你的問題很可能是在這裏:

public int getCount() { 
    return 0; 
} 

這表示該適配器代表零個項目。它應該返回列表中的項目數量。

+0

你是對的!我所要做的只是改變返回0;返回habits.size();非常感謝你! – Rbro112

相關問題