0

我有一個自定義的適配器,它呈現兩(2)個複選框,一張圖片和客戶端的名稱。適配器所需的所有信息都是從包含Client類的ArratList中獲取的。ArrayAdapter中的ViewHolder問題 - Android

爲了處理採購訂單,每一行都需要爲客戶檢查(選擇)兩個複選框,以防特定客戶有一個複選框取消選中,而另一個複選框不復存在,會將標誌提升爲MISMATCH 。爲了生成有效的訂單,兩個複選框都需要檢查。

我們正在實現一個驗證按鈕,它將查找適配器中的任何不匹配,然後突出顯示不匹配。

EDITION:在按下verificationBtn後,我可以確定是否有任何行在複選框上不匹配,例如checkbox1被選中,checkbox2不被選中。這將標記行不匹配。我正在使用基於clientList的複選框的位置,它是List clientList的數組列表。

問題:如何獲得viewHolder的順序與clientList位置進行比較的位置?有什麼辦法可以強制viewHolder存儲位置並將其取回並與cli.getClient_number()進行比較?

方法1:

viewHolder.clientName.setBackgroundColor((Interger.parseInt(cli.getClient_number())) == position ? Color.GREEN : Color.TRANSPARENT); 

方法2

viewHolder.clientName.setBackgroundColor(Color.GREEN); 

在這裏,我實現了代碼

到目前爲止,我已經測試了兩種沒有運氣不同的方式。

// This goes in my main Client Activity 
Button verificationBtn = (Button) findViewById(R.id.verificationBtn); 
    verificationBtn.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 

       buffer.setLength(0); 
       mismatchTv.setText(""); 

       for (Client cli : clientList) { 
        if (cli.isCheckboxOneSelected() != cli.isCheckboxTwoSelected()) { 

        //We had defined above the following buffer = new StringBuffer(); 
        buffer.append((ah.parseInt(cli.getClient_number(), 0) - 1) + ", "); 
        cli.setMismatch(true); 

        //We are passing here the ID that correspond to the client mismatch 
        list_adapter.setBackgroundColor(Integer.parseInt(cli.getClient_number()) - 1); 

        setListAdapter(list_adapter); 
        Log.w("cli.getClient_number() ", String.valueOf(Integer.parseInt(cli.getClient_number()) - 1)); 
       } 


} 

// We display any mismatch on a TextView on top of the screen 
if (buffer.length() != 0) { 

    //This is a TextView on top of the screen 
    mismatchTv.setText("Error en Client(s) " 
     + buffer.toString()); 
} 




// This goes inside of the ClientArrayAdapter 
public void setBackgroundColor(int position) { 
    Log.w("inside of setBackgroundColor method", "True"); 
    switchIndex = 1; 
    positionFetched = position; 
} 

// This goes inside of the ClientArrayAdapter 
// and inside the body of public View getView(int position, View convertView, ViewGroup parent) { 
switch (switchIndex) { 

    case 1: 

     viewHolder.cbone 
      .setButtonDrawable(R.drawable.btn_checkbox_selector); 
     viewHolder.cbtwo 
      .setButtonDrawable(R.drawable.btn_checkbox_selector); 

     Log.w("switch 1 was called ", "True"); 
     for (Client cli : clientList) { 
      if (cli.isCheckboxOneSelected() != cli.isCheckboxTwoSelected()) { 

       Client cli = getItem(positionFetched); 

        if (cli.isMismatch()) { 
         cli.setColor(Color.BLACK); 
         Log.e("if (cli.isMismatch()) ", ""); 

         //HERE WE ARE TRYING TO HIGHLIGHT THE ROW WITH MISMATCH 
         //WHY THIS LINE DOES NOT WORK? 
         //THE ISSUE THAT I AM GETTING IS THAT I CANNOT CONTROL WHAT ROW TO AFFECT 
         //IN THE VIEW HOLDER 
         viewHolder.clientName.setBackgroundColor(Color.GREEN); 

        } 

       } 

      } 

     break; 

    default: 
     viewHolder.cbone.setButtonDrawable(R.drawable.disabled_cb); 
     viewHolder.cbtwo.setButtonDrawable(R.drawable.disabled_cb); 
     break; 
} 

// This goes inside of the ClientArrayAdapter   
    static class ViewHolder { 
     public TextView clientName; 
     public TextView clientNumber; 
     public ImageView imageView; 
     public CheckBox cbtwo; 
     public CheckBox cbone; 
     public int position; 
    } 
+0

大:)感謝您讓我們知道:)祝您好運。 – ElDuderino

+0

我明白,這不是任何Android開發人員每天都能看到的典型任務,這是一個針對老狼開發人員可以解決的高級請求。 :-) –

+0

我看不到一個問題,這就是爲什麼評論......但現在我發現它,隱藏在你的代碼深處......「爲什麼這條線路不起作用?」 ......你至少可以告訴我們你想要的是什麼......發生了什麼,應該發生什麼......你知道......我想你的問題是你只有一個單獨的觀點......說更多,我必須看到整個getView ... – ElDuderino

回答

0

後三日內試圖找出什麼不對的代碼,我終於找到了解決方案移動方法#1只是在getView方法的盡頭。 :-)

相關問題