2016-09-30 89 views
-2

我是android和java的新手。我想讓ListView呈現紅色的第三行(不是每三行)。更改ListView中某一行的文本顏色

ArrayList<String> weekInfoList = new ArrayList<>(); 
weekInfoList.add("first row"); 
weekInfoList.add("second row"); 
weekInfoList.add("third row"); 

ArrayAdapter arrayAdapter1 = new ArrayAdapter(MainActivity.this, R.layout.list_item2, weekInfoList); 
weeklyListView.setAdapter(arrayAdapter1); 

weeklyListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> a, View v, int position, long id) { 

    // intent to next activity 
    } 
}); 

請幫幫我。

+0

你應該使用recyclerview而不是 –

+4

檢查這個答案http://stackoverflow.com/questions/13109840/android-alternate-row-colors-in-listview –

回答

0

在你的適配器類,你可能有多個項目顯示,因此,如果您正在使用android.support.v4比它會像,

holder.thirdTextview.setTextColor(ContextCompat.getColor(activity, R.color.red)); 
1

您可以創建自己的自定義ArrayAdapter,而不是使用默認,然後重寫getView方法以根據行位置設置顏色。

public class CustomAdapter extends ArrayAdapter { 
    public CustomAdapter(Context context, int resource) { 
     super(context, resource); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // check position 
     // if every 3rd row, set color 

     // return the modified convertView 
    } 
} 
1

您可以使用BaseAdapterArrayAdapter爲@ginomempin建議。並覆蓋它的方法getView

現在,如您所提到的每第三排想要更改文字顏色,您可以執行以下操作。

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

    LayoutInflater inf = (LayoutInflater) parent.getContext() 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    final Holder vh; 
    if (itemLayoutView == null) { 
     vh = new Holder(); 
     itemLayoutView = inf.inflate(R.layout.custom_layout, 
        null); 

     vh.textview = (TextView) itemLayoutView 
        .findViewById(R.id.textview); 
     temLayoutView.setTag(vh); 
    } else { 
     vh = (Holder) itemLayoutView.getTag(); 
    } 

    if(position%3==0){ 
     holder.textview.setTextColor(Color.parseColor("#ff0000")); 
    }else{ 
     holder.textview.setTextColor(Color.parseColor("#666666")); 
    } 

    return convertView; 
} 

有關如何使用自定義適配器參考ThisThis

快樂編碼的更多細節。

0

由於我對編程非常陌生,我試圖避免使用自定義適配器,但似乎我們必須使用它。

備選答案非常簡短。所以我在這裏附上我的解決方案以獲取更多詳細信

ArrayAdapter customAdapter = new MySimpleArrayAdapter(getApplicationContext(),weekInfoList); 
weeklyListView.setAdapter(customAdapter); 

public class MySimpleArrayAdapter extends ArrayAdapter { 
    private final Context context; 
    private final ArrayList<String> values; 

    public MySimpleArrayAdapter(Context context, ArrayList<String> values) { 
     super(context, -1, values); 
     this.context = context; 
     this.values = values; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View rowView = inflater.inflate(R.layout.list_item2, parent, false); 
     TextView textView = (TextView) rowView.findViewById(R.id.weeknumber2); 
     textView.setText(values.get(position)); 

     if (position==2){ 
      textView.setTextColor(Color.RED); 
     } 

     return rowView; 
    } 
} 

感謝Suraj的解決方案https://stackoverflow.com/a/13109854/2466516

相關問題