2017-09-12 3 views
0

세 개의 TextView가있는 사용자 정의 목록보기가 있습니다. 모든 행에는 사람 이름, 전자 메일 및 요일이 표시됩니다. 이제는 120 일 미만의 날짜가있는 붉은 색을 설정하고 120 일 이상인 녹색 색을 설정하고 싶습니다. 내 문제를 해결하는 방법 ?? 어떤 도움을 주시면 감사하겠습니다.android에서 ListView 배경 항목 색상을 설정하는 방법

My ListView Image

person_show

public class person_show extends Activity 
{ 
    static int total_day; 
    List<personInfo>PersonInfo; 
    public MyAdapter adapter; 
    static Vector<String>email = new Vector<>(); 
    static Vector<String>name = new Vector<>(); 
    static Vector<String>city = new Vector<>(); 
    static String[] Name,Email,City; 
    ListView list; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.personlistview); 
     Firebase.setAndroidContext(this); 
     //show_person(); 
     Person_Show(); 
    } 

    private void Person_Show() 
    { 
     PersonInfo = new ArrayList<>(); 
     list = (ListView)findViewById(R.id.list); 

      String[] value = new String[]{" No Data Found"}; 
      ArrayAdapter<String> Adapter = new ArrayAdapter<String>(this,R.layout.persontextview,value); 
      list.setAdapter(Adapter); 

      PersonInfo.add(new personInfo("abc","[email protected]","65 days")); 
      PersonInfo.add(new personInfo("xyz","[email protected]","130 days")); 
      PersonInfo.add(new personInfo("pqr","[email protected]","70 days")); 
      PersonInfo.add(new personInfo("ABC","[email protected]","140 days")); 
      for(int i = 0;i<email.size();i++) 
       PersonInfo.add(new personInfo(name.get(i),email.get(i),"150 days")); 
      adapter = new MyAdapter(this,PersonInfo); 
      list.setAdapter(adapter); 

    } 

MyAdapter

public class MyAdapter extends BaseAdapter 
{ 
    List<personInfo>PersonInfo; 
    Context context; 
    public MyAdapter(Context context,List<personInfo>personInfo) 
    { 
     this.context = context; 
     this.PersonInfo = personInfo; 
    } 
    @Override 
    public int getCount() { 
     return PersonInfo.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return PersonInfo.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

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

     LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View view = inflater.inflate(R.layout.person_layout,parent,false); 
     TextView txt_name,txt_email,txt_phone; 
     txt_name = (TextView)view.findViewById(R.id.t_name); 
     txt_email = (TextView)view.findViewById(R.id.t_email); 
     txt_phone = (TextView)view.findViewById(R.id.t_phone); 
     txt_name.setText(PersonInfo.get(position).getName()); 
     txt_email.setText(PersonInfo.get(position).getEmail()); 
     txt_phone.setText(PersonInfo.get(position).getPhone()); 
     return view; 
    } 
} 

personInfo

public class personInfo 
{ 
    String name,phone,email; 
    public personInfo(String name,String email,String phone) 
    { 
     this.name = name ; 
     this.email = email; 
     this.phone = phone; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getPhone() { 
     return phone; 
    } 

    public void setPhone(String phone) { 
     this.phone = phone; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 
} 
+1

어댑터 코드 – Rajasekhar

+0

게시 그래서 무엇을 시도 했습니까? 어떤 문제에 직면 해 있습니까? – Piyush

+0

plesae 일부 어댑터 코드를 붙여 넣습니다. –

답변

0

어댑터 코드를 표시합니다. 처음에 days을 확인하십시오. days에는 일수가 없습니다. daysint이어야합니다.

아래를 확인하십시오. LOGIC.

String[] splited = PersonInfo.get(position).getPhone().split("\\s+"); //65 days 
String str_Days=splited[0]; //65 
int days = Integer.valueOf(str_Days); 

if (days<=120) 
{ 
     Your_LayoutOBJ.setBackgroundColor(Color.parseColor("#54D66A")); 
} 
else 
{ 
     Your_LayoutOBJ.setBackgroundColor(Color.parseColor("#FFFFFF")); 
} 

은 참고

Your_LayoutOBJ은 RootLayout입니다.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/rootLayout" 
    > 

    ..... 

</RelativeLayout> 

LIKE Textview and Set background color를 선언합니다.

0

당신이 사용하는 경우 어댑터 또는 뷰 홀더에이 코드를 추가합니다 RecyclerView

if(yourObject.days>120){ 
    rootLayout.setBackgroundColor(ContextCompat.getColor(mContex‌​t, R.color.green)); 
    }else{ 
    rootLayout.setBackgroundColor(ContextCompat.getColor(mContex‌​t, R.color.red)); 
    }