Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

listview with edittext ,checkbox and textView in android

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..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...