问题
i have created a listview with checkbox,edittext and textview.
data is populated in listview with sql server. but i am not being able to use onCheckedChangedListener
on checkbox. so that on clicking the checkbox corresponding data of textview and edittext is not being saved..
i know i am doing mistake somewhere in my adapter class..
How to save data and what logic should i use in onCheckedChangedListener
in my adapter class?
code for pojo class
public class Model {
public String name="";
public boolean selected=false;
public String score="";
public Model(String name) {
this.name = name;
selected = false;
score="";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
}
code for Adapter class
public class MyAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
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 scores;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = null;
if (convertView == null)
{
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.row, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.scores=(EditText) view.findViewById(R.id.txtAddress);
viewHolder.scores.addTextChangedListener(new TextWatcher()
{
public void onTextChanged(CharSequence s, int start, int before, int count) {
Model element=(Model)viewHolder.scores.getTag();
element.setScore(s.toString());
}
public void beforeTextChanged(CharSequence s, int start, int count,int after)
{
}
public void afterTextChanged(Editable s)
{
}
});
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
Model element = (Model) viewHolder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
}
});
viewHolder.checkbox.setTag(list.get(position));
viewHolder.scores.setTag(list.get(position));
view.setTag(viewHolder);
}
else
{
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
((ViewHolder) view.getTag()).scores.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.scores.setText(list.get(position).getScore());
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}
}
code for MainActivity class
public class MainActivity extends Activity {
ListView listView;
Button btnSave;
Connection connect;
ArrayAdapter<Model> adapter;
MyConnection mycon;
List<Model> list = new ArrayList<Model>();
List<String>data=new ArrayList<String>();
List<String>data2=new ArrayList<String>();
StringBuilder sb;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mycon=new MyConnection(getApplicationContext());
listView = (ListView) findViewById(R.id.my_list);
btnSave = (Button)findViewById(R.id.btnSave);
sb=new StringBuilder();
adapter = new MyAdapter(this,getModel());
listView.setAdapter(adapter);
if(list.isEmpty()){
Toast.makeText(getApplicationContext(), "kuldeep", Toast.LENGTH_LONG).show();
}
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()+"address: "+list.get(i).getScore().toString(), Toast.LENGTH_SHORT).show();
}
}
});
}
private List<Model> getModel() {
list.clear();
try{
Statement smt=mycon.connection().createStatement();
ResultSet rs=smt.executeQuery("WORKINGTYPE");
while(rs.next()){
list.add(new Model(rs.getString("FIELD_NAME")));
}
}catch(Exception e){
e.printStackTrace();
}
/* list.add(new Model("kuldeep"));
list.add(new Model("sandeep"));
list.add(new Model("ravi"));
list.add(new Model("pradeep"));
list.add(new Model("praveena"));
list.add(new Model("ruchi"));
list.add(new Model("vikas"));
list.add(new Model("sonu"));
list.add(new Model("ashu"));
*/
return list;
}
}
for saving data of textview and EditText what logic should i use and where in Adapter clss i should write it..
回答1:
May not be a solution, but a suggestion.
Prefer not to declare your Listeners inside an 'if' condition. What I meant is,
- IF convertview == null
- find views
- ELSE
- getTag()
- Rest of the codes
回答2:
Is it that your checkbox event is not firing?
I use setOnClickListener
for my Checkbox
events:
CheckBox checkbox = (CheckBox)view.findViewById(R.id.item_checkbox);
checkbox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Mark model as selected/not selected
if (model.isSelected())
model.markAsNotSelected();
else
model.markAsSelected();
}
});
回答3:
I know it's an old question, but if anyone else is facing same problem, replace the Adapter class with the following code.
public class MyAdapter extends ArrayAdapter<Model> implements TextWatcher{
private final List<Model> list;
private final Context context;
public MyAdapter(Context 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 scores;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder viewHolder = null;
if (convertView == null){
LayoutInflater inflator = LayoutInflater.from(context);
convertView = inflator.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.scores=(EditText) view.findViewById(R.id.txtAddress);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
viewHolder.checkbox.setTag(list.get(position));
viewHolder.scores.setTag(list.get(position));
convertView.setTag(viewHolder);
}
else
{
convertView = (ViewHolder) convertView.getTag();
}
viewHolder.scores.addTextChangedListener(this)
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
Model element = (Model) viewHolder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
}
});
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.scores.setText(list.get(position).getScore());
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Model element=(Model)viewHolder.scores.getTag();
element.setScore(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after)
{
}
@Override
public void afterTextChanged(Editable s)
{
}
});
}
来源:https://stackoverflow.com/questions/18671222/listview-with-edittext-checkbox-and-textview-in-android