自定义适配器具有不同的行类型

时间:2012-03-16 02:49:18

标签: android android-cursoradapter

我的customAdapter有问题,我希望它显示一个editText,然后是数据库中的列表,然后再显示一个editText。

让我告诉你我的适配器代码:

package com.android.adapter;

import com.android.activity.R;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.TextView;

public class NotesCursorAdapter extends CursorAdapter{

    private Context context;
    private int addNoteTopPosition = 0;
    private int addNoteBottomPosition;
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_ADD_NOTE_BOTTOM = 1;
    private static final int TYPE_ADD_NOTE_TOP = 2;
    private static final int TYPE_MAX_COUNT = TYPE_ADD_NOTE_TOP + 1;

    public NotesCursorAdapter (Context context, Cursor cursor, boolean flag){
        super(context, cursor, flag);
        this.context = context;
    }



    @Override
    public void bindView(View v, Context con, Cursor cursor) {
        //      System.out.println("BindView");
        int type = getItemViewType(cursor.getPosition());
        if (v == null) {
            switch (type) {

            case TYPE_ADD_NOTE_TOP:
                EditText editText = (EditText)v.findViewById(R.id.add_note_top_id);
                break;

            case TYPE_ITEM:
                TextView content = (TextView)v.findViewById(R.id.note);
                content.setText(cursor.getString(cursor.getColumnIndex("content_note")));
                break;

            case TYPE_ADD_NOTE_BOTTOM:
                EditText editText2 = (EditText)v.findViewById(R.id.add_note_bottom_id);
                break;


            }
        }
    }

    @Override
    public View newView(Context con, Cursor cursor, ViewGroup vp) {
        //      System.out.println("NewView");
        LayoutInflater inflater = LayoutInflater.from(con);
        View v = inflater.inflate(R.layout.row_note, vp, false);
        //      bindView(v, con, cursor);
        //      return v;

        ViewHolder holder = null;
        int type = getItemViewType(cursor.getPosition());
        if (vp == null) {
            holder = new ViewHolder();
            switch (type) {

            case TYPE_ADD_NOTE_TOP:
                v = inflater.inflate(R.layout.add_note_top, null);
                holder.textView = (EditText)vp.findViewById(R.id.add_note_top_id);
                break;
            case TYPE_ITEM:
                v = inflater.inflate(R.layout.row_note, null);
                holder.textView = (TextView)vp.findViewById(R.id.note);
                holder.textView.setText(cursor.getString(cursor.getColumnIndex("content_note")));
                break;
            case TYPE_ADD_NOTE_BOTTOM:
                v = inflater.inflate(R.layout.add_note_bottom, null);
                holder.textView = (EditText)vp.findViewById(R.id.add_note_bottom_id);
                break;
            }
            v.setTag(holder);
        } else {
            holder = (ViewHolder)v.getTag();
        }

        addNoteBottomPosition = cursor.getCount()-1;

        return v;

    }

    @Override
    public int getItemViewType(int position) {
        int type;
        if (position == addNoteTopPosition){
            type = TYPE_ADD_NOTE_TOP;
        } else if (position == addNoteBottomPosition){
            type = TYPE_ADD_NOTE_BOTTOM;
        }else {
            type = TYPE_ITEM;
        }
        return type;
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_MAX_COUNT;
    }

    public static class ViewHolder {
        public TextView textView;
    }


}

结果是我有我的数据库中的列表,但根本没有editText。因为我是Android的新手,我显然误解了一些东西,或者根本就没有得到它,肯定是关于bindView和newView。

感谢您的关注。

编辑:好的,我很难解释我的想法,对不起(加上我的代码错误的方式,因为它错了:()

我想要的是以下模板:具有自定义cursorAdapter的ListView具有以下结构:

  • 空的EditText(用户可以在其中单击以在数据库中添加行)

  • 显示textViews

  • 中数据库表中数据的行列表
  • 在最后一行,是另一个与第一行具有相同目的的editText。

我正在尝试了解自定义适配器是如何工作的,我已经阅读过这些来源,但我并不知道如何在我的情况下这样做。

0 个答案:

没有答案