我是android的新手,帮助代码如何使用BaseAdapter通过单击Button将EditText值添加到ListView。 当我在EditText中输入值并单击添加按钮时,值必须放在ListViw中(ListView必须只有BaseAdapter)
ListDataActivity.java
public class ListDataActivity {
String myData;
public String getMyData()
{
return myData;
}
public void setMyData(String myDataa)
{
myData=myDataa;
}}
BaseAdapterActivity.java
public class MyBaseAdapterActivity extends BaseAdapter {
ArrayList<ListDataActivity> list =new ArrayList<ListDataActivity>();
LayoutInflater inflater;
Context context;
public MyBaseAdapterActivity(Context context,ArrayList<ListDataActivity>list)
{
this.list=list;
this.context=context;
inflater = LayoutInflater.from(this.context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder myViewHolder;
if (convertView==null)
{
convertView=inflater.inflate(R.layout.activity_addlist,parent,false);
myViewHolder=new MyViewHolder(convertView);
convertView.setTag(myViewHolder);
}else {
myViewHolder =(MyViewHolder)convertView.getTag();
}
ListDataActivity listDataActivity = getItemId(position);
myViewHolder.mydata.setText(listDataActivity.getMyData());
return convertView;
}
private class MyViewHolder{
String mydata;
public MyViewHolder(View item)
{
mydata=(ListView)item.findViewById(R.id.myData);
}
}}
AddListActivity.java
public class AddlistActivity extends Activity {
ListView lstName;
Button btnAdd;
EditText edtName;
String myData;
Context context=AddlistActivity.this;
ArrayList<ListDataActivity>list=new ArrayList<ListDataActivity>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addlist);
lstName=(ListView)findViewById(R.id.lst_name);
btnAdd=(Button)findViewById(R.id.btn_add);
edtName=(EditText)findViewById(R.id.edt_name);
getDataInList();
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myData=edtName.getText().toString().trim();
}
});
lstName.setAdapter(new MyBaseAdapterActivity(context,list));
}
private void getDataInList()
{
for (int i = 0 ; i < myData.length();i++)
{
ListDataActivity listDataActivity = new ListDataActivity();
listDataActivity.setMyData(myData[i]);
list.add(listDataActivity);
}
}}
答案 0 :(得分:0)
要在点击按钮时将值从编辑文本存储到列表视图,请在按钮上单击以下步骤:
从editText获取文本并存储在String中。
String myText=editText.getText().toString().trim();
然后将其保存到您的arraylist(您将用于通过BaseAdapter将数据提供到列表视图的数据结构)。
ArrayList<String> stockList = new ArrayList<String>();
stockList.add(myText);
感谢