거기에 ArrayList<String>
몇 가지 데이터가 있습니다. 다른 항목과 함께 내 Custom ArrayAdapter 생성자에 전달하고 있습니다. getView 메서드에서 목록 Arraylist<String>
항목으로 항목을 확인하고 있습니다.
그들이 같으면 Listview에서 textview의 색상을 변경하고 싶습니다. 일부 항목의 색상을 변경하지만 Listview를 스크롤하면 ArrayList<String>
전달 되지 않은 다른 항목의 색상이 변경 됩니다.
다음은 내 생성자와 getView () 메서드의 코드입니다.
public CustomMenuArrayAdapter(Context ctx,int res,String[] menuName,String[] menuDesc,String[] menuIngred,String[] menuPrice,List orderNames){
super(ctx,res,menuName);
this.ctx=ctx;
this.menuName=menuName;
this.menuDesc=menuDesc;
this.menuIngred=menuIngred;
this.menuPrice=menuPrice;
this.orderNames=orderNames;
this.inflaters=LayoutInflater.from(ctx);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHold;
View v=convertView;
if(v==null){
viewHold=new ViewHolder();
v=inflaters.inflate(R.layout.category_details,parent,false);
// viewHold.txtQtys=(TextView)v.findViewById(R.id.txtQty);
viewHold.txtAdd=(TextView)v.findViewById(R.id.txtAdd);
viewHold.txtPrices=(TextView)v.findViewById(R.id.txtPrices);
viewHold.txtName=(TextView)v.findViewById(R.id.txtCatNameDetails);
viewHold.txtDesc=(TextView)v.findViewById(R.id.txtCatDescDetails);
viewHold.txtIngred=(TextView)v.findViewById(R.id.txtIngredientDetails);
// viewHold.add=(ImageView)v.findViewById(R.id.imgPlus);
// viewHold.sub=(ImageView)v.findViewById(R.id.imgMinus);
v.setTag(viewHold);
// viewHold.txtName.setTag(position);
}else{
viewHold=(ViewHolder)v.getTag();
}
if(orderNames.get(0)!=null || !orderNames.get(0).equals("")) {
for(int k=0;k<orderNames.size();k++) {
Log.e("k:----->"+k," menuN:"+menuName[position]+"<--->"+orderNames.get(k));
if (orderNames.get(k).toString().equals(menuName[position])) {
TextView txt=(TextView)v.findViewById(R.id.txtCatNameDetails);
txt.setBackgroundColor(Color.LTGRAY);
break;
}
}
}
viewHold.txtName.setText(menuName[position]);
viewHold.txtDesc.setText("Desc: "+menuDesc[position]);
viewHold.txtIngred.setText("Ingred: "+menuIngred[position]);
viewHold.txtPrices.setText("UGX : " + menuPrice[position]);
return v;
}
논리가 올바른지 확인하십시오. 예를 들어, 이것은 잘못 보입니다.
if (orderNames.get(0) != null || !orderNames.get(0).equals("")) {
....
}
수행하는 첫 번째 확인은 다음과 같습니다.
orderNames.get(0) != null
이 검사의 두 가지 결과 :
TRUE : orderNames.get(0)
is not null
===> 두 번째 검사도 수행되지 않습니다 ORing
.
FALSE : orderNames.get(0)
is null
===> ===>이기 때문에 두 번째 검사가 수행 ORing
되며 NullPointerException
여기에 다음 이 제공됩니다 !(null).equals("")
.
더 좋아 보이나요?
// Check if orderNames.get(0) is empty (note the use of `trim()`)
if (orderNames.get(0) != null && !orderNames.get(0).trim().equals("")) {
....
}
더 나은 :
// Rejects `null`, ""(empty string) & " "(strings that contain only whitespaces)
if (!TextUtils.isEmpty(orderNames.get(0)) && !TextUtils.isEmpty(orderNames.get(0).trim)) {
....
}
그래도이 수표로 무엇을 성취했는지 모르겠습니다. 이것은 목록의 첫 번째 항목에 비어 있지 않은 문자열이 포함되어 있음을 확인합니다. 나중에 전체 목록을 반복하고 null 또는 빈 문자열 검사도 수행하지 않습니다.
나는 이것을 권장하지 않지만 시도해보십시오. 작동한다면 이유를 알아 내십시오.
// Reset - in case this view was recycled
viewHold.txtName.setBackgroundColor(Color.TRANSPARENT);
if (!TextUtils.isEmpty(orderNames.get(0)) && !TextUtils.isEmpty(orderNames.get(0).trim)) {
for(int k = 0; k < orderNames.size(); k++) {
Log.e("k:----->" + k, " menuN: " + menuName[position] + "<--->" + orderNames.get(k));
// Can possibly throw NPE
// If `menuName[position]` is guaranteed to be non-null, you can reverse the check
if (orderNames.get(k).toString().equals(menuName[position])) {
viewHold.txtName.setBackgroundColor(Color.LTGRAY);
break;
}
}
}
참고 : ViewHolder
패턴 구현 은 괜찮습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다