9
Simple example of EditText
with ListView
:
XML - { main.xml
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="onClick"
android:text="Add" />
<ListView
android:id="@+id/listViewMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:descendantFocusability="beforeDescendants"></ListView>
</LinearLayout>
XML - { item_edittext.xml
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<EditText
android:id="@+id/ItemCaption"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip"
android:singleLine="true"></EditText>
</LinearLayout>
Java - { Main.class
}
public class Main extends AppCompatActivity implements View.OnClickListener {
private Button btnAdd;
private ArrayList arrTemp = new ArrayList();
private ArrayList array = new ArrayList();
MyListAdapter myListAdapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(this);
array.add("teste");
arrTemp.add(array);
myListAdapter = new MyListAdapter();
ListView listView = (ListView) findViewById(R.id.listViewMain);
listView.setAdapter(myListAdapter);
}
public void onClick(View view) {
array.add("teste");
myListAdapter.notifyDataSetChanged();
}
private class MyListAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
if (array != null && array.size() != 0) {
return array.size();
}
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return array.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = Main.this.getLayoutInflater();
convertView = inflater.inflate(R.layout.item_edittext, null);
holder.editText1 = (EditText) convertView.findViewById(R.id.ItemCaption);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ref = position;
return convertView;
}
private class ViewHolder {
EditText editText1;
int ref;
}
}
}
A very simple way would be to work with Listview, I created a Listviewadapter that contains the fields you want and add a new line every click on the button "+".
– Nayron Morais