我有以下问题。我设置了在创建时填充数据行的数据库。我需要具有更改数据行的功能(Dialog)。我设法让Dialog在LongClick上打开,但是当按钮点击时添加新条目时,没有任何改变。我不确定我错在哪里。
这是我的文件,显示列表:
public class Monday extends ListActivity {
private static final int MyMenu = 0;
MyDB dba;
DiaryAdapter myAdapter;
private class MyDiary {
public String title;
public String content;
public MyDiary(String t, String c) {
title=t;
content=c;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
dba = new MyDB(this);
dba.open();
setContentView(R.layout.fragment_monday);
super.onCreate(savedInstanceState);
myAdapter = new DiaryAdapter(this);
this.setListAdapter(myAdapter);
}
public class DiaryAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<MyDiary> fragment_monday;
public DiaryAdapter(Context context) {
mInflater = LayoutInflater.from(context);
fragment_monday = new ArrayList<MyDiary>();
getdata();
ListView list = getListView();
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
new EditListItemDialog(view.getContext(), myAdapter, position).show();
return true;
}
});
}
public void getdata(){
Cursor c = dba.getdiaries();
startManagingCursor(c);
if(c.moveToFirst()) {
do {
String title = c.getString(c.getColumnIndex(Constants.TITLE_NAME));
String content = c.getString(c.getColumnIndex(Constants.CONTENT_NAME));
MyDiary temp = new MyDiary(title,content);
fragment_monday.add(temp);
} while(c.moveToNext());
}
}
@Override
public int getCount() {
return fragment_monday.size();
}
public MyDiary getItem(int i) {
return fragment_monday.get(i);
}
public long getItemId(int i) {
return i;
}
public View getView(int arg0, View arg1, ViewGroup arg2) {
final ViewHolder holder;
View v = arg1;
if ((v == null) || (v.getTag() == null)) {
v = mInflater.inflate(R.layout.diaryrow, null);
holder = new ViewHolder();
holder.mTitle = (TextView)v.findViewById(R.id.name);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.mdiary = getItem(arg0);
holder.mTitle.setText(holder.mdiary.title);
v.setTag(holder);
return v;
}
public class ViewHolder {
MyDiary mdiary;
TextView mTitle;
}
}
/** Called when the user clicks the Edit button */
public void visitDiary(View view) {
Intent intent = new Intent(this, Diary.class);
startActivity(intent);
}
/** Called when the user clicks the back button */
public void visitSchedule(View view) {
Intent intent = new Intent(this, DisplayScheduleScreen.class);
startActivity(intent);
}
}
这是一个Dialog文件:
class EditListItemDialog extends Dialog implements View.OnClickListener {
private View editText;
private DiaryAdapter adapter;
// public EditListItemDialog(Context context, List<String> fragment_monday) { //first constructor
// super(context);
// this.fragment_monday = fragment_monday;
// }
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_text_dialog);//here is your xml with EditText and 'Ok' and 'Cancel' buttons
View btnOk = findViewById(R.id.button_ok);
editText = findViewById(R.id.edit_text);
btnOk.setOnClickListener(this);
}
private List<String> fragment_monday;
public EditListItemDialog(Context context, DiaryAdapter adapter, int position) {
super(context);
this.fragment_monday = new ArrayList<String>();
this.adapter = adapter;
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Position is the number of the item clicked
//You can use your adapter to modify the item
adapter.getItem(position); //Will return the clicked item
}
@Override
public void onClick(View v) {
fragment_monday.add(((TextView) v).getText().toString());//here is your updated(or not updated) text
adapter.notifyDataSetChanged();
dismiss();
}
}
如果有人知道这里可能存在什么问题(为什么数据行没有得到更新),我将非常感谢任何帮助。
使用对话框更新列表中的行时使用logcat:
04-21 19:18:45.209: I/InputReader(190): dispatchTouch::touch event's action is 0
04-21 19:18:45.209: I/InputDispatcher(190): Delivering touch to current input target
04-21 19:18:45.359: I/InputReader(190): dispatchTouch::touch event's action is 1
04-21 19:18:45.359: I/InputDispatcher(190): Delivering touch to current input target
04-21 19:18:45.369: V/onEvaluateInputViewShown()(263): in inputmethodservice value = 1
04-21 19:18:45.369: V/onEvaluateInputViewShown()(263): in inputmethodservice value = 1
04-21 19:18:45.369: I/DHWR(263): [I] ./../engine/rel/source/wrapper/dhwr_main.c(501) DHWRSetAttribute: SetAttribute
04-21 19:18:45.399: I/DHWR(263): [I] ./../engine/rel/source/wrapper/dhwr_main.c(509) DHWRSetAttribute: ptype: 0, pMode:0x20fb78, nMode:1, pCands:10
04-21 19:18:45.399: I/DHWR(263): [I] ./../engine/rel/source/wrapper/dhwr_main.c(512) DHWRSetAttribute: pMode[0] lang:0, type:0xc
04-21 19:18:45.399: I/DHWR(263): [I] ./../engine/rel/source/wrapper/dhwr_main.c(501) DHWRSetAttribute: SetAttribute
04-21 19:18:45.419: I/DHWR(263): [I] ./../engine/rel/source/wrapper/dhwr_main.c(509) DHWRSetAttribute: ptype: 0, pMode:0x20fb78, nMode:1, pCands:10
04-21 19:18:45.419: I/DHWR(263): [I] ./../engine/rel/source/wrapper/dhwr_main.c(512) DHWRSetAttribute: pMode[0] lang:0, type:0xc
04-21 19:18:45.419: I/DHWR(263): [I] ./../engine/rel/source/wrapper/dhwr_main.c(1294) DHWRSetWritingArea: left:0, top:0, right:0, bottom:0, sizeRate:60
04-21 19:18:46.749: I/InputReader(190): dispatchTouch::touch event's action is 0
04-21 19:18:46.749: I/InputDispatcher(190): Delivering touch to current input target
04-21 19:18:46.759: V/AudioPolicyManager(95): startOutput() output 1, stream 1, session 8608
04-21 19:18:46.759: V/AudioPolicyManager(95): getDeviceForStrategy() from cache strategy 0, device 2
04-21 19:18:46.759: V/AudioPolicyManager(95): getNewDevice() selected device 2
04-21 19:18:46.759: V/AudioPolicyManager(95): setOutputDevice() output 1 device 2 delayMs 0
04-21 19:18:46.759: V/AudioPolicyManager(95): setOutputDevice() setting same device 2 or null device for output 1
04-21 19:18:46.759: V/AudioPolicyManager(95): releaseOutput() 1
04-21 19:18:46.769: V/AudioHardwareMSM72XX(95): open driver
04-21 19:18:46.769: V/AudioHardwareMSM72XX(95): get config
04-21 19:18:46.769: V/AudioHardwareMSM72XX(95): set config
04-21 19:18:46.769: V/AudioHardwareMSM72XX(95): buffer_size: 4800
04-21 19:18:46.769: V/AudioHardwareMSM72XX(95): buffer_count: 2
04-21 19:18:46.769: V/AudioHardwareMSM72XX(95): channel_count: 2
04-21 19:18:46.769: V/AudioHardwareMSM72XX(95): sample_rate: 44100
04-21 19:18:46.769: V/AudioHardwareMSM72XX(95): count : 4800
04-21 19:18:46.849: I/InputReader(190): dispatchTouch::touch event's action is 1
04-21 19:18:46.849: I/InputDispatcher(190): Delivering touch to current input target
04-21 19:18:46.939: W/AudioFlinger(95): write blocked for 165 msecs, 1984 delayed writes, thread 0xcce8
04-21 19:18:46.969: V/AudioPolicyManager(95): stopOutput() output 1, stream 1, session 8608
04-21 19:18:46.969: V/AudioPolicyManager(95): getNewDevice() selected device 0
04-21 19:18:46.969: V/AudioPolicyManager(95): setOutputDevice() output 1 device 0 delayMs 150
04-21 19:18:46.969: V/AudioPolicyManager(95): setOutputDevice() setting same device 0 or null device for output 1
04-21 19:18:47.039: I/InputReader(190): dispatchTouch::touch event's action is 0
04-21 19:18:47.039: I/InputDispatcher(190): Delivering touch to current input target
04-21 19:18:47.069: V/AudioPolicyManager(95): startOutput() output 1, stream 1, session 8609
04-21 19:18:47.069: V/AudioPolicyManager(95): getDeviceForStrategy() from cache strategy 0, device 2
04-21 19:18:47.069: V/AudioPolicyManager(95): getNewDevice() selected device 2
04-21 19:18:47.069: V/AudioPolicyManager(95): setOutputDevice() output 1 device 2 delayMs 0
04-21 19:18:47.069: V/AudioPolicyManager(95): setOutputDevice() setting same device 2 or null device for output 1
04-21 19:18:47.069: V/AudioPolicyManager(95): releaseOutput() 1
04-21 19:18:47.159: I/InputReader(190): dispatchTouch::touch event's action is 1
04-21 19:18:47.159: I/InputDispatcher(190): Delivering touch to current input target
04-21 19:18:47.159: V/AudioPolicyManager(95): stopOutput() output 1, stream 1, session 8609
04-21 19:18:47.159: V/AudioPolicyManager(95): getNewDevice() selected device 0
04-21 19:18:47.159: V/AudioPolicyManager(95): setOutputDevice() output 1 device 0 delayMs 150
04-21 19:18:47.159: V/AudioPolicyManager(95): setOutputDevice() setting same device 0 or null device for output 1
04-21 19:18:47.329: I/InputReader(190): dispatchTouch::touch event's action is 0
04-21 19:18:47.329: I/InputDispatcher(190): Delivering touch to current input target
04-21 19:18:47.329: V/AudioPolicyManager(95): startOutput() output 1, stream 1, session 8610
04-21 19:18:47.329: V/AudioPolicyManager(95): getDeviceForStrategy() from cache strategy 0, device 2
04-21 19:18:47.329: V/AudioPolicyManager(95): getNewDevice() selected device 2
04-21 19:18:47.329: V/AudioPolicyManager(95): setOutputDevice() output 1 device 2 delayMs 0
04-21 19:18:47.329: V/AudioPolicyManager(95): setOutputDevice() setting same device 2 or null device for output 1
04-21 19:18:47.329: V/AudioPolicyManager(95): releaseOutput() 1
04-21 19:18:47.409: I/InputReader(190): dispatchTouch::touch event's action is 1
04-21 19:18:47.409: I/InputDispatcher(190): Delivering touch to current input target
04-21 19:18:47.439: V/AudioPolicyManager(95): stopOutput() output 1, stream 1, session 8610
04-21 19:18:47.439: V/AudioPolicyManager(95): getNewDevice() selected device 0
04-21 19:18:47.439: V/AudioPolicyManager(95): setOutputDevice() output 1 device 0 delayMs 150
04-21 19:18:47.439: V/AudioPolicyManager(95): setOutputDevice() setting same device 0 or null device for output 1
04-21 19:18:47.589: I/InputReader(190): dispatchTouch::touch event's action is 0
04-21 19:18:47.589: I/InputDispatcher(190): Delivering touch to current input target
04-21 19:18:47.599: V/AudioPolicyManager(95): startOutput() output 1, stream 1, session 8611
04-21 19:18:47.599: V/AudioPolicyManager(95): getDeviceForStrategy() from cache strategy 0, device 2
04-21 19:18:47.599: V/AudioPolicyManager(95): getNewDevice() selected device 2
04-21 19:18:47.599: V/AudioPolicyManager(95): setOutputDevice() output 1 device 2 delayMs 0
04-21 19:18:47.599: V/AudioPolicyManager(95): setOutputDevice() setting same device 2 or null device for output 1
04-21 19:18:47.599: V/AudioPolicyManager(95): releaseOutput() 1
04-21 19:18:47.679: I/InputReader(190): dispatchTouch::touch event's action is 1
04-21 19:18:47.679: I/InputDispatcher(190): Delivering touch to current input target
04-21 19:18:47.699: V/AudioPolicyManager(95): stopOutput() output 1, stream 1, session 8611
04-21 19:18:47.699: V/AudioPolicyManager(95): getNewDevice() selected device 0
04-21 19:18:47.699: V/AudioPolicyManager(95): setOutputDevice() output 1 device 0 delayMs 150
04-21 19:18:47.699: V/AudioPolicyManager(95): setOutputDevice() setting same device 0 or null device for output 1
04-21 19:18:48.069: W/FastDormancy(270): before ======= ENTER DORMANCY =======
04-21 19:18:48.099: I/WifiService(190): isShowingAccessPointListDialog():false
04-21 19:18:49.269: I/InputReader(190): dispatchTouch::touch event's action is 0
04-21 19:18:49.269: I/InputDispatcher(190): Delivering touch to current input target
04-21 19:18:49.389: I/InputReader(190): dispatchTouch::touch event's action is 1
04-21 19:18:49.389: I/InputDispatcher(190): Delivering touch to current input target
04-21 19:18:49.399: V/AudioPolicyManager(95): startOutput() output 1, stream 1, session 8612
04-21 19:18:49.399: V/AudioPolicyManager(95): getDeviceForStrategy() from cache strategy 0, device 2
04-21 19:18:49.399: V/AudioPolicyManager(95): getNewDevice() selected device 2
04-21 19:18:49.399: V/AudioPolicyManager(95): setOutputDevice() output 1 device 2 delayMs 0
04-21 19:18:49.399: V/AudioPolicyManager(95): setOutputDevice() setting same device 2 or null device for output 1
04-21 19:18:49.399: V/AudioPolicyManager(95): releaseOutput() 1
04-21 19:18:49.559: I/DHWR(263): [I] ./../engine/rel/source/wrapper/dhwr_main.c(501) DHWRSetAttribute: SetAttribute
04-21 19:18:49.559: I/DHWR(263): [I] ./../engine/rel/source/wrapper/dhwr_main.c(509) DHWRSetAttribute: ptype: 0, pMode:0x20fb78, nMode:1, pCands:10
04-21 19:18:49.559: I/DHWR(263): [I] ./../engine/rel/source/wrapper/dhwr_main.c(512) DHWRSetAttribute: pMode[0] lang:0, type:0xc
04-21 19:18:49.559: I/DHWR(263): [I] ./../engine/rel/source/wrapper/dhwr_main.c(501) DHWRSetAttribute: SetAttribute
04-21 19:18:49.569: I/DHWR(263): [I] ./../engine/rel/source/wrapper/dhwr_main.c(509) DHWRSetAttribute: ptype: 0, pMode:0x20fb78, nMode:1, pCands:10
04-21 19:18:49.569: I/DHWR(263): [I] ./../engine/rel/source/wrapper/dhwr_main.c(512) DHWRSetAttribute: pMode[0] lang:0, type:0xc
04-21 19:18:49.589: I/DHWR(263): [I] ./../engine/rel/source/wrapper/dhwr_main.c(1294) DHWRSetWritingArea: left:0, top:0, right:0, bottom:0, sizeRate:60
04-21 19:18:49.589: V/onEvaluateInputViewShown()(263): in inputmethodservice value = 1
04-21 19:18:49.589: V/onEvaluateInputViewShown()(263): in inputmethodservice value = 1
04-21 19:18:49.739: V/AudioPolicyManager(95): stopOutput() output 1, stream 1, session 8612
04-21 19:18:49.739: V/AudioPolicyManager(95): getNewDevice() selected device 0
04-21 19:18:49.739: V/AudioPolicyManager(95): setOutputDevice() output 1 device 0 delayMs 150
04-21 19:18:49.739: V/AudioPolicyManager(95): setOutputDevice() setting same device 0 or null device for output 1