带有EditText和Action Button的Android Listview

时间:2013-12-26 02:07:39

标签: android android-listview android-edittext

我有一个Listview,我希望EditText中的文本为
单击ActionButton时添加到ListView。

我尝试以这种方式管理它,但它不起作用:

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class Activity3 extends Activity
{
    ListView HeldenList;
    EditText FigurVaule;
    Button save;

    List<String> HeldenListString = new ArrayList<String>();

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act3);

    HeldenListString.add("Hello World");

    HeldenList = (ListView) findViewById(R.id.HeldenListe);
    FigurVaule = (EditText) findViewById(R.id.editText1);
    save = (Button) findViewById(R.menu.save);

    HeldenList.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, HeldenListString));
    save.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View currentView) {
            HeldenListString.add(new String(FigurVaule.getText().toString()));
        }
    });
  }


  public boolean onCreateOptionsMenu(Menu paramMenu) {
    new MenuInflater(this).inflate(R.menu.save, paramMenu);
    return super.onCreateOptionsMenu(paramMenu);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      switch(item.getItemId()) {
      case R.menu.save:
          Intent intent = new Intent(this, Activity2.class);
          this.startActivity(intent);



          break;
      default:
          Intent intent1 = new Intent(this, Activity2.class);
          this.startActivity(intent1);
      }

      return true;
  }
}

感谢您的帮助,抱歉我的英语不好(我来自德国)。

2 个答案:

答案 0 :(得分:0)

您必须将其添加到ArrayAdapter。这样的事情应该有效:

ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, HeldenListString);
HeldenList.setAdapter(adapter);
save.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View currentView) {
        adapter.add(new String(FigurVaule.getText().toString()));
    }
});

答案 1 :(得分:0)

ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, HeldenListString);
HeldenList.setAdapter(adapter);
save.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View currentView) {
        adapter.add(new String(FigurVaule.getText().toString()));
        adapter.notifyDataSetChanged(); 
    }
});

我希望对你有用。