Android - 在解除AlertDialog后清除值

时间:2014-03-24 14:27:22

标签: android listview alertdialog

我正在创建一个包含ListView的应用,只需点击ListView中的任意一项,就会弹出AlertDialog个自定义{{1}这将为用户提供更多选项。现在,自定义View中有一个TextView,它显示项目的名称,该名称已被用户长按。但是,当我第二次长按View中的任何项目时,会出现问题。

让我用一个例子清楚地解释这个 -

假设列表中有四个项目 -

  1. 数学
  2. 物理
  3. 地理
  4. 让我们说用户长按了数学"数学" ListView加载后(第一次)。结果,Activity弹出AlertDialog说"英语"。

    现在,在用户取消之前的对话框并再次长按,例如" Geography"之后。结果,TextView再次弹出AlertDialog说"英语"!然而,它应该显示"地理"。

    这是我使用的代码 -

    TextView

    Add_Topics.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; } }

    AddTopics_MenuAdapter.java

    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; } }

    SubjectsDatabase.java

    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(); } }

    Topic.java

    请帮我解决这个问题。在此先感谢!!

1 个答案:

答案 0 :(得分:1)

onCreateDialog创建并缓存对话框。对showDialog的第二次调用仅重新显示原始缓存对话框。如果要在显示之前修改对话框内容,则需要覆盖onPrepareDialog