我正试图从列表视图的复选框中获取列表视图位置。这必须在自定义列表视图适配器中完成。 这是我目前拥有的代码,其中包括检查侦听器上的Checkbox,以获取ListView的位置,不幸的是它无法正常工作,我无法从oncheck监听器获取位置。
这是适配器:
public class AdapterOrderProgress extends ArrayAdapter<DataSetTasks>{
private static class ViewHolder {
TextView TaskName;
CheckBox TaskStart;
CheckBox TaskEnd;
}
public AdapterOrderProgress(Context context, ArrayList<DataSetTasks> tasks) {
super(context, R.layout.list_layout_orderprogress, tasks);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
DataSetTasks tasks = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
final ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.list_layout_orderprogress, parent, false);
viewHolder.TaskName = (TextView) convertView.findViewById(R.id.txtTaskName);
viewHolder.TaskStart = (CheckBox) convertView.findViewById(R.id.cbxStartTask);
viewHolder.TaskEnd = (CheckBox) convertView.findViewById(R.id.cbxEndTask);
viewHolder.TaskStart.setTag(position);
viewHolder.TaskStart.setOnCheckedChangeListener(checkListener);
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
// Populate the data into the template view using the data object
viewHolder.TaskName.setText(tasks.TaskName);
// Return the completed view to render on screen
return convertView;
}
CompoundButton.OnCheckedChangeListener checkListener = new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
int position = buttonView.getTag();
}
};
}
以下是自定义列表视图的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:id="@+id/cbxStartTask" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Task Here"
android:id="@+id/txtTaskName"/>
</LinearLayout>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:id="@+id/cbxEndTask"/>
主要活动:
public void PopulateOrderProgressList(){
Connection con = connectionClass.CONN();
ArrayList<DataSetTasks> arrayofTasks = new ArrayList<>();
// Create the adapter to convert the array to views
AdapterOrderProgress adapter = new AdapterOrderProgress(getContext(), arrayofTasks);
// Attach the adapter to a ListView
listView.setAdapter(adapter);
try {
String sql = "exec sp_Get_Production_Tasks\n@Production_ID = " + ProductionID + ",@Product_ID = " + ProductID;
PreparedStatement statement = con.prepareStatement(sql);
ResultSet rs = statement.executeQuery();
while (rs.next()) {
String TaskName = rs.getString("Task_Name").trim();
DataSetTasks newTask = new DataSetTasks(TaskName);
adapter.add(newTask);
}
}
catch(SQLException e){
Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:1)
将 AdapterOrderProgress 类更新为:
public class AdapterOrderProgress extends ArrayAdapter<DataSetTasks> {
Context _context;
List<DataSetTasks> _listTasks = new ArrayList<DataSetTasks>();
ViewHolder viewHolder;
public AdapterOrderProgress(Context context, ArrayList<DataSetTasks> tasks) {
super(context, R.layout.list_layout_orderprogress, tasks);
this._context = context;
this._listTasks = tasks;
}
@Override
public Contact getItem(int position) {
return _listTasks.get(position);
}
@Override
public int getCount() {
return _listTasks.size();
}
@Override
public long getItemId(int pos) {
return pos;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
DataSetTasks tasks = _listTasks(position);
View view = convertView;
// Check if an existing view is being reused, otherwise inflate the view
if(view == null)
{
LayoutInflater mLayoutInflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = mLayoutInflater.inflate(R.layout.list_layout_orderprogress, null);
}
else
{
view = convertView;
}
// View Holder
viewHolder = new ViewHolder();
viewHolder.TaskName = (TextView) view.findViewById(R.id.txtTaskName);
viewHolder.TaskStart = (CheckBox) view.findViewById(R.id.cbxStartTask);
viewHolder.TaskEnd = (CheckBox) view.findViewById(R.id.cbxEndTask);
// Populate the data into the template view using the data object
viewHolder.TaskName.setText(tasks.TaskName);
// CheckBox click listener
viewHolder.TaskStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
CheckBox checkBox = (CheckBox) view;
if (checkBox.isChecked())
{
................
}
else
{
................
}
}
});
view.setTag(tasks);
// Return the completed view to render on screen
return view;
}
static class ViewHolder {
TextView TaskName;
CheckBox TaskStart;
CheckBox TaskEnd;
}
}