Android - 自定义ListView适配器不显示对象

时间:2014-05-06 02:27:01

标签: java android listview

我正在对我的应用进行UI重新设计,我正在尝试将对象添加到listView,但它们似乎并没有添加。我在这里为对象Habit创建了一个自定义类:

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

然后在我的主要活动中,通过调用我在我的主要活动中的displayHabits方法中在数据库助手中编写的getAllHabits方法,从数据库中获取所有对象。

主要活动:

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

检索数据库中所有Habit对象的方法Helper:

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。我相信我要么从数据库中错误地获取习惯对象,要么我在适配器类中的某个地方出错了。如果有人能指出我出错的地方或给我提示解决这个问题

1 个答案:

答案 0 :(得分:0)

你的问题可能就在这里:

public int getCount() {
    return 0;
}

这表示适配器代表零项。它应该返回列表中的项目数。