I have Checkbox and EditText and a Textview in a listView. It gets value for the text view from a list. Checkbox will be checked dynamically. In the same way EditText also c
Just keep viewHolder.address.setTag(position) and it works perfect cheers.
Adapter Class:
package com.qzick.adapter;
import java.util.ArrayList;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import com.example.qzick.R;
import com.qzick.model.Get_All_Class_Model;
public class Get_Class_Adapter extends BaseAdapter {
protected ArrayList<Get_All_Class_Model> get_class_details;
LayoutInflater inflater;
Context context;
private int x = 1;
public Get_Class_Adapter(Context context,
ArrayList<Get_All_Class_Model> get_class_details) {
this.get_class_details = get_class_details;
this.inflater = LayoutInflater.from(context);
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return get_class_details.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return get_class_details.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = this.inflater.inflate(
R.layout.activity_adapter_class_ll, parent, false);
holder.textclass = (TextView) convertView
.findViewById(R.id.text_class_ll);
holder.txtid = (TextView) convertView.findViewById(R.id.text_id_ll);
holder.checkclass = (CheckBox) convertView
.findViewById(R.id.check_class_LL);
holder.edtsection = (EditText) convertView
.findViewById(R.id.edttxt_addsection_ll);
holder.checkclass
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
get_class_details.get(getPosition).setChecked(
buttonView.isChecked());
notifyDataSetChanged();
}
});
convertView.setTag(holder);
convertView.setTag(R.id.check_class_LL, holder.checkclass);
convertView.setTag(R.id.edttxt_addsection_ll, holder.edtsection);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkclass.setTag(position);
holder.edtsection.setTag(position);
holder.edtsection.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
int pos = (Integer) holder.edtsection.getTag();
get_class_details.get(pos).setEdtsections(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
holder.txtid.setText(get_class_details.get(position).getId());
holder.textclass.setText(get_class_details.get(position).getText());
holder.edtsection.setText(get_class_details.get(position)
.getEdtsections());
holder.textclass.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
x++;
if (x % 2 == 0) {
holder.checkclass.setChecked(false);
} else {
holder.checkclass.setChecked(true);
}
}
});
holder.checkclass.setChecked(get_class_details.get(position)
.isChecked());
return convertView;
}
private class ViewHolder {
TextView textclass, txtid;`enter code here`
CheckBox checkclass;
EditText edtsection;
}
}
Easy and beautiful solution to handle EditText with listView: (Does not require holder or RecycleView or anything else)
Brief explaination:
1) In getView method when you inflate the view, apply the myTextWatcher the editText. Pass this EditText to the myTextWatcher()
2) Inside getView Method find that EditText and set position as editText.setTag [Each time. not only when the view was inflated.]
3) Define MyTextWatcher. It should have reference to EditText on which it is applied.
4) myTextWatcher.onTextChanged() will read the tag set to the editText and do the required work
Modify your getView() method of Adapter class:
@Override
public View getView(int position, View convertView, final ViewGroup parent) {
if(convertView==null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.single_row_layout,parent,false);
EditText et = convertView.findViewById(R.id.idEditText);
et.addTextChangedListener(new MyTextWatcher(et));
}
//This is again required to find reference to EditText... so that 'position' can be applied on to it as 'tag' EACH time.
EditText editText = (EditText) convertView.findViewById(R.id.idEditText);;
//This tag will be used inside onTextChanged()
editText.setTag(position);
}
Define your MyTextWatcher class as:
private class MyTextWatcher implements TextWatcher{
//int position;
EditText et;
public MyTextWatcher(EditText editText){
this.et = editText;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(et.getTag()!=null){
// This is required to ensure EditText is edited by user and not through program
if(et.hasFocus()){
int position = (int)et.getTag();
String newText = et.getText()+"";
//Implement your actions here........
//you can get require things/ views from listView.getChildAt(position)..
}
}
}
@Override
public void afterTextChanged(Editable s) {
}
}
you can achieve this using the custom list view.
find the example of listview with edittext is here