我有一个EditText,Button和ListView。我想在按下Button后将EditText中的文本放入我的ListView中,就像第一项一样。接下来点击第二项......我怎么能这样做?这是我的代码:
zadatSumu = (EditText)findViewById(R.id.zadatSumu);
nastavit = (Button)findViewById(R.id.nastavitBtn);
zoznam = (ListView)findViewById(R.id.listView);
nastavit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
???
}
});
谢谢!
答案 0 :(得分:2)
首先,您的类应扩展ListActivity并添加以下字段:
ArrayList<String> listItems=new ArrayList<String>();
//DEFINING A STRING ADAPTER WHICH WILL HANDLE THE DATA OF THE LISTVIEW
ArrayAdapter<String> adapter;
在onCreate中获取适配器实例:
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
在onClick上插入此代码:
listItems.add(zadatSumu.getText.tostring());
adapter.notifyDataSetChanged();
答案 1 :(得分:0)
为call listview项设置此代码。
ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listItems);
setListAdapter(adapter);
答案 2 :(得分:0)
试试这会帮你老兄
public class Editvalue implements serializable
{
String strEditvalue="";
public String getstrEditvalue() {
return strEditvalue;
}
public void setstrEditvalue(String stredit) {
this.strEditvalue = stredit;
}
}
zadatSumu = (EditText)findViewById(R.id.zadatSumu);
nastavit = (Button)findViewById(R.id.nastavitBtn);
zoznam = (ListView)findViewById(R.id.listView);
Arraylist<Editvalue> arlistdata=new ArrayList<Editvalue>();
nastavit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String streditvalue=zadatSumu.getText().tostring();
Editvalue ed=new Editvalue();
ed.setstrEditvalue(setstrEditvalue);
arlistdata.add(ed);
Activity_Name.this.runOnUiThread(new Runnable() {
@Override
public void run() {
ListAdapter adapter=new ListAdapter(Youractivity.this,arlistdata);
zoznam.setadapter(adapter);
adapter.notifyDataSetChanged();
}
});
}
});
public class ListAdapter extends ArrayAdapter<Editvalue> {
ArrayList<Editvalue>ed;
Context context;
LayoutInflator layoutinflator;
public ListAdapter(Context context,List<Editvalue> edval) {
this.context=context;
this.ed=(ArrayList<Editvalue>)edval;
layoutinflator=LayoutInflator.from(this.context);
}
public int getconut()
{
return ed.size();
}
@Override
public Edvalue getItem(int position)
{
return ed.get(position);
}
@Override
pubic long getItemId(int arg0)
{
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View convertView = layoutinflator.inflate(R.layout.itemlistrow, null);
try{
final Viewholder holder;
holder=new Viewholder();
holder.txtEdtvalue=(TextView)convertView findViewById(R.id.textview1);
convertView.setTag(holder);
Editvalue edv=new Editvalue();
holder.txtEdtvalue.setText(edv.getstrEditvalue);
}
catch(Exception e)
{
e.printstacktrace();
}
return convertView ;
}
class Viewholder{
TextView txtEdtvalue=null;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item_type1_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text goes here" />
</LinearLayout>
一旦完成,请告知我