Here is a an example of repeating a cell/layout inside of a layout. The hard part I found was trying to get the text from the Edit Text field since using the id will not work. You can use the position of the cell but since the list can get rearranged due to the soft keyboard popping up. Setting a tag to the edit text field will work better.
I created two different ways to gather the input: using a holder inside the adapter and using a TextWatcher. A TextWatcher will grab the input in real time unlike a holder which will update when the focus changes.
Here is the code:
MainActivity.java (option A)
This one uses a Holder to get the input. This use both position of the cell and set/get tag of the edit field.
public class ExampleList extends Activity{ private AccountAdapter myAdapter; public ArrayList ealist = new ArrayList();//This can be any ArrayList of objects private ListView myList; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listholder); ealist = Banks.getlist(); // populating your list myAdapter = new AccountAdapter(); myList = (ListView) findViewById(R.id.MyList); // grab your listview from your layout myList.setItemsCanFocus(true); myList.setAdapter(myAdapter); //use this update your listview } public class AccountAdapter extends BaseAdapter { // variables used for the class only. private LayoutInflater mInflater; public ArrayList myItems = new ArrayList(); public AccountAdapter() { mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); for (int i = 0; i < ealist.size(); i++) { myItems.add(new ListItem()); } notifyDataSetChanged(); } public int getCount() { return ealist.size(); } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { final ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); Bank account = ealist.get(position); // grabbing individual objects convertView = mInflater.inflate(R.layout.cell, null); // this is your cell holder.caption = (EditText) convertView .findViewById(R.id.payingAmount); holder.caption.setTag(account.savingAccountName); // setting the tag // do rest of your setting like you always do } } else { holder = (ViewHolder) convertView.getTag(); } holder.caption.setId(position); //we need to update adapter once we finish with editing holder.caption.setOnFocusChangeListener(new OnFocusChangeListener() { public void onFocusChange(View v, boolean hasFocus) { if (!hasFocus){ final int position = v.getId(); final EditText Caption = (EditText) v; String val = Caption.getText().toString(); // you have the value here if(val.compareTo("") !=0){ String accountName = ""; if(Caption.getTag() != null){ accountName = Caption.getTag().toString(); // get the tag } /** if you need to find the object associated with the editText for(int i=0; i<ealist.size(); i++){ if(accountName.compareToIgnoreCase(ealist.get(i).savingAccountName) == 0){ ealist.get(i).paymentAmount = val; } } } ((ListItem)myItems.get(position)).caption = val; } } }); return convertView; } } public class ViewHolder { EditText caption; } class ListItem { String caption; } public String getValue(int position){ (((ListItem)myAdapter.myItems.get(position)).caption); } }
MainActivity.java (Option B)
This one uses a TextWatcher and set/get tag
public class ExampleList extends Activity{ private AccountAdapter myAdapter; public ArrayList ealist = new ArrayList();//This can be any ArrayList of objects private ListView myList; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listholder); ealist = Banks.getlist(); // populating your list myAdapter = new AccountAdapter(); myList = (ListView) findViewById(R.id.MyList); // grab your listview from your layout myList.setItemsCanFocus(true); myList.setAdapter(myAdapter); //use this update your listview } public class AccountAdapter extends BaseAdapter { // variables used for the class only. private LayoutInflater mInflater; public AccountAdapter() { mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); notifyDataSetChanged(); } public int getCount() { return ealist.size(); } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { Bank account = ealist.get(position); // grabbing individual objects convertView = mInflater.inflate(R.layout.cell, null); // this is your cell EditText editValue = (EditText) convertView .findViewById(R.id.payingAmount); editValue.setTag(account.savingAccountName); // setting the tag // do rest of your setting like you always do } } return convertView; } } private class MyTextWatcher implements TextWatcher{ private View view; EditText amount; private MyTextWatcher(View view) { this.view = view; } public void beforeTextChanged(CharSequence s, int start, int count, int after) { //do nothing } public void onTextChanged(CharSequence s, int start, int before, int count) { } public void afterTextChanged(Editable s) { amount = (EditText) view.findViewById(R.id.payingAmount); return; } } }
Layouts:
listholder.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:layout_marginTop="5dp" android:orientation="vertical" > <ListView android:id="@+id/MyList" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_centerHorizontal="true" android:layout_weight="6.5" android:divider="#ababab" > </ListView> </LinearLayout>
cell.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="130dp" android:background="@drawable/gradient_cell" android:minHeight="60dp" android:orientation="horizontal" android:padding="10dp" > <EditText android:id="@+id/payingAmount" android:layout_width="140dp" android:layout_height="wrap_content" /> </RelativeLayout>
i don’t see where you utilize your textwatcher in your second example. It’s never called, used or invoked int he code you present. How do you watch for changes in example #2?
Please respond to how you would use the textwatcher…its never being called