最新编辑--------------------------------------------- ------------------------------- 我更新了代码,现在它是正确的,虽然我无法运行该功能。在长时间点击事件中没有任何事情发生......
在我的文件中显示来自数据库的行列表我放置了用于设置OnLongClickListener
的代码,但部分代码(已注释)返回错误:The constructor ListView(Monday.MyDiary) is undefined
。
以下是插入新代码的文件:
package com.example.classorganizer;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.cookbook.data.Constants;
import com.cookbook.data.MyDB;
public class Monday extends ListActivity {
private static final int MyMenu = 0;
MyDB dba;
DiaryAdapter myAdapter;
private class MyDiary{
public MyDiary(String t, String c){
title=t;
content=c;
ListView listView = new ListView(this); //here the error pops out
listView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
new EditListItemDialog(v.getContext()).show();
return true;
}
});
}
public String title;
public String content;
}
@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);
}
private 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();
}
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文件:
package com.example.classorganizer;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
class EditListItemDialog extends Dialog implements View.OnClickListener {
private View editText;
public EditListItemDialog(Context context) {
super(context);
}
@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);
}
@Override
public void onClick(View v) {
((TextView) editText).getText().toString();//here is your updated(or not updated) text
dismiss();
}
}
我不知道如何解决这个问题。我想要实现的是通过长按它们来编辑列表中显示的行的功能。
EDIT ---------------------------------------------- ------------------------------------
现在代码看起来像这样:
ListView listView = new ListView(Monday.this);
listView.setOnItemLongClickListener(new View.OnItemLongClickListener() {
@Override
public boolean onLongClick(View v) {
new EditListItemDialog(v.getContext()).show();
return true;
}
});
第二行的View.OnItemLongClickListener出现错误
EDIT ---------------------------------------------- ----------------------------------------
更新的代码没有错误,但不确定它是否正确:
ListView listView = new ListView(Monday.this);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
new EditListItemDialog(view.getContext()).show();
return true;
}
});
答案 0 :(得分:1)
Listview
不能setonLongClickListener
你应该实现setOnItemItemLongClickListener
因为ListView
包含项目列表,所以你总是可以实现其项目长按一下监听器下面:
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
return false;
}
});
答案 1 :(得分:0)
ListView listView = new ListView(this);
您应该向ListView构造函数传递一个上下文,而不是MyDiary
对象。
使用
修复它ListView listView = new ListView(Monday.this);
它应该有用,因为你的MyDiary是星期一的内部类,并不是静态的。
如果你改变这个,你应该将活动的Context
传递给MyDiary构造函数并使用它。
并且,对于您的下一个问题:您应该在ListView中使用OnItemLongClickListener
,而不是OnLongClickListener
。