EditText With ListView.
When we are using EditText in ListView that time we have to implement TextWatcher because you have to add text in your Model/Pojo class after text is entered.
Adapter Class.
I have created one custom baseadapter class for it. In this adapter class i have created one class for TextWatcher because we have to update arraylist after text changed.
package mavya.edittextinlistview; import android.app.Activity; import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.EditText; import java.util.ArrayList; /** * Created on 05/01/17. */public class WorkListAdapter extends BaseAdapter { private Context context; private ArrayList<WorkModel> workModelArrayList; public WorkListAdapter(final Context context, final ArrayList<WorkModel> workModelArrayList) { this.context = context; this.workModelArrayList = workModelArrayList; } @Override public int getCount() { return workModelArrayList.size(); } @Override public WorkModel getItem(int i) { return workModelArrayList.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(final int i, View view, ViewGroup viewGroup) { final ViewHolder viewHolder; if (view == null) { final LayoutInflater inflater = ((Activity) context).getLayoutInflater(); view = inflater.inflate(R.layout.row_worker, viewGroup, false); viewHolder = new ViewHolder(view); view.setTag(viewHolder); } else { viewHolder = (ViewHolder) view.getTag(); } final WorkModel workModel = workModelArrayList.get(i); removeTextChangedListener(viewHolder); viewHolder.edtName.setText(workModel.getWorkerName()); addTextChangedListener(viewHolder, i); return view; } private class ViewHolder { private EditText edtName; private MyTextWatcher myTextWatcher; ViewHolder(final View view) { edtName = (EditText) view.findViewById(R.id.row_worker_edit_name); } public EditText getProductQuantity() { return edtName; } public MyTextWatcher getTextChangedListener() { return myTextWatcher; } public void setTextChangedListener(MyTextWatcher textChangedListener) { this.myTextWatcher = textChangedListener; } } private class MyTextWatcher implements TextWatcher { private int pos; public MyTextWatcher(final int pos) { this.pos = pos; } @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { workModelArrayList.get(pos).setWorkerName(editable.toString()); } } private void removeTextChangedListener(ViewHolder viewHolder) { final MyTextWatcher textChangedListener = viewHolder.getTextChangedListener(); final EditText edtValue = viewHolder.getProductQuantity(); edtValue.removeTextChangedListener(textChangedListener); } private void addTextChangedListener(ViewHolder viewHolder, int position) { final MyTextWatcher textChangedListener = new MyTextWatcher(position); final EditText edtValue = viewHolder.getProductQuantity(); edtValue.addTextChangedListener(textChangedListener); viewHolder.setTextChangedListener(textChangedListener); } }
Model Class :
public class WorkModel { private int workerId; private String workerName; public int getWorkerId() { return workerId; } public void setWorkerId(int workerId) { this.workerId = workerId; } public String getWorkerName() { return workerName; } public void setWorkerName(String workerName) { this.workerName = workerName; } }
Activity Class :
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ListView lv = (ListView) findViewById(R.id.activity_main_lv); final ArrayList<WorkModel> workModelList = new ArrayList<>(); for(int i = 0 ; i < 1000; i++){ workModelList.add(new WorkModel()); } final WorkListAdapter workListAdapter = new WorkListAdapter(this, workModelList); lv.setAdapter(workListAdapter); } }
No comments:
Post a Comment