I have a ListView that contains two TextViews and one EditText in each row. I have searched and searched and cannot figure out how to get the text that the user enters into the EditText. My most recent attempt implements a TextWatcher.
public void addIngredientToMeal(String ingredientName) {
ingredient_list = (ListView) findViewById(R.id.ingredients_in_meal_listView);
int numberOfIngredients = ingredient_list.getChildCount();
ListAdapter adapter = new ListAdapter();
adapter.name_array = new String[numberOfIngredients+1];
for (int i = 0; i < numberOfIngredients; i++)
adapter.name_array[i] = (String) ingredient_list.getItemAtPosition(i);
adapter.name_array[numberOfIngredients] = ingredientName;
ingredient_list.setItemsCanFocus(true);
ingredient_list.setAdapter(adapter);
}
private class ListAdapter extends BaseAdapter{
public String[] name_array, qty_array;
@Override
public int getCount() {
// TODO Auto-generated method stub
if(name_array != null && name_array.length != 0){
return name_array.length;
}
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return name_array[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//ViewHolder holder = null;
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = NewMeal.this.getLayoutInflater();
convertView = inflater.inflate(R.layout.ingredients_in_meal_layout, null);
holder.textView1 = (TextView) convertView.findViewById(R.id.ingredient_name_in_new_meal_textView);
holder.editText1 = (EditText) convertView.findViewById(R.id.ingredient_qty_in_new_meal_editText);
holder.textView2 = (TextView) convertView.findViewById(R.id.ingredient_unit_in_new_meal_textView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ref = position;
holder.textView1.setText(name_array[position]);
holder.textView2.setText(databaseAdapter.dbQuery(name_array[position]));
holder.editText1.setText(qty_array[position]);
holder.editText1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
qty_array[holder.ref] = arg0.toString();
}
});
return convertView;
}
private class ViewHolder {
TextView textView1, textView2;
EditText editText1;
int ref;
}
}
}
For those who may have a similar issue, I solved the problem I was having.
The problem: Need to get values entered in multiple editTexts that are contained within a listView. Using listviewname.getItemAtPosition(integer) only gets the value of the first editText. Moreover, I had multiple views contained in each row of the listview, so getItemAtPosition only returned the value of the first view in the first row of the listView.
The solution:
Here's some sample code that could be implemented on a button press:
ListView lvName = (ListView) findViewById(R.id.listview1);
View v;
EditText et;
int listLength = lv.getChildCount();
String[] valueOfEditText = new String(listLength);
for (int i = 0; i < listLength; i++)
{
v = lvName.getChildAt(i);
et = (EditText) v.findViewById(R.id.edittext1);
valueOfEditText[i] = et.getText().toString();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With