-2

我创建了一个带有复选框、edittext 和 textview 的列表视图。
使用 sql server 在 listview 中填充数据。但我无法 onCheckedChangedListener在复选框上使用。这样在单击复选框时,不会保存 textview 和 edittext 的相应数据..

我知道我在适配器类的某个地方做错了..

如何保存数据以及我应该onCheckedChangedListener在适配器类中使用什么逻辑?

pojo 类的代码

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;  
        }
    }

适配器类的代码

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;

 } 

}

MainActivity 类的代码

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;
    }
    }

为了保存 textview 和 EditText 的数据,我应该使用什么逻辑以及应该在 Adapter clss 中的哪个位置编写它..

4

3 回答 3

1

可能不是解决方案,而是建议。
最好不要在“如果”条件下声明您的侦听器。我的意思是,

  • 如果转换视图 == 空
    • 查找视图
  • 别的
    • 获取标签()
  • 其余代码
于 2013-09-07T08:23:25.870 回答
0

是您的复选框事件没有触发吗?

setOnClickListener用于我的Checkbox活动:

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();
    }                   
});
于 2013-09-07T08:26:36.083 回答
0

我知道这是一个老问题,但如果其他人面临同样的问题,请将 Adapter 类替换为以下代码。

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)
            {                              
            }
            });         

}
于 2016-10-06T06:29:19.900 回答