gpt4 book ai didi

android - 动态地将数据提供给列表

转载 作者:行者123 更新时间:2023-11-30 03:47:24 24 4
gpt4 key购买 nike

我是 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;
}
}

setter/getter ..

 public class Getters {
public Getters(String name) {

this.name = name;
}

String name;

public String getName() {

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

}

最佳答案

请先see this然后实现您的代码,使其易于实现。

编辑:

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

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

编辑

只需编写下面的代码即可在 ListView 中添加事件

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



}
});

希望你明白了。

关于android - 动态地将数据提供给列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14748488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com