2016-09-30 2 views
-2

저는 android와 java를 처음 사용하고 있습니다. ListView 붉은 색으로 3 행 (3 행마다가 아니라)의 텍스트를 만들고 싶습니다.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 
    } 
}); 

도와주세요. 이 같은 것보다 당신이 android.support.v4를 사용하는 경우 귀하의 어댑터 클래스에서

+0

대신 recyclerview를 사용해야합니다. –

+4

답장을 확인하십시오. http://stackoverflow.com/questions/13109840/android-alternate-row-colors-in-listview –

답변

0

, 당신은

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

당신은 사용자 정의 ArrayAdapter를 만드는 대신 기본값을 사용하여 수, 표시 할 하나 개 이상의 항목이있을 수 있습니다 행 위치에 따라 색상을 설정하려면 getView 메소드를 대체하십시오. @ginomempin가 제안

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

당신은 BaseAdapter 또는 ArrayAdapter를 사용할 수 있습니다. 그것을 getView 방법으로 대체하십시오.

이제 3 번째 행마다 텍스트 색상을 변경하려고 했으므로 다음을 수행 할 수 있습니다. 어댑터 ThisThis

해피 코딩을 참조 사용자 정의 사용하는 방법에 대한 자세한 내용은

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

.

0

저는 프로그래밍에 익숙하지 않기 때문에 사용자 지정 어댑터를 피하려고했지만 사용해야하는 것처럼 보입니다.

대체 답변은 매우 간단합니다. 그래서 여기에 더 자세한 참조를 위해 내 솔루션을 첨부합니다. SURAJ의 솔루션 https://stackoverflow.com/a/13109854/2466516

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

감사합니다.

관련 문제