Get Value from EditText placed in List View in android
I dont know how to get the the Edit text's value pla开发者_C百科ce in a list view.I filled the list view with data using the simpleadapter.
Will any one give sample code for getiing the edittext value from a list
for (int i = 0; i < ListSortOrder.getAdapter().getCount(); i++) {
HashMap result = (HashMap)ListSortOrder.getItemAtPosition(i);
**View vListSortOrder;
vListSortOrder=ListSortOrder.getChildAt(i);
TextView Sort_DeliveryOrder=(TextView)vListSortOrder.findViewById(R.id.et_Sort_Order);**
initialSortOrder.put("DeliveryOrder",Sort_DeliveryOrder.getText().toString());
dbAdapter = new DatabaseAdapter(this);
dbAdapter.open();
dbAdapter.BeginTransaction();
dbAdapter.UpdateRecord("tblDelivery", initialSortOrder, "PKDelivery" + "=" + result.get("Sort_PKDelivery").toString(), null);
}
Maybe you already have the answer, but I will put my solution here cause I see a lot of questions without good answer. I have a custom ArrayAdapter with the fowling code that updates the source list "telefones".
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.telefone_form, parent, false);
tipoEditText = (EditText) view.findViewById(R.id.telefone_form_tipo);
telefoneEditText = (EditText) view.findViewById(R.id.telefone_form_telefone);
Telefone telefone = telefones.get(position);
tipoEditText.setText(telefone.getTipo());
telefoneEditText.setText(telefone.getTelefone());
final int i = position;
tipoEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
telefones.get(i).setTipo(s.toString());
}
});
telefoneEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
telefones.get(i).setTelefone(s.toString());
}
});
In the Activity that sets the Adapter I get all changes with the code:
for (int i = 0; i < telefoneListView.getAdapter().getCount(); i++) {
View viewTelefone = telefoneListView.getChildAt(i);
if (viewTelefone.findViewById(R.id.telefone_form_tipo) != null) {
EditText tipoEditText = (EditText) viewTelefone.findViewById(R.id.telefone_form_tipo);
EditText telefoneEditText = (EditText) viewTelefone.findViewById(R.id.telefone_form_telefone);
Log.d(TAG, tipoEditText.getText().toString() + ":" + telefoneEditText.getText().toString());
}
}
Regards, Marcello
I faced same problem but solved it. So I am sharing what I done:
Logic: As When user fills value in EditText of listview, values with it index must be stored for future use. So,in its TouchListener create instance of ArrayList and filled it with empty value. Now when user changes its value new value is updated in last position(index stored in TouchListener) of ArrayList.
- Problem: It also fills empty values as TouchListener called every time when user changes value in EditText.So, when ever this ArrayList used it had to remove the empty values.
*Solution
**In getView set tag (position) in EditText so that we can identify it on its events. //On TouchListner myEditText.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { HashMap<String, String> hashMap=new HashMap<String,String>(); hashMap.put("", ""); MyArrayList.add(hashMap); if(MyArrayList.size()>0) { index=MyArrayList.size()-1; } } } return false; } }); myEditText.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub System.out.println(myEditText.getText().toString()); String quanString=String.valueOf(myEditText.getTag()); HashMap<String, String> hashMap=new HashMap<String, String>(); hashMap.put(quanString,value); MyArrayList.set(index,hashMap); } }); return listRowItemView; } //Let Suppose values had to fetch on button click-- findViewById(R.id.submit).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { System.out.println("MyArrayList....value before removing====>"+MyArrayList); // TODO Auto-generated method stub for(int i=0;i<MyArrayList.size();i++) { HashMap<String, String> hashMap=new HashMap<String, String>(); hashMap= MyArrayList.get(i); for (Entry<String, String> entry : hashMap.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); if(key.length()<=0 & value.length() <=0) { MyArrayList.remove(i); System.out.println("At index"+i+" key value was empty so it is removes"); System.out.println("MyArrayList..value after removeing empty values====>"+MyArrayList); } } } } });
package com.example.user.listview;
import android.content.ContentValues;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
private ListView lv1;
private EditText Et1;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv1 = (ListView) findViewById(R.id.lv);
btn = (Button) findViewById(R.id.btn);
Et1 = (EditText) findViewById(R.id.ETName);
final ArrayList<String> List = new ArrayList<>();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String item = Et1.getText().toString();
List.add(item);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_dropdown_item_1line,List);
lv1.setAdapter(adapter);
}
});
}
}
精彩评论