2017-03-02 23 views
0

キー/値のペアの行ごとに2つのTextViewを持つカスタムListviewに詳細な製品情報を表示しようとしています。データが正しく表示されます。私はまた、違うセカンドラインを着色しました。Android - カスタムListViewビューア

私の問題があります。上下にスクロールすると、異なる色の行が色を変えてこの状態に留まります。データはこの問題の影響を受けません。 TextViewのバックグラウンドカラー。私はViewHolderパターンを使用しますが、これは何も変わりません。アダプターのコードを追加しました。私は十分だと思う。何かお考えですか?問題の

スクリーンショット:

enter image description here

コード:

public class ProductDetailAdapter extends BaseAdapter { 

private LinkedHashMap<String,String> list; 
private Context context; 

public ProductDetailAdapter(Context c, LinkedHashMap<String,String> list){ 
    super(); 
    this.context = c; 
    this.list=list; 

} 
@Override 
public int getCount() { 
    return list.size(); 
} 

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

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

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

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    ProductDetailAdapter.ViewHolder viewHolder; 

    if(convertView == null){ 
     convertView=inflater.inflate(R.layout.product_detail_data_row,null); 
     viewHolder = new ViewHolder(); 
     viewHolder.textViewKey = (TextView) convertView.findViewById(R.id.productDataKey); 
     viewHolder.textViewValue = (TextView) convertView.findViewById(R.id.productDataValue); 
     convertView.setTag(viewHolder); 

    }else { 
     viewHolder = (ProductDetailAdapter.ViewHolder) convertView.getTag(); 
    } 

    viewHolder.textViewKey.setText((String)list.keySet().toArray()[position]); 
    viewHolder.textViewValue.setText(list.get(list.keySet().toArray()[position])); 

    if(position % 2 == 0){ 
     viewHolder.textViewKey.setBackgroundColor(context.getResources().getColor(R.color.colorParkerWhite2)); 
     viewHolder.textViewValue.setBackgroundColor(context.getResources().getColor(R.color.colorParkerWhite2)); 
    } 

    return convertView; 
} 

private static class ViewHolder { 

    public TextView textViewKey; 
    public TextView textViewValue; 

    public ViewHolder(){}; 

} 
} 
+0

のために動作します参照してください。あなたの質問を編集してください。感謝! – aldakur

+1

image url:https://i.stack.imgur.com/OS3Op.pngはうまく動作します – Mike

+0

'if(position%2 == 0)'の中に新しい 'ViewHolderインスタンス'を作成してみてください – aldakur

答えて

4

行がリサイクルされているので起こります。それはよくある問題です。

次の操作を行うことによってそれを解決することができます:あなたは、アダプタへのデータソースを渡したらノー

super(); 

super(c, 0, list); 

これに代えて:

if(position % 2 == 0){ 
    viewHolder.textViewKey.setBackgroundColor(context.getResources().getColor(R.color.colorParkerWhite2)); 
    viewHolder.textViewValue.setBackgroundColor(context.getResources().getColor(R.color.colorParkerWhite2)); 
} else { 
    viewHolder.textViewKey.setBackgroundColor(context.getResources().getColor(R.color.colorParkerWhite1)); //Or the color that you want for odd rows 
    viewHolder.textViewValue.setBackgroundColor(context.getResources().getColor(R.color.colorParkerWhite1)); //Or the color that you want for odd rows 
} 
0

はこれを試してみてくださいより長いニーズ:

同様にgetCount

のgetItem

getItemId

0

このlinklink2link3これらは、画像のURLが壊れているあなた

関連する問題