1

我是 android 的新手,我正在设计一个应用程序描述......当我点击按钮时,我保留了一个编辑文本和一个按钮……

  public class Dynamic extends Activity {


 ArrayList<Getters> x=new ArrayList<Getters>();



@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b=(Button)findViewById(R.id.nameok);
    final EditText ed=(EditText)findViewById(R.id.name);
    b.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            // TODO Auto-generated method stub
            String name=ed.getText().toString();
            Getters y=new Getters(name);
            x.add(y);
            ListView lv= (ListView)findViewById(R.id.listView1);
            Demo demo=new Demo();
        lv.setAdapter(demo);

        }
    });




}
public class Demo extends BaseAdapter
{

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return x.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View v = convertView;

        if (v == null) {
            LayoutInflater vi = LayoutInflater.from(parent.getContext());
            v = vi.inflate(R.layout.forlist, null);
        }
        //View v=LayoutInflater.from(getApplicationContext()).inflate(R.layout.forlist, null);
        TextView listname=(TextView)findViewById(R.id.listname);
        listname.setText(x.get(position).name);

        return v;
    }

}




public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
   }

吸气剂..

 public class Getters {
public Getters(String name) {

    this.name = name;
}

String name;

public String getName() {

     Log.v("name---",""+name);
    return name;

}

}

4

1 回答 1

1

请先看看这个,然后实现你的代码,这样它就很容易实现。

编辑 :

http://www.framentos.com/en/android-tutorial/2012/07/16/listview-in-android-using-custom-listadapter-and-viewcache/

希望您能了解如何动态管理列表数据。

编辑

只需编写以下代码即可在列表视图中添加事件

b.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            // TODO Auto-generated method stub
            String name=ed.getText().toString();
            your List_view_obj.add(name);
            //call adapter class and pass that string                
           your List_view_obj.setAdapter(your adapter's obj);

           your_adapter_obj.notifyDataSetChanged();



        }
    });

希望你明白了。

于 2013-02-07T10:24:27.793 回答