我尝试了很多不同的方法,但不使用我的代码。我知道如何制作一个警报对话框,但每当我把它放在我的MainActivity中时,我都会遇到一些我似乎无法解决的错误。也许我把它放在错误的地方......或者错别字?我不知道,这是我的MainActivity,代码运行完美。我如何在那里放置一个Alert Dialog,当用户点击yes时删除一个ListView项?谢谢。
public class MainActivity extends Activity {
EditText et;
ListView lv;
ArrayAdapter<String> adapter;
Button btn;
ArrayList<String> list = new ArrayList<String>();
final Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.editText);
lv = (ListView) findViewById(R.id.listView);
btn = (Button) findViewById(R.id.button);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
lv.setAdapter(adapter);
LoadPreferences();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String task = et.getText().toString();
adapter.add(task);
adapter.notifyDataSetChanged();
SavePreferences("LISTS", task);
}
});}
protected void SavePreferences(String key, String value) {
// TODO Auto-generated method stub
SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = data.edit();
editor.putString(key, value);
editor.commit();
}
protected void LoadPreferences(){
SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
String dataSet = data.getString("LISTS", " ");
adapter.add(dataSet);
adapter.notifyDataSetChanged();
}
}
以下是我尝试使用的警报对话框:
@Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Hello My Name is");
alertDialog.setMessage("" + mNameList.get(position));
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
}
});
alertDialog.setButton2("Remove", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Removed from list", Toast.LENGTH_SHORT).show();
}
});
alertDialog.show();
}
我正试着把它放在正确的位置:
adapter.add(task);
adapter.notifyDataSetChanged();
SavePreferences("LISTS", task);
}
});}
它应该去哪里?
答案 0 :(得分:0)
有了这个你想要:
使用字符串的新内容更新ListView。
Array.prototype.shuffle = function(local){
var a = this;
var newArray = typeof local === "boolean" && local ? this : [];
for (var i = 0, newIdx, curr, next; i < a.length; i++){
newIdx = Math.floor(Math.random()*i);
curr = a[i];
next = a[newIdx];
newArray[i] = next;
newArray[newIdx] = curr;
}
return newArray;
};
此示例用于不同的情况,它来自我最近制作的应用程序 - 尽管略有减少,因此您只能看到您需要的内容。使用警报对话框很容易适应。