データベースにアイテムのリストがあり、値は 0、1、または 2 です。simplecursor アダプターと、表示用のリストビューを持つアクティビティを使用しました。ここで、各アイテムの背景色をその値を反映するように変更します。つまり、値が 0 のアイテムの背景を緑、値が 1 のアイテムを黄色、値が 2 のアイテムを赤にします。どうすればいいですか?現在、アダプタとアクティビティの背景が黒くなっています。
質問する
854 次
3 に答える
1
位置の値を確認してgetView()
、そのインデックス付きの行に必要な背景色を指定できます。
于 2013-01-15T10:37:09.077 に答える
-1
リスト ビューの単一リスト アイテムの XML レイアウト ファイルを作成する必要があります。次に、アダプター クラスを作成します。そのxmlビューを膨らませます。getView() メソッドのそのアダプター内で、ロジックを確認します。
これが私のアダプタクラス getView()..
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.home_list_item, null);
holder = new ViewHolder();
holder.mTxtContactName = (TextView) convertView.findViewById(R.id.txtContactName);
holder.mTxtContactNumber = (TextView) convertView.findViewById(R.id.txtContactNumber);
holder.mTxtContactAdd = (TextView) convertView.findViewById(R.id.txtContactAddress);
holder.mImgCall = (ImageButton) convertView.findViewById(R.id.imgCall);
holder.mImgSms = (ImageButton) convertView.findViewById(R.id.imgSms);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mTxtContactName.setText(list.get(position).getContactName());
holder.mTxtContactNumber.setText(list.get(position).getContactNumber());
holder.mTxtContactAdd.setText(list.get(position).getContactAdd());
holder.mImgSms.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("con", "SMS");
Intent smsIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"+list.get(position).getContactNumber()));
context.startActivity(smsIntent);
}
});
holder.mImgCall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel://"+list.get(position).getContactNumber()));
context.startActivity(callIntent);
}
});
return convertView;
}
static class ViewHolder {
TextView mTxtContactName;
TextView mTxtContactNumber;
TextView mTxtContactAdd;
ImageButton mImgCall;
ImageButton mImgSms;
}
于 2013-01-15T11:34:50.290 に答える
-1
次のように、アダプタの getView() 内をチェックしてください
if(item.getValue()==0)
v.setBackgroundColor(Color.GREEN);
else if(item.getValue()==1)
v.setBackgroundColor(Color.YELLOW);
else
v.setBackgroundColor(Color.RED);
item
ここにオブジェクトがgetValue()
あり、値0、1、2がある場所です。
于 2013-01-15T11:15:46.350 に答える