gpt4 book ai didi

android - Dialogox 中的按钮单击监听器问题

转载 作者:搜寻专家 更新时间:2023-11-01 08:20:08 25 4
gpt4 key购买 nike

我正在尝试从父对话框打开子对话框,它工作正常。里面有几个按钮。我面临的问题是点击监听器在子对话框中没有响应,而且子对话框没有关闭。

public class NoteListAdapter  extends RecyclerView.Adapter<NoteListAdapter.NoteListHolder>{

private Context context;
private LayoutInflater inflater;
private ItemModel current;
public List<ItemModel> data;
SubListAdapter adapter;
NoteListHolder holder;
String get_text;
EditText et_get_text;
AlertDialog alert_dialog, tag_alert_dialog;
String id;
LinearLayout tag_important, tag_study, tag_progress, tag_normal, tag_watchlist, tag_shopping, tag_food,tag_enterain,
tag_transport, tag_services, tag_health;
TextView set_date;
String tag_text, tag_color;
private List<TagsModel> tagList;
String note_id;
RecyclerView sub_recycler;

/* constructor */

public NoteListAdapter(Context context, List<ItemModel> data) {

inflater = LayoutInflater.from(context);
this.context = context;
this.data = data;
}


/* notify on item delete */

public void removeItem(int position) {
data.remove(position);
notifyItemRemoved(position);
notifyDataSetChanged();
}

/* delete all items */
public void removeAll() {
data.clear();
notifyDataSetChanged();
}

@Override
public NoteListAdapter.NoteListHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View rootView = inflater.inflate(R.layout.note_recycler, parent, false);
holder = new NoteListHolder(rootView);
return holder;
}

@Override
public void onBindViewHolder(NoteListAdapter.NoteListHolder holder, int position) {

current = data.get(position);
// tag_current = tag_data.get(position);
String note_id = data.get(position).getId();
Log.d("note_idm","note id "+note_id);

ListNoteDataBase dataBase = new ListNoteDataBase(context);
dataBase.open();
dataBase.inserttags("abc ", note_id, "ef1232");
tagList = dataBase.getTagList(note_id);

String text = tagList.get(position).getTag();
String col = tagList.get(position).getTag_color();

final String input_text = current.getText();
holder.note_text.setText(input_text);

holder.tag_view.setText(text);
dataBase.close();

// holder.tagCircle.setColor(Color.parseColor("#" + col));
//holder.tagCircle.setColor(tag_color);*/

}

@Override
public int getItemCount() {
return data.size();
}

/* edit item from recycler view */

public void restoreItem(ItemModel item, int position) {
data.add(position, item);
notifyItemInserted(position);
}



/* declare and initialize NoteListHolder class*/

class NoteListHolder extends RecyclerView.ViewHolder{
private TextView note_text, tag_view;
public RelativeLayout viewBackground, viewForeground;
String note_txt_str;
LinearLayout add_tag;
Button tag_set;
public GradientDrawable tagCircle;

public NoteListHolder(View itemView) {
super(itemView);
note_text = itemView.findViewById(R.id.note_text);
viewBackground = itemView.findViewById(R.id.view_background);
viewForeground = itemView.findViewById(R.id.view_foreground);
tag_view = itemView.findViewById(R.id.tag_view);
tagCircle = (GradientDrawable) tag_view.getBackground();

viewForeground.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = getAdapterPosition();
note_id = data.get(pos).getId();
Log.d("note_id", "note id " + note_id);
Bundle b = new Bundle();
b.putString("note_id", note_id);
edit_dialog();
}
});
}

private void edit_dialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(R.layout.notedialog);
builder.setTitle("Edit Note");
// alert_dialog = builder.create();
alert_dialog = builder.show();
alert_dialog.show();
et_get_text = alert_dialog.findViewById(R.id.txtNote);
sub_recycler = alert_dialog.findViewById(R.id.recycler_dialog);

add_tag = alert_dialog.findViewById(R.id.add_tag);
add_tag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(R.layout.tag_dialog);
builder.setTitle("Select Tag");
//tag_alert_dialog = builder.create();
tag_alert_dialog = builder.show();
tag_alert_dialog.getWindow().setGravity(Gravity.BOTTOM);
tag_alert_dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
tag_alert_dialog.show();

tag_important = tag_alert_dialog.findViewById(R.id.tag_imp);
tag_food = tag_alert_dialog.findViewById(R.id.tag_food);
tag_enterain = tag_alert_dialog.findViewById(R.id.tag_enterainment);
tag_normal = tag_alert_dialog.findViewById(R.id.tag_normal);
tag_health = tag_alert_dialog.findViewById(R.id.tag_health);
tag_transport = tag_alert_dialog.findViewById(R.id.tag_transport);
tag_study = tag_alert_dialog.findViewById(R.id.tag_study);
tag_services = tag_alert_dialog.findViewById(R.id.tag_services);
tag_shopping = tag_alert_dialog.findViewById(R.id.tag_shop);
tag_progress = tag_alert_dialog.findViewById(R.id.tag_progress);
tag_watchlist = tag_alert_dialog.findViewById(R.id.tag_watchlist);
tag_set = tag_alert_dialog.findViewById(R.id.set_tag);

tag_important.setOnClickListener(this);
tag_food.setOnClickListener(this);
tag_enterain.setOnClickListener(this);
tag_normal.setOnClickListener(this);
tag_health.setOnClickListener(this);
tag_transport.setOnClickListener(this);
tag_study.setOnClickListener(this);
tag_services.setOnClickListener(this);
tag_shopping.setOnClickListener(this);
tag_progress.setOnClickListener(this);
tag_watchlist.setOnClickListener(this);

ListNoteDataBase appDatabase = new ListNoteDataBase(context);
appDatabase.open();
tagList = appDatabase.getTagList(note_id);
String tag_id = tagList.get(getAdapterPosition()).getTagId();
int id = v.getId();
switch (id) {
case R.id.tag_imp:
tag_text = "Important";
tag_color = "C61F2B";

appDatabase.updateTags(tag_text, tag_id, tag_color);
appDatabase.close();

break;
case R.id.tag_progress:
tag_text = "Progress";
tag_color = "A50CE2";

appDatabase.updateTags(tag_text, tag_id, tag_color);
appDatabase.close();

break;
case R.id.tag_study:
tag_text = "Study";
tag_color = "FCA81D";
appDatabase.updateTags(tag_text, tag_id, tag_color);
appDatabase.close();

break;
case R.id.tag_watchlist:
tag_text = "Watchlist";
tag_color = "F4C664";

appDatabase.updateTags(tag_text, tag_id, tag_color);
appDatabase.close();

break;
case R.id.tag_normal:
tag_text = "Normal";
tag_color = "10CAC5";

appDatabase.updateTags(tag_text, tag_id, tag_color);
appDatabase.close();


break;
case R.id.tag_shop:
tag_text = "Shopping";
tag_color = "15B412";

appDatabase.updateTags(tag_text, tag_id, tag_color);
appDatabase.close();


break;
case R.id.tag_food:
tag_text = "Food & Drinks";
tag_color = "F15696";

appDatabase.updateTags(tag_text, tag_id, tag_color);
appDatabase.close();

break;
case R.id.tag_enterainment:
tag_text = "Entertainment";
tag_color = "A3128E";

appDatabase.updateTags(tag_text, tag_id, tag_color);
appDatabase.close();

break;
case R.id.tag_transport:
tag_text = "Transportation";
tag_color = "3324BD";
appDatabase.updateTags(tag_text, tag_id, tag_color);
appDatabase.close();

break;
case R.id.tag_services:
tag_text = "Services";
tag_color = "#F3C861";
appDatabase.updateTags(tag_text, tag_id, tag_color);
appDatabase.close();

break;
case R.id.tag_health:
tag_text = "Health";
tag_color = "EC4A3A";
appDatabase.updateTags(tag_text, tag_id, tag_color);
appDatabase.close();

break;
default:
tag_text = "";
tag_color = "ffffff";
appDatabase.updateTags(tag_text, tag_id, tag_color);
appDatabase.close();
break;
}

tag_set.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// if (tag_alert_dialog.isShowing()) {
tag_alert_dialog.hide();
//}
}
});
}
});
final EditText et_sub_txt = alert_dialog.findViewById(R.id.et_subNote);
ImageButton sub_action = alert_dialog.findViewById(R.id.action_done);

sub_action.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String sub_text_str = et_sub_txt.getText().toString();
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
ListNoteDataBase appDatabase = new ListNoteDataBase(context);
appDatabase.open();
appDatabase.insertsubnotes(sub_text_str, note_id);
appDatabase.close();
subNote_adapter();
et_sub_txt.getText().clear();
et_sub_txt.clearFocus();
}
});
subNote_adapter();

set_date = alert_dialog.findViewById(R.id.creation_date);
String get_date = data.get(getAdapterPosition()).getDate();
set_date.setText(get_date);

String show_dia_text = data.get(getAdapterPosition()).getText();
id = data.get(getAdapterPosition()).getId();


//set_date.setText((CharSequence) update_date);

/*set cursor position */

note_txt_str = note_text.getText().toString();
et_get_text.setText(note_txt_str);
int position = show_dia_text.length();
et_get_text.setSelection(position);

Button button_edit_done = alert_dialog.findViewById(R.id.edit_note);
button_edit_done.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

get_text = et_get_text.getText().toString().trim();
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
ListNoteDataBase appDatabase = new ListNoteDataBase(context);
appDatabase.open();
appDatabase.updateNotes(get_text, id);
appDatabase.close();
note_text.setText(get_text);
if (alert_dialog.isShowing()) {
alert_dialog.dismiss();
}

}
});
setOnDoneButton();
}

private void setOnDoneButton() {
et_get_text.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
get_text = et_get_text.getText().toString().trim();
ListNoteDataBase appDatabase = new ListNoteDataBase(context);
appDatabase.open();
appDatabase.updateNotes(get_text, id);
appDatabase.close();
note_text.setText(get_text);

if (alert_dialog.isShowing()) {
alert_dialog.dismiss();
}
}
return false;
}
});
}

private void subNote_adapter() {
// recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL,false));
// recyclerView.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
sub_recycler.setLayoutManager(new GridLayoutManager(context, 1));
ListNoteDataBase appDatabase = new ListNoteDataBase(context);
appDatabase.open();
adapter = new SubListAdapter(context, appDatabase.getSubList(note_id));
sub_recycler.setAdapter(adapter);
adapter.notifyDataSetChanged();
appDatabase.close();
}

}

以上代码是在我的adapter中写的。我想从单击按钮并关闭对话框中获取标签文本和标签颜色值。但它没有实现任何点击监听器。

最佳答案

public class NoteListAdapter  extends RecyclerView.Adapter<NoteListAdapter.NoteListHolder>{

private Context context;
private LayoutInflater inflater;
private ItemModel current;
public List<ItemModel> data;
SubListAdapter adapter;
NoteListHolder holder;
String get_text;
EditText et_get_text;
AlertDialog alert_dialog, tag_alert_dialog;
String id;
LinearLayout tag_important, tag_study, tag_progress, tag_normal, tag_watchlist, tag_shopping, tag_food,tag_enterain,
tag_transport, tag_services, tag_health;
TextView set_date;
String tag_text, tag_color;
private List<TagsModel> tagList;
String note_id;
RecyclerView sub_recycler;

/* constructor */

public NoteListAdapter(Context context, List<ItemModel> data) {

inflater = LayoutInflater.from(context);
this.context = context;
this.data = data;
}


/* notify on item delete */

public void removeItem(int position) {
data.remove(position);
notifyItemRemoved(position);
notifyDataSetChanged();
}

/* delete all items */
public void removeAll() {
data.clear();
notifyDataSetChanged();
}

@Override
public NoteListAdapter.NoteListHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View rootView = inflater.inflate(R.layout.note_recycler, parent, false);
holder = new NoteListHolder(rootView);
return holder;
}

@Override
public void onBindViewHolder(NoteListAdapter.NoteListHolder holder, int position) {

current = data.get(position);
// tag_current = tag_data.get(position);
String note_id = data.get(position).getId();
Log.d("note_idm","note id "+note_id);

ListNoteDataBase dataBase = new ListNoteDataBase(context);
dataBase.open();
dataBase.inserttags("abc ", note_id, "ef1232");
tagList = dataBase.getTagList(note_id);

String text = tagList.get(position).getTag();
String col = tagList.get(position).getTag_color();

final String input_text = current.getText();
holder.note_text.setText(input_text);

holder.tag_view.setText(text);
dataBase.close();

// holder.tagCircle.setColor(Color.parseColor("#" + col));
//holder.tagCircle.setColor(tag_color);*/

}

@Override
public int getItemCount() {
return data.size();
}

/* edit item from recycler view */

public void restoreItem(ItemModel item, int position) {
data.add(position, item);
notifyItemInserted(position);
}



/* declare and initialize NoteListHolder class*/

class NoteListHolder extends RecyclerView.ViewHolder{
private TextView note_text, tag_view;
public RelativeLayout viewBackground, viewForeground;
String note_txt_str;
LinearLayout add_tag;
Button tag_set;
public GradientDrawable tagCircle;

public NoteListHolder(View itemView) {
super(itemView);
note_text = itemView.findViewById(R.id.note_text);
viewBackground = itemView.findViewById(R.id.view_background);
viewForeground = itemView.findViewById(R.id.view_foreground);
tag_view = itemView.findViewById(R.id.tag_view);
tagCircle = (GradientDrawable) tag_view.getBackground();

viewForeground.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = getAdapterPosition();
note_id = data.get(pos).getId();
Log.d("note_id", "note id " + note_id);
Bundle b = new Bundle();
b.putString("note_id", note_id);
edit_dialog();
}
});
}

private void edit_dialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(R.layout.notedialog);
builder.setTitle("Edit Note");
// alert_dialog = builder.create();
alert_dialog = builder.show();
//alert_dialog.show();
et_get_text = alert_dialog.findViewById(R.id.txtNote);
sub_recycler = alert_dialog.findViewById(R.id.recycler_dialog);

add_tag = alert_dialog.findViewById(R.id.add_tag);
add_tag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(R.layout.tag_dialog);
builder.setTitle("Select Tag");
//tag_alert_dialog = builder.create();
tag_alert_dialog = builder.show();
tag_alert_dialog.getWindow().setGravity(Gravity.BOTTOM);
tag_alert_dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
//tag_alert_dialog.show();

tag_important = tag_alert_dialog.findViewById(R.id.tag_imp);
tag_food = tag_alert_dialog.findViewById(R.id.tag_food);
tag_enterain = tag_alert_dialog.findViewById(R.id.tag_enterainment);
tag_normal = tag_alert_dialog.findViewById(R.id.tag_normal);
tag_health = tag_alert_dialog.findViewById(R.id.tag_health);
tag_transport = tag_alert_dialog.findViewById(R.id.tag_transport);
tag_study = tag_alert_dialog.findViewById(R.id.tag_study);
tag_services = tag_alert_dialog.findViewById(R.id.tag_services);
tag_shopping = tag_alert_dialog.findViewById(R.id.tag_shop);
tag_progress = tag_alert_dialog.findViewById(R.id.tag_progress);
tag_watchlist = tag_alert_dialog.findViewById(R.id.tag_watchlist);
tag_set = tag_alert_dialog.findViewById(R.id.set_tag);

View.OnClickListener listener=new View.OnClickListener(){
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.tag_imp:
tag_text = "Important";
tag_color = "C61F2B";

appDatabase.updateTags(tag_text, tag_id, tag_color);
appDatabase.close();

break;
case R.id.tag_progress:
tag_text = "Progress";
tag_color = "A50CE2";

appDatabase.updateTags(tag_text, tag_id, tag_color);
appDatabase.close();

break;
case R.id.tag_study:
tag_text = "Study";
tag_color = "FCA81D";
appDatabase.updateTags(tag_text, tag_id, tag_color);
appDatabase.close();

break;
case R.id.tag_watchlist:
tag_text = "Watchlist";
tag_color = "F4C664";

appDatabase.updateTags(tag_text, tag_id, tag_color);
appDatabase.close();

break;
case R.id.tag_normal:
tag_text = "Normal";
tag_color = "10CAC5";

appDatabase.updateTags(tag_text, tag_id, tag_color);
appDatabase.close();


break;
case R.id.tag_shop:
tag_text = "Shopping";
tag_color = "15B412";

appDatabase.updateTags(tag_text, tag_id, tag_color);
appDatabase.close();


break;
case R.id.tag_food:
tag_text = "Food & Drinks";
tag_color = "F15696";

appDatabase.updateTags(tag_text, tag_id, tag_color);
appDatabase.close();

break;
case R.id.tag_enterainment:
tag_text = "Entertainment";
tag_color = "A3128E";

appDatabase.updateTags(tag_text, tag_id, tag_color);
appDatabase.close();

break;
case R.id.tag_transport:
tag_text = "Transportation";
tag_color = "3324BD";
appDatabase.updateTags(tag_text, tag_id, tag_color);
appDatabase.close();

break;
case R.id.tag_services:
tag_text = "Services";
tag_color = "#F3C861";
appDatabase.updateTags(tag_text, tag_id, tag_color);
appDatabase.close();

break;
case R.id.tag_health:
tag_text = "Health";
tag_color = "EC4A3A";
appDatabase.updateTags(tag_text, tag_id, tag_color);
appDatabase.close();

break;
default:
tag_text = "";
tag_color = "ffffff";
appDatabase.updateTags(tag_text, tag_id, tag_color);
appDatabase.close();
break;
}
}
};

tag_important.setOnClickListener(listener);
tag_food.setOnClickListener(listener);
tag_enterain.setOnClickListener(listener);
tag_normal.setOnClickListener(listener);
tag_health.setOnClickListener(listener);
tag_transport.setOnClickListener(listener);
tag_study.setOnClickListener(listener);
tag_services.setOnClickListener(listener);
tag_shopping.setOnClickListener(listener);
tag_progress.setOnClickListener(listener);
tag_watchlist.setOnClickListener(listener);

ListNoteDataBase appDatabase = new ListNoteDataBase(context);
appDatabase.open();
tagList = appDatabase.getTagList(note_id);
String tag_id = tagList.get(getAdapterPosition()).getTagId();


tag_set.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// if (tag_alert_dialog.isShowing()) {
tag_alert_dialog.hide();
//}
}
});
}
});
final EditText et_sub_txt = alert_dialog.findViewById(R.id.et_subNote);
ImageButton sub_action = alert_dialog.findViewById(R.id.action_done);

sub_action.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String sub_text_str = et_sub_txt.getText().toString();
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
ListNoteDataBase appDatabase = new ListNoteDataBase(context);
appDatabase.open();
appDatabase.insertsubnotes(sub_text_str, note_id);
appDatabase.close();
subNote_adapter();
et_sub_txt.getText().clear();
et_sub_txt.clearFocus();
}
});
subNote_adapter();

set_date = alert_dialog.findViewById(R.id.creation_date);
String get_date = data.get(getAdapterPosition()).getDate();
set_date.setText(get_date);

String show_dia_text = data.get(getAdapterPosition()).getText();
id = data.get(getAdapterPosition()).getId();


//set_date.setText((CharSequence) update_date);

/*set cursor position */

note_txt_str = note_text.getText().toString();
et_get_text.setText(note_txt_str);
int position = show_dia_text.length();
et_get_text.setSelection(position);

Button button_edit_done = alert_dialog.findViewById(R.id.edit_note);
button_edit_done.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

get_text = et_get_text.getText().toString().trim();
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
ListNoteDataBase appDatabase = new ListNoteDataBase(context);
appDatabase.open();
appDatabase.updateNotes(get_text, id);
appDatabase.close();
note_text.setText(get_text);
if (alert_dialog.isShowing()) {
alert_dialog.dismiss();
}

}
});
setOnDoneButton();
}

private void setOnDoneButton() {
et_get_text.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
get_text = et_get_text.getText().toString().trim();
ListNoteDataBase appDatabase = new ListNoteDataBase(context);
appDatabase.open();
appDatabase.updateNotes(get_text, id);
appDatabase.close();
note_text.setText(get_text);

if (alert_dialog.isShowing()) {
alert_dialog.dismiss();
}
}
return false;
}
});
}

private void subNote_adapter() {
// recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL,false));
// recyclerView.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
sub_recycler.setLayoutManager(new GridLayoutManager(context, 1));
ListNoteDataBase appDatabase = new ListNoteDataBase(context);
appDatabase.open();
adapter = new SubListAdapter(context, appDatabase.getSubList(note_id));
sub_recycler.setAdapter(adapter);
adapter.notifyDataSetChanged();
appDatabase.close();
}

}

关于android - Dialogox 中的按钮单击监听器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52400170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com