コードを投稿するために Stackoverflow を試してみましたが、修正するにはミスが多すぎると思います。
私の最大の問題は誤解であるため、それをどのように開発する必要があるかを尋ねます。
問題: カスタムのcursorAdapterから供給されるlistViewを実行したいと考えています。
なぜカスタム cursorAdapter ?
今のところ、listView の最初の行に editText を配置し、次にデータベースの項目を配置し、次に listView の最後の行に別の editText を配置したいからです。ヘッダーとフッターで解決できましたが、後で同じリストビューに他の多くの行テンプレートが必要になり、さらに多くのカーソルが必要になる可能性があります。それが理由です。
このチュートリアルに従って、アダプターで多くのタイプを使用しようとしました: 1つのリストビューに多くの行タイプがあることについてのリンク
もちろん、bindView と newView をオーバーライドしました。
しかし
失敗しています。両方のeditTextをlistViewに直接追加する方法がわからないなど、多くのバグがあります...私にとって最大の問題は理解です。それを行う方法の説明が必要です。正確に何をすべきかを意味しますbindViewで、そしてnewViewで正確に何をすべきか。
気が変わったので、これが私のアダプターのコードです。気をつけて、それは多分恐ろしいです。
package com.android.activity;
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 Cursor cursor;
private int addNoteTopPosition = 0;
private int addNoteBottomPosition;
private LayoutInflater inflater;
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, int flag){
super(context, cursor);
this.context = context;
addNoteTopPosition = 0;
addNoteBottomPosition = cursor.getCount()+1;
inflater = LayoutInflater.from(this.context);
}
// public
@Override
public int getCount() {
return super.getCount() + 2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ADD_NOTE_TOP:
convertView = inflater.inflate(R.layout.add_note_top, null);
holder.view = (EditText)convertView.findViewById(R.id.add_note_top_id);
break;
case TYPE_ITEM:
convertView = inflater.inflate(R.layout.row_note, null);
holder.view = (TextView)convertView.findViewById(R.id.note);
getCursor().moveToPosition(position - 1);
// System.out.println("modified : " + getCursor().getInt(getCursor().getColumnIndex("_id")));
((TextView) holder.view).setText(getCursor().getInt(getCursor().getColumnIndex("_id")) + getCursor().getString(getCursor().getColumnIndex("content_note")));
break;
case TYPE_ADD_NOTE_BOTTOM:
convertView = inflater.inflate(R.layout.add_note_bottom, null);
holder.view = (EditText)convertView.findViewById(R.id.add_note_bottom_id);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
return convertView;
}
@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;
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public View view;
}
}
注:後でviewHolderなどの最適化を修正します...
注2:念のため、これが私の主な活動コードです
package com.android.activity;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewDebug.FlagToString;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.CursorAdapter;
import com.android.database.Note;
import com.android.database.NoteDataSource;
public class EverydayNotesAndroid3Activity extends Activity {
/** Called when the activity is first created. */
private Cursor cursorNotes;
private NotesCursorAdapter notesCursorAdapter;
private InputMethodManager imm;
private Activity activity;
private ListView listView;
private int positionItem;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
activity = this;
NoteDataSource.getSingletonObject(activity);
NoteDataSource.getSingletonObject(activity).open();
cursorNotes = NoteDataSource.getSingletonObject(activity).getCursorUpdatedDatabase(); // return all notes in a cursor
startManagingCursor(cursorNotes);
listView = (ListView) findViewById(R.id.main_list);
notesCursorAdapter = new NotesCursorAdapter(activity, cursorNotes, 3);
listView.setAdapter(notesCursorAdapter);
Button b = new Button(activity);
b = (Button) findViewById(R.id.done);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
NoteDataSource.getSingletonObject(activity).createNoteTop("Hello stack overflow world");
cursorNotes = NoteDataSource.getSingletonObject(activity).getCursorUpdatedDatabase();
notesCursorAdapter.changeCursor(cursorNotes);
notesCursorAdapter.notifyDataSetChanged();
}
});
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id){
if (position == 0){
showAlertWindowAddTop(parent, v, position, id);
}else if (position == parent.getChildCount()-1){
showAlertWindowAddBottom(parent, v, position, id);
}else{
showAlertWindowModify(parent, v, position, id);
}
}
});
}
そして心配しないでください:
1) 関数 showAlertWindowblabla() ==> 動作します
2) createNoteTop 関数も機能し、データベースに行を挿入します。
読んでくれてありがとう、あなたの答えを探しています。
編集:わかりましたので、大したことではない仕事をした後、私は自分が望むものに近づいています。残念ながら、私はまだ表示のバグがあります。フィールドを変更すると、理由がわかりません。彼はこのフィールドをリストの別の場所にドロップするだけです。これは (bindView または newView メソッドで) 簡単に修正できる大きな間違いだと思いますが、疲れていて、どこから来たのかわかりません。そこで、一石二鳥となるように、customAdapter の修正コードを投稿します。