动态地将数据提供给列表

时间:2013-02-07 10:11:11

标签: android list button

我是新的android im desingnig一个应用程序描述是... 我保留了一个编辑文本和一个按钮,当我点击按钮时我必须将该值存储在列表中...我试过这样 我没有得到错误的地方......

  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;

}

}

1 个答案:

答案 0 :(得分:1)

请先see this然后实施您的代码,以便于实施。

修改:

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();



        }
    });

希望你明白了。