我正在制作一个简单的规划应用程序,只有一个xml和一个活动。这是MainActivity,基本上我使用editText输入,将其转换为带有onClick按钮的listView项,然后保存它。唯一的问题是我的警告对话框,当你想删除一个项目有错误时 - 它不会识别单词" position"。我觉得这只是一些愚蠢的错误..?谢谢。
package com.kass.planner2;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
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);
}
});
// set ListView item listener
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view; final int position, long id) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Confirm Delete");
alertDialogBuilder.setMessage("Sure you want to delete?");
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
adapter.remove(adapter.getItem(position));
}
});
alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});}
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();
}
}`
答案 0 :(得分:0)
尝试这种方式:
ArrayAdapter<String> adapter = (ArrayAdapter<String>)getListView().getAdapter();
adapter.remove(adapter.getItem(info.position));
adapter.notifyDataSetChanged();
答案 1 :(得分:0)
position
变量如果要在那里访问,则需要final
。
按如下方式进行:
final int p = position; // just before setPositiveButton
alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
adapter.remove(adapter.getItem(p));
}
});
答案 2 :(得分:0)
可能是,这需要.... adapter.notifyDataSetChanged();当删除..
alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
adapter.remove(adapter.getItem(position));
adapter.notifyDataSetChanged();
}
});