J'ai Checkbox et EditText et un Textview dans un listView. Il obtient une valeur pour la vue texte à partir d'une liste. La case à cocher sera cochée dynamiquement. De la même manière, EditText peut également être entré dynamiquement. Maintenant, mon problème est, quand je fais défiler la liste (de haut en bas) après avoir saisi le texte dans le texte d'édition, je ne pouvais pas obtenir la valeur saisie. Je coche la case aussi comme ça. Mais en utilisant la position, je l'ai réglé correctement. Je ne pouvais pas savoir comment définir correctement la valeur EditText sur la liste. Aidez-moi, s'il vous plaît. Voici mon code:
main.xml: (xml principal pour le lancement)
<?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:orientation="vertical" >
<ListView
Android:id="@+id/my_list"
Android:layout_width="fill_parent"
Android:layout_height="250px" />
<Button
Android:text="Save"
Android:id="@+id/btnSave"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"/>
</LinearLayout>
row.xml: (ligne ListView)
<?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="wrap_content"
Android:orientation="horizontal" >
<TextView
Android:id="@+id/label"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text="@+id/label"
Android:textSize="30sp"/>
<CheckBox
Android:id="@+id/check"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"/>
<EditText
Android:text=""
Android:id="@+id/txtAddress"
Android:layout_width="150px"
Android:layout_height="wrap_content"/>
</LinearLayout>
Model.Java: (c'est la classe POJO)
package com.checkboxlistview;
public class Model {
private String name;
private boolean selected;
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public void setName(String name) {
this.name = name;
}
public Model(String name) {
this.name = name;
}
public String getName() {
return name;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
MyAdapter.Java: (Ceci est utilisé pour maintenir la vue dans la vue liste à l'aide du convertisseur et du titulaire)
package com.checkboxlistview;
import Java.util.List;
import Android.app.Activity;
import Android.text.Editable;
import Android.text.TextWatcher;
import Android.view.LayoutInflater;
import Android.view.View;
import Android.view.ViewGroup;
import Android.widget.ArrayAdapter;
import Android.widget.CheckBox;
import Android.widget.CompoundButton;
import Android.widget.EditText;
import Android.widget.TextView;
public class MyAdapter extends ArrayAdapter<Model> implements TextWatcher {
private final List<Model> list;
private final Activity context;
int listPosititon;
public MyAdapter(Activity context, List<Model> list) {
super(context, R.layout.row, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
protected EditText address;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
listPosititon = position;
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) convertView
.findViewById(R.id.check);
viewHolder.address = (EditText) convertView
.findViewById(R.id.txtAddress);
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
//Here we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(
buttonView.isChecked());
// Set the value of checkbox to maintain its state.
}
});
viewHolder.address.addTextChangedListener(this);
convertView.setTag(viewHolder);
convertView.setTag(R.id.label, viewHolder.text);
convertView.setTag(R.id.check, viewHolder.checkbox);
convertView.setTag(R.id.txtAddress, viewHolder.address);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkbox.setTag(position); // This line is important.
viewHolder.text.setText(list.get(position).getName());
viewHolder.checkbox.setChecked(list.get(position).isSelected());
if (list.get(position).getAddress() != null) {
viewHolder.address.setText(list.get(position).getAddress() + "");
} else {
viewHolder.address.setText("");
}
return convertView;
}
@Override
public void afterTextChanged(Editable s) {
list.get(listPosititon).setAddress(s.toString());
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
}
MainActivity.Java (Ceci est l'activité):
package com.checkboxlistview;
import Java.util.ArrayList;
import Java.util.List;
import Android.app.Activity;
import Android.os.Bundle;
import Android.view.View;
import Android.widget.ArrayAdapter;
import Android.widget.Button;
import Android.widget.ListView;
import Android.widget.Toast;
public class MainActivity extends Activity {
ListView listView;
Button btnSave;
ArrayAdapter<Model> adapter;
List<Model> list = new ArrayList<Model>();
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.my_list);
btnSave = (Button)findViewById(R.id.btnSave);
adapter = new MyAdapter(this,getModel());
listView.setAdapter(adapter);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < list.size(); i++) {
Toast.makeText(getBaseContext(), "Name : "+list.get(i).getName() +" Selected: "+list.get(i).isSelected(), Toast.LENGTH_SHORT).show();
}
}
});
}
private List<Model> getModel() {
list.add(new Model("Linux"));
list.add(new Model("Windows7"));
list.add(new Model("Suse"));
list.add(new Model("Eclipse"));
list.add(new Model("Ubuntu"));
list.add(new Model("Solaris"));
list.add(new Model("Android"));
list.add(new Model("iPhone"));
list.add(new Model("Java"));
list.add(new Model(".Net"));
list.add(new Model("PHP"));
return list;
}
}
Il n'y a pas d'erreur dans le code. Ca marche bien. Je pouvais conserver la position de la case à cocher et afficher à la même position même si je faisais défiler de haut en bas. Mais je ne pouvais pas obtenir et définir correctement la valeur EditText. S'il vous plaît, aidez-moi ... Merci d'avance.
vous pouvez y parvenir en utilisant la vue liste personnalisée.
trouver l'exemple de listview avec edittext est ici
Il suffit de garder viewHolder.address.setTag (position) et cela fonctionne parfaitement.
Classe d'adaptateur:
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;
}
}