2017-05-05 45 views
-1

我在Android的SolutionActivity類中有一個listview。適配器使用基於用戶在其他活動中輸入的結果的元素填充列表。 listview只能有兩個值,「true」或者「false」。我在網上搜索了很多'getView'方法,在自定義的適配器類中調用,但是我試圖實現這個,但是我不能弄清楚如何?難道我創建一個獨立的類,只是爲了我的適配器?或者,我可以在我的SolutionActivity的末尾添加呢?我如何使用getView方法?反正這是我的代碼...如何根據每行上的內容更改ListView中某一行的顏色?

public void setUserResults() { //displays the bit combination and users services in the listviews 
    ListView serviceNames = (ListView) findViewById(R.id.listofservices); 
    ListView bitResults = (ListView) findViewById(R.id.bitresults); 
    UserInputSet userInputSet = UserInputSet.getInstance(); 
    List<String> userServices = MainActivity.dimensions; 
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 
      userServices); 
    ArrayAdapter<Boolean> bitArrayAdapter = new ArrayAdapter<Boolean>(this, android.R.layout.simple_list_item_1, 
      CustomUseCase.getBestComboArray()); 
    serviceNames.setAdapter(arrayAdapter); 
    bitResults.setAdapter(bitArrayAdapter); 
} 

我想設置的行綠色如果「getBestComboArray()」的值是true,否則紅色如果是假的。任何人都可以提出一個很好的解決了這個?謝謝

+0

你必須使用自定義適配器 –

+0

@AliAhsan你知道,我只是不知道如何實現它,就像我在問題中所說的那樣。 –

+1

好的,讓我在回答中添加代碼 –

回答

2

創建自定義適配器類

public class CustomAdapter extends ArrayAdapter<String> { 
private List<Boolean> greenColor; 
private List<String> Words; 

public CustomAdapter(Activity context, List<String> words,List<Boolean> layoutcolor) 
{ 
        Words = words; 
      this.context = context 
      greenColor = layoutcolor; 

} 

@NonNull 
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    View view = convertView; 
    if(view==null) 
     view = LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false); 


    View view1 = view.findViewById(R.id.colorContainer); 

    if(layoutColor.get(position)){ 
    view1.setBackgroundColor(Color.GREEN); 
    }else{ 
      view1.setBackgroundColor(Color.RED); 
      } 


    TextView textViewdefault = (TextView) view.findViewById(R.id.text_view); 
    textViewdefault.setText(words.get(position)); 

    return view; 

} 
} 

創建名爲LIST_ITEM佈局和享有改變你想顯示每行

<LinearLayout 
    android:id="@+id/colorContainer" 
    android:layout_width="match_parent" 
    android:layout_toRightOf="@id/image" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 

    > 
<TextView 
    android:id="@+id/text_view" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    tools:text="text1" 
    android:textSize="18sp" 
    android:paddingTop="16dp" 
    android:layout_marginLeft="16dp" 
    android:textColor="#ffffff" 
    /> 



</LinearLayout> 

然後設置調用適配器列表視圖

CustomAdapter <String> bitArrayAdapter = new CustomAdapter <String>(this, TextViewList, // TextViewList is the list of text for textview of each row 
CustomUseCase.getBestComboArray()); 

bitResults.setAdapter(bitArrayAdapter); 

,你是好去:)

+0

讓我知道如果你仍然有任何問題 –

+0

我現在要測試,對於遲到的回覆感到抱歉 –

+0

嗨,我得到「ArrayAdapter 沒有默認構造函數」錯誤你給的構造函數 –

相關問題