2015-06-02 36 views
-1

我将数据通过可编辑文本添加到列表视图中,listview是自定义的,每行都包含一个textview和一个复选框,现在我想要一个吐司消息出现,只要我点击每行的复选框就可以了在自定义适配器上执行View.OnClickListener,并通过OnClick方法设置if else条件,以便我的Toast消息可以出现,但问题出在列表视图被填充时,复选框根本无法工作,我认为问题可能在于convertview可能是不正确的膨胀或复选框可能工作在错误的观点,有人可以帮我这个吗?尝试在使用自定义适配器的自定义todolist中实现复选框时遇到问题?

public class todoFragment extends ListFragment{ 

private EditText mToDoField; 
private Button mAdd; 
UsersAdapter mAdapter; 
private TextView todoTextView; 
private CheckBox todoCheckBox; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    getActivity().setTitle(R.string.todo_title); 
} 

public class UsersAdapter extends ArrayAdapter<String> implements View.OnClickListener{ 

    public Context context; 
    public ArrayList<String> values; 

    public UsersAdapter(Context context, ArrayList<String> values) { 
     super(context, 0, values); 

     this.context = context; 
     this.values = values; 
    } 

    @Override 
    public void onClick(View view) { 

     todoCheckBox.setOnClickListener(this); 

     if (todoCheckBox.isChecked()){ 
      Toast.makeText(getContext(), "CheckBox is clicked.", Toast.LENGTH_LONG).show(); 
     } 
     else{ 
      Toast.makeText(getContext(), "CheckBox is not clicked.", Toast.LENGTH_LONG).show(); 
     } 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     convertView = LayoutInflater.from(getContext()).inflate(R.layout.todo_list, parent, false); 

     todoTextView = (TextView) convertView.findViewById(R.id.todo_TextView); 
     todoCheckBox = (CheckBox) convertView.findViewById(R.id.todo_CheckBox); 

     todoTextView.setText(values.get(position)); 

     return convertView; 
    } 
} 



@TargetApi(9) // remember this for isEmpty() 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.fragment_todo, container, false); 

    ArrayList<String> todoList = new ArrayList<String>(); 
    mAdapter = new UsersAdapter(getActivity(), todoList); 
    ListView listViewToDo = (ListView) v.findViewById (android.R.id.list); 
    listViewToDo.setAdapter(mAdapter); 

    mToDoField = (EditText) v.findViewById(R.id.todo_editText); 

    mAdd = (Button)v.findViewById(R.id.add_button); 
    mAdd.setOnClickListener(new View.OnClickListener() { 
     @Override 
      public void onClick(View view) { 

      String toDo = mToDoField.getText().toString().trim(); 

      if (toDo.isEmpty()){ 

      } 

      mAdapter.add(toDo); 

      mToDoField.setText(""); 
     } 
    }); 

    return v; 
} 
+0

当你点击它们时,复选框是否被检查? –

+0

@JayeshElamgodil没有任何事情发生,一个空盒子,当我点击它们时什么也没有发生。 – Athelon

回答

1

我认为以下两件事情应该可以帮助您作出复选框可点击

- >>添加机器人:在todo_list布局

的父ViewGroup中descendantFocusability =“blocksDescendants” - >>添加机器人:点击=“真”在你todo_CheckBox视图定义

一次,就可以开始看到的复选框点击屏幕上,作为user370305正确地提到上的复选框的检查与检测OnCheckedChangeListener而不是OnCliclListener

+0

我只实现了android可点击的true和OnCheckedChangeListener,它的工作,非常感谢你。 – Athelon

+0

很高兴工作! –

1

我会建议你实现setOnCheckedChangeListener适配器类的getView(),你的ListRow定义复选框。

像,(可以是在下面的代码的代码格式错误)

@Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 

     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     convertView = LayoutInflater.from(getContext()).inflate(R.layout.todo_list, parent, false); 

     todoTextView = (TextView) convertView.findViewById(R.id.todo_TextView); 
     todoCheckBox = (CheckBox) convertView.findViewById(R.id.todo_CheckBox); 

     todoTextView.setText(values.get(position)); 

     todoCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)                          { 
      Toast.makeText(getContext(), position+" CheckBox Status: "+isChecked, Toast.LENGTH_LONG).show(); 
     } 
     } 
    );  
     return convertView; 
    } 
} 

而从适配器除去public void onClick(View view)

+0

谢谢你,它的工作 – Athelon

相关问题