0

所以我有一个带有按钮的用户界面,单击时会显示弹出用户输入。无论输入什么,都应添加到数组列表中并显示在屏幕上的列表中。以下是我的代码。我尝试使用 notifyDataSetChanged() 更新视图但没有结果。

public class reminderList extends ListActivity
{
ArrayList values = new ArrayList(20);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    values.add(0,"Test");
    values.add(0,"test2");

    //final ListView listView = (ListView) findViewById(R.id.list);
    setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, values));

    final Button addButton = (Button) findViewById(R.id.addTask);

    addButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            alertPop();

        }
    });
}

private void alertPop() {
    AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setTitle("Enter new task");
    alert.setMessage("Input");

    // Set an EditText view to get user input
    final EditText input = new EditText(this);
    alert.setView(input);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            Editable uin = input.getText();
            // Do something with value!
            values.add(0,uin);

            //((ArrayAdapter<Object>) list.getAdapter()).notifyDataSetChanged();
        }
    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // Canceled.
        }
    });

    alert.show();
  }
}

任何帮助是极大的赞赏。

4

1 回答 1

1

试试这个,

在设置适配器之后放置 notifyDataSetChanged()....

例如:

ArrayAdapter<String> adpt = new ArrayAdapter(this, android.R.layout.simple_list_item_1, values)

setListAdapter(adpt);

notifyDataSetChanged(adpt);

于 2012-06-22T03:02:02.553 回答