gpt4 book ai didi

Android - 关闭 AlertDialog 后清除值

转载 作者:行者123 更新时间:2023-11-30 03:00:36 25 4
gpt4 key购买 nike

我正在制作一个包含 ListView 的应用程序,长按 ListView 中的任何项目将弹出一个 AlertDialog 自定义 View 将为用户提供更多选项。现在,自定义 View 中有一个 TextView,它显示项目,已被用户长按。但是,当我第二次长按 ListView 中的任何项目时出现问题。

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

假设,列表中有四项 -

  1. 英语
  2. 数学
  3. 物理
  4. 地理

假设用户在 Activity 加载后(第一次)长按了“数学”。结果,AlertDialog 弹出,TextView 显示“English”。

现在,在用户关闭之前的对话框并再次长按“地理”之后。结果,AlertDialog 再次弹出,TextView 显示“English”!然而,它应该显示“地理”。

这是我正在使用的代码 -

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

主题.java

package com.Swap.StudyBuddy;

public class Topic {

String Topic;
int _id;

public int get_id() {
return _id;
}
public void set_id(int _id) {
this._id = _id;
}
public Topic() {

}
public Topic(int id, String topic) {
this._id = id;
this.Topic = topic;
}
public Topic(String topic) {
this.Topic = topic;
}

public String getTopic() {
return Topic;
}

public void setTopic(String topic) {
Topic = topic;
}
}

请帮我解决这个问题。提前致谢!!

最佳答案

onCreateDialog创建和缓存对话框。第二次调用 showDialog只是重新显示原始缓存对话框。您需要覆盖 onPrepareDialog如果您想在显示之前修改对话框内容。

关于Android - 关闭 AlertDialog 后清除值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22612225/

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