0

J'essaie d'obtenir la position listview à partir de la case à cocher de la liste. Cela doit être fait dans l'adaptateur de vue Liste personnalisée. Ceci est le code que j'ai actuellement qui inclut une case à cocher sur check listener pour obtenir la position de ListView malheureusement il ne fonctionne pas et je suis incapable d'obtenir la position de l'écouteur oncheck.Récupère la position de liste à partir de la case à cocher dans l'adaptateur avec la disposition ListView personnalisée

est ici l'adaptateur:

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

Voici le XML pour l'affichage de la liste personnalisée:

<?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"/> 

Et l'activité principale:

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

Répondre

1

Mise à jour votre AdapterOrderProgress classe comme:

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