我通过可编辑的文本将数据添加到listview,listview是自定义的,每行包含textview和一个复选框,现在每当我点击每行的复选框时我想要一个Toast消息出现我正在自定义适配器上实现View.OnClickListener
,并通过OnClick
方法设置if else条件,以便我的toast消息可以出现但这里的问题是在填充listview时复选框根本不起作用,我认为问题可能是转换视图可能错误地膨胀,或者复选框可能正在使用错误的视图,有人可以帮我解决这个问题吗?
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 :(得分:1)
我建议您在Adapter类的Marshal.Copy
中实现using System;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
public unsafe struct MyStruct
{
private const int floatArrayLength = 4;
private fixed float _floatArray[floatArrayLength];
public float[] floatArray
{
get
{
float[] result = new float[floatArrayLength];
fixed (float* ptr = _floatArray)
{
Marshal.Copy((IntPtr)ptr, result, 0, floatArrayLength);
}
return result;
}
set
{
int length = Math.Min(floatArrayLength, value.Length);
fixed (float* ptr = _floatArray)
{
Marshal.Copy(value, 0, (IntPtr)ptr, length);
for (int i = length; i < floatArrayLength; i++)
ptr[i] = 0;
}
}
}
}
class Program
{
static void WriteArray(float[] arr)
{
foreach (float value in arr)
{
Console.Write(value);
Console.Write(" ");
}
Console.WriteLine();
}
static void Main(string[] args)
{
MyStruct myStruct = new MyStruct();
WriteArray(myStruct.floatArray);
myStruct.floatArray = new float[] { 1, 2, 3, 4 };
WriteArray(myStruct.floatArray);
myStruct.floatArray = new float[] { 5, 6 };
WriteArray(myStruct.floatArray);
Console.ReadLine();
}
}
}
,在其中为ListRow定义CheckBox。
喜欢,(可能是下面代码中的代码格式错误)
setOnCheckedChangeListener
从适配器中删除getView()
。
答案 1 :(得分:1)
我认为以下两件事可以帮助您使复选框可点击
- &GT;&GT;在todo_list布局的父ViewGroup中添加android:descendantFocusability =“blocksDescendants”
- &GT;&GT;在todo_CheckBox视图的定义中添加android:clickable =“true”
有一次,您可以开始看到点击屏幕上的复选框,因为用户370305正确地提到了使用OnCheckedChangeListener而不是OnCliclListener检测到复选框的检查