Android-在AlertDialog关闭后清除值
我正在制作一个应用程序,它包含一个Android-在AlertDialog关闭后清除值,android,listview,android-alertdialog,Android,Listview,Android Alertdialog,我正在制作一个应用程序,它包含一个列表视图,长按列表视图中的任何项目都会弹出一个带有自定义视图的警报对话框,该视图将为用户提供更多选项。现在,自定义视图中有一个文本视图,显示项目名称,用户长时间单击的。但是,当我第二次长时间单击列表视图中的任何项目时,就会出现问题 让我用一个例子来解释清楚- 假设列表中有四项: 英式 数学 物理学 地理 假设用户在活动加载(第一次)后长时间单击“数学”。结果,AlertDialog弹出,其中TextView显示“英语” 现在,在用户取消上一个对话框并再次长时间单
列表视图
,长按列表视图
中的任何项目都会弹出一个带有自定义视图
的警报对话框
,该视图将为用户提供更多选项。现在,自定义视图
中有一个文本视图
,显示项目名称,用户长时间单击的。但是,当我第二次长时间单击列表视图中的任何项目时,就会出现问题
让我用一个例子来解释清楚-
假设列表中有四项:
英式
数学
物理学
地理
假设用户在活动加载(第一次)后长时间单击“数学”。结果,AlertDialog
弹出,其中TextView
显示“英语”
现在,在用户取消上一个对话框并再次长时间单击后,比如说“地理”。结果,AlertDialog
再次弹出,其中TextView
显示“英语”!然而,它应该显示“地理”
这是我正在使用的代码-
添加主题.java
:
package com.Swap.StudyBuddy;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Add_Topics extends Activity {
AddTopics_MenuAdapter adapter;
ListView topics_list;
ArrayList<Topic> the_subjects = new ArrayList<Topic>();
String new_subject;
SubjectsDatabase sd = new SubjectsDatabase(this);
EditText tpc_nm;
InputMethodManager imm;
ImageView list_bg;
ImageView bg_shadow;
TextView itemTitle;
int whereArgs2;
View dialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add__topics);
topics_list = (ListView) findViewById(R.id.topics_list);
topics_list.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
whereArgs2 = arg2;
showDialog(1);
return false;
}
});
sd.getWritableDatabase();
the_subjects = sd.getTopics();
overridePendingTransition(R.anim.reverse_in_left, R.anim.reverse_out_left);
Animation enter = AnimationUtils.loadAnimation(getBaseContext(), R.anim.upcoming_menu);
Animation enter_slow = AnimationUtils.loadAnimation(getBaseContext(), R.anim.enter_l2r_slide);
TextView des = (TextView)findViewById(R.id.des_at);
TextView title = (TextView)findViewById(R.id.title_at);
Button add_topic = (Button)findViewById(R.id.add_topic_button);
Typeface roboto_lt = Typeface.createFromAsset(getAssets(), "Roboto-Light.ttf");
des.setTypeface(roboto_lt);
title.setTypeface(roboto_lt);
add_topic.setTypeface(roboto_lt);
title.startAnimation(enter);
des.startAnimation(enter_slow);
adapter = new AddTopics_MenuAdapter(this, the_subjects);
topics_list.setAdapter(adapter);
backgroundChanges();
}
public void onClickAddTopic(View v) {
showDialog(0);
tpc_nm.requestFocus();
getBaseContext();
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
protected Dialog onCreateDialog(int id) {
switch(id) {
case 0:
tpc_nm = new EditText(this);
tpc_nm.setHint("New Topic/Subject Name");
Typeface roboto_lt = Typeface.createFromAsset(getAssets(), "Roboto-Light.ttf");
tpc_nm.setTypeface(roboto_lt);
Builder bld = new AlertDialog.Builder(this);
bld.setIcon(R.drawable.ic_launcher);
bld.setTitle("Add Topic/Subject");
bld.setView(tpc_nm);
bld.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButon) {
String new_topic_name = tpc_nm.getText().toString();
new_subject = new_topic_name;
adapter = null;
the_subjects.add(new Topic(new_topic_name));
sd.addTopic(new Topic(new_topic_name));
sd.close();
adapter = new AddTopics_MenuAdapter(getBaseContext(), the_subjects);
imm.toggleSoftInput(InputMethodManager.RESULT_HIDDEN, 0);
tpc_nm.setText("");
backgroundChanges();
adapter.notifyDataSetChanged();
}
});
bld.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
imm.toggleSoftInput(InputMethodManager.RESULT_HIDDEN, 0);
tpc_nm.setText("");
backgroundChanges();
}
});
return bld.create();
case 1:
String item = the_subjects.get(whereArgs2).getTopic();
final Builder build = new AlertDialog.Builder(this);
LayoutInflater li = this.getLayoutInflater();
dialog = li.inflate(R.layout.topics_dialog, null);
itemTitle = (TextView) dialog.findViewById(R.id.itemTitle);
itemTitle.setText(item);
Toast.makeText(getBaseContext(), String.valueOf(whereArgs2) , Toast.LENGTH_SHORT).show();
build.setView(dialog);
return build.create();
case 2:
final EditText edited_topic_name = new EditText(this);
Typeface roboto_light = Typeface.createFromAsset(getAssets(), "Roboto-Light.ttf");
edited_topic_name.setTypeface(roboto_light);
edited_topic_name.setHint("New Topic Name");
Builder bld1 = new AlertDialog.Builder(this);
bld1.setView(edited_topic_name);
bld1.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
sd.getWritableDatabase();
sd.updateTopic(new Topic(edited_topic_name.getText().toString()));
sd.close();
the_subjects.clear();
adapter = null;
the_subjects = sd.getTopics();
adapter = new AddTopics_MenuAdapter(getBaseContext(), the_subjects);
edited_topic_name.setText("");
adapter.notifyDataSetChanged();
}
});
bld1.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
edited_topic_name.setText("");
}
});
}
return null;
}
/*public void onPause() {
super.onPause();
the_subjects.clear();
}
public void onStop() {
super.onStop();
the_subjects.clear();
}
public void onDestroy() {
super.onDestroy();
the_subjects.clear();
} */
public void backgroundChanges() {
list_bg = (ImageView) findViewById(R.id.list_bg);
bg_shadow = (ImageView) findViewById(R.id.bg_shadow);
int topic_list_length = the_subjects.size();
switch(topic_list_length) {
case 0:
break;
default:
list_bg.setImageResource(R.drawable.round);
bg_shadow.setImageResource(R.drawable.shadow);
}
}
public void onClickRemove(View v) {
sd.getWritableDatabase();
sd.removeTopic(new Topic(the_subjects.get(whereArgs2).toString()));
adapter = null;
the_subjects = null;
the_subjects = sd.getTopics();
sd.close();
adapter = new AddTopics_MenuAdapter(this, the_subjects);
dismissDialog(1);
adapter.notifyDataSetChanged();
topics_list.setAdapter(adapter);
}
public void onClickEdit(View v) {
showDialog(2);
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_add__topics, menu);
return true;
}
}
package com.Swap.StudyBuddy;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class AddTopics_MenuAdapter extends ArrayAdapter<Topic> {
private final Context context;
final ArrayList<Topic> topics;
public AddTopics_MenuAdapter(Context context, ArrayList<Topic> topics) {
super(context, R.layout.add_topics_menu, topics);
this.context = context;
this.topics = topics;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View topicsView = inflater.inflate(R.layout.add_topics_menu, parent, false);
final TextView topic_name = (TextView) topicsView.findViewById(R.id.topic_title);
Typeface rbt_lt = Typeface.createFromAsset(getContext().getAssets(), "Roboto-Light.ttf");
Animation enter = AnimationUtils.loadAnimation(getContext(), R.anim.upcoming_menu_fast);
Topic topic = topics.get(position);
topic_name.setText(topic.getTopic());
topic_name.setTypeface(rbt_lt);
topic_name.setAnimation(enter);
return topicsView;
}
}
package com.Swap.StudyBuddy;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class SubjectsDatabase extends SQLiteOpenHelper{
private static final String SUBJECT_ID = "id";
private static final String SUBJECT_NAME = "name";
private static final String DATABASE_NAME = "topicsDatabase";
private static final String TABLE_TOPICS = "topics";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "CREATE_TABLE" + TABLE_TOPICS + "(" +
SUBJECT_ID + "INTEGER_PRIMARY_KEY, " + SUBJECT_NAME + "TEXT" + ")";
public SubjectsDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DATABASE_CREATE);
}
catch(SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXSISTS topics");
onCreate(db);
}
public void addTopic(Topic topic) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues topics = new ContentValues();
topics.put(SUBJECT_NAME, topic.getTopic());
db.insert(TABLE_TOPICS, null, topics);
db.close();
}
public void removeTopic(Topic topic) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_TOPICS, SUBJECT_ID + " = ?",
new String[] {String.valueOf(topic.get_id())});
db.close();
}
public ArrayList<Topic> getTopics(){
ArrayList<Topic> topics = new ArrayList<Topic>();
String selectQuery = "SELECT * FROM " + TABLE_TOPICS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cur = db.rawQuery(selectQuery, null);
if(cur.moveToFirst()) {
do {
Topic topic = new Topic();
topic.set_id(Integer.parseInt(cur.getString(0)));
topic.setTopic(cur.getString(1));
topics.add(topic);
} while(cur.moveToNext());
}
db.close();
return topics;
}
public void updateTopic(Topic topic_name) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues topic = new ContentValues();
topic.put(SUBJECT_NAME, topic_name.getTopic());
db.update(TABLE_TOPICS, topic, SUBJECT_ID + " = ?",
new String[] {String.valueOf(topic_name.get_id())});
db.close();
}
}
SubjectsDatabase.java
:
package com.Swap.StudyBuddy;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Add_Topics extends Activity {
AddTopics_MenuAdapter adapter;
ListView topics_list;
ArrayList<Topic> the_subjects = new ArrayList<Topic>();
String new_subject;
SubjectsDatabase sd = new SubjectsDatabase(this);
EditText tpc_nm;
InputMethodManager imm;
ImageView list_bg;
ImageView bg_shadow;
TextView itemTitle;
int whereArgs2;
View dialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add__topics);
topics_list = (ListView) findViewById(R.id.topics_list);
topics_list.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
whereArgs2 = arg2;
showDialog(1);
return false;
}
});
sd.getWritableDatabase();
the_subjects = sd.getTopics();
overridePendingTransition(R.anim.reverse_in_left, R.anim.reverse_out_left);
Animation enter = AnimationUtils.loadAnimation(getBaseContext(), R.anim.upcoming_menu);
Animation enter_slow = AnimationUtils.loadAnimation(getBaseContext(), R.anim.enter_l2r_slide);
TextView des = (TextView)findViewById(R.id.des_at);
TextView title = (TextView)findViewById(R.id.title_at);
Button add_topic = (Button)findViewById(R.id.add_topic_button);
Typeface roboto_lt = Typeface.createFromAsset(getAssets(), "Roboto-Light.ttf");
des.setTypeface(roboto_lt);
title.setTypeface(roboto_lt);
add_topic.setTypeface(roboto_lt);
title.startAnimation(enter);
des.startAnimation(enter_slow);
adapter = new AddTopics_MenuAdapter(this, the_subjects);
topics_list.setAdapter(adapter);
backgroundChanges();
}
public void onClickAddTopic(View v) {
showDialog(0);
tpc_nm.requestFocus();
getBaseContext();
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
protected Dialog onCreateDialog(int id) {
switch(id) {
case 0:
tpc_nm = new EditText(this);
tpc_nm.setHint("New Topic/Subject Name");
Typeface roboto_lt = Typeface.createFromAsset(getAssets(), "Roboto-Light.ttf");
tpc_nm.setTypeface(roboto_lt);
Builder bld = new AlertDialog.Builder(this);
bld.setIcon(R.drawable.ic_launcher);
bld.setTitle("Add Topic/Subject");
bld.setView(tpc_nm);
bld.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButon) {
String new_topic_name = tpc_nm.getText().toString();
new_subject = new_topic_name;
adapter = null;
the_subjects.add(new Topic(new_topic_name));
sd.addTopic(new Topic(new_topic_name));
sd.close();
adapter = new AddTopics_MenuAdapter(getBaseContext(), the_subjects);
imm.toggleSoftInput(InputMethodManager.RESULT_HIDDEN, 0);
tpc_nm.setText("");
backgroundChanges();
adapter.notifyDataSetChanged();
}
});
bld.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
imm.toggleSoftInput(InputMethodManager.RESULT_HIDDEN, 0);
tpc_nm.setText("");
backgroundChanges();
}
});
return bld.create();
case 1:
String item = the_subjects.get(whereArgs2).getTopic();
final Builder build = new AlertDialog.Builder(this);
LayoutInflater li = this.getLayoutInflater();
dialog = li.inflate(R.layout.topics_dialog, null);
itemTitle = (TextView) dialog.findViewById(R.id.itemTitle);
itemTitle.setText(item);
Toast.makeText(getBaseContext(), String.valueOf(whereArgs2) , Toast.LENGTH_SHORT).show();
build.setView(dialog);
return build.create();
case 2:
final EditText edited_topic_name = new EditText(this);
Typeface roboto_light = Typeface.createFromAsset(getAssets(), "Roboto-Light.ttf");
edited_topic_name.setTypeface(roboto_light);
edited_topic_name.setHint("New Topic Name");
Builder bld1 = new AlertDialog.Builder(this);
bld1.setView(edited_topic_name);
bld1.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
sd.getWritableDatabase();
sd.updateTopic(new Topic(edited_topic_name.getText().toString()));
sd.close();
the_subjects.clear();
adapter = null;
the_subjects = sd.getTopics();
adapter = new AddTopics_MenuAdapter(getBaseContext(), the_subjects);
edited_topic_name.setText("");
adapter.notifyDataSetChanged();
}
});
bld1.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
edited_topic_name.setText("");
}
});
}
return null;
}
/*public void onPause() {
super.onPause();
the_subjects.clear();
}
public void onStop() {
super.onStop();
the_subjects.clear();
}
public void onDestroy() {
super.onDestroy();
the_subjects.clear();
} */
public void backgroundChanges() {
list_bg = (ImageView) findViewById(R.id.list_bg);
bg_shadow = (ImageView) findViewById(R.id.bg_shadow);
int topic_list_length = the_subjects.size();
switch(topic_list_length) {
case 0:
break;
default:
list_bg.setImageResource(R.drawable.round);
bg_shadow.setImageResource(R.drawable.shadow);
}
}
public void onClickRemove(View v) {
sd.getWritableDatabase();
sd.removeTopic(new Topic(the_subjects.get(whereArgs2).toString()));
adapter = null;
the_subjects = null;
the_subjects = sd.getTopics();
sd.close();
adapter = new AddTopics_MenuAdapter(this, the_subjects);
dismissDialog(1);
adapter.notifyDataSetChanged();
topics_list.setAdapter(adapter);
}
public void onClickEdit(View v) {
showDialog(2);
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_add__topics, menu);
return true;
}
}
package com.Swap.StudyBuddy;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class AddTopics_MenuAdapter extends ArrayAdapter<Topic> {
private final Context context;
final ArrayList<Topic> topics;
public AddTopics_MenuAdapter(Context context, ArrayList<Topic> topics) {
super(context, R.layout.add_topics_menu, topics);
this.context = context;
this.topics = topics;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View topicsView = inflater.inflate(R.layout.add_topics_menu, parent, false);
final TextView topic_name = (TextView) topicsView.findViewById(R.id.topic_title);
Typeface rbt_lt = Typeface.createFromAsset(getContext().getAssets(), "Roboto-Light.ttf");
Animation enter = AnimationUtils.loadAnimation(getContext(), R.anim.upcoming_menu_fast);
Topic topic = topics.get(position);
topic_name.setText(topic.getTopic());
topic_name.setTypeface(rbt_lt);
topic_name.setAnimation(enter);
return topicsView;
}
}
package com.Swap.StudyBuddy;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class SubjectsDatabase extends SQLiteOpenHelper{
private static final String SUBJECT_ID = "id";
private static final String SUBJECT_NAME = "name";
private static final String DATABASE_NAME = "topicsDatabase";
private static final String TABLE_TOPICS = "topics";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "CREATE_TABLE" + TABLE_TOPICS + "(" +
SUBJECT_ID + "INTEGER_PRIMARY_KEY, " + SUBJECT_NAME + "TEXT" + ")";
public SubjectsDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DATABASE_CREATE);
}
catch(SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXSISTS topics");
onCreate(db);
}
public void addTopic(Topic topic) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues topics = new ContentValues();
topics.put(SUBJECT_NAME, topic.getTopic());
db.insert(TABLE_TOPICS, null, topics);
db.close();
}
public void removeTopic(Topic topic) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_TOPICS, SUBJECT_ID + " = ?",
new String[] {String.valueOf(topic.get_id())});
db.close();
}
public ArrayList<Topic> getTopics(){
ArrayList<Topic> topics = new ArrayList<Topic>();
String selectQuery = "SELECT * FROM " + TABLE_TOPICS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cur = db.rawQuery(selectQuery, null);
if(cur.moveToFirst()) {
do {
Topic topic = new Topic();
topic.set_id(Integer.parseInt(cur.getString(0)));
topic.setTopic(cur.getString(1));
topics.add(topic);
} while(cur.moveToNext());
}
db.close();
return topics;
}
public void updateTopic(Topic topic_name) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues topic = new ContentValues();
topic.put(SUBJECT_NAME, topic_name.getTopic());
db.update(TABLE_TOPICS, topic, SUBJECT_ID + " = ?",
new String[] {String.valueOf(topic_name.get_id())});
db.close();
}
}
请帮我解决这个问题。提前谢谢 创建并缓存对话框。对的第二个调用只是重新显示原始的缓存对话框。如果要在显示之前修改对话框内容,则需要重写。有效。如果调用了removeDialog()
,而不是调用了dismissDialog()
,或者调用了其他方法来解除旧的/不推荐使用的对话框(例如setPositiveButton()
的onClick()
)。