0

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があります。

カスタムアダプタがどのように機能するかを理解しようとしています。ソースを読んだことがありますが、私の場合はその方法がわかりません。

4

0 に答える 0