0
我想從列表視圖中的複選框獲得列表視圖的位置。這必須在自定義列表視圖適配器中完成。 這是我目前擁有的代碼,其中包括檢查監聽器上的複選框,以獲取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\[email protected]_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();
}
}