我在我的ListView
中显示了一些行,并希望通过查看列表中的任何日期是否与今天的日期匹配来仅更改其中一行。
我的部分代码是:
DateFormat df = new SimpleDateFormat("EEEEE, LLLL d", Locale.US);
String[] suspendedDates = {
"Tuesday, January 1",
"Wednesday, April 16",
"Monday, October 6",
"Wednesday, December 25"
};
lvDisplay = (ListView) findViewById(R.id.lvDisp);
for (int i = 0; i < suspendedDates.length; i ++) {
sDate = suspendedDates[i];
sReason = datesReason[i];
if (Arrays.asList(suspendedDates).contains(df.format(Calendar.getInstance(Locale.US).getTime()))) {
inIconShow = R.drawable.caliconpressed; //if today matches display a different drawable
contents.add(new SetRows(inIconShow, sDate, sReason));
}
if (!Arrays.asList(suspendedDates).contains(df.format(Calendar.getInstance(Locale.US).getTime()))) {
inIconShow = R.drawable.calicon; //if today doesn't match the array display the default drawable
contents.add(new SetRows(inIconShow, sDate, sReason));
}
}
// Now set your adapter.
adapter = new SetRowsCustomAdapter(MainActivity.this, R.layout.listdates, contents);
lvDisplay.setAdapter(adapter);
SetRowsCustomAdapter
上课:
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class SetRowsCustomAdapter extends ArrayAdapter<SetRows> {
Context context;
int layoutResourceId;
ArrayList<SetRows> data=new ArrayList<SetRows>();
public SetRowsCustomAdapter(Context context, int layoutResourceId, ArrayList<SetRows> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ImageHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ImageHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.tvDateVal);
//holder.txtTitle.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/robm.ttf"));
holder.imgIcon = (ImageView)row.findViewById(R.id.ivIcon0);
holder.txtID = (TextView)row.findViewById(R.id.tvReasonVal);
//holder.txtID.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/robm.ttf"));
row.setTag(holder);
}
else
{
holder = (ImageHolder)row.getTag();
}
SetRows myImage = data.get(position);
holder.txtTitle.setText(myImage.name);
holder.txtID.setText(myImage.id);
int outImage=myImage.image;
holder.imgIcon.setImageResource(outImage);
return row;
}
static class ImageHolder
{
ImageView imgIcon;
TextView txtTitle;
TextView txtID;
}
}
所以第4行应该有不同的图标,其他3应该有默认图标。发生的事情是,如果日期匹配,则每一行都有不同的图标,否则每一行都有默认图标。
我该如何解决?
答案 0 :(得分:1)
如果你想改变你可以用getView
方法改变图像,如下面的代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ImageHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ImageHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.tvDateVal);
//holder.txtTitle.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/robm.ttf"));
holder.imgIcon = (ImageView)row.findViewById(R.id.ivIcon0);
holder.txtID = (TextView)row.findViewById(R.id.tvReasonVal);
//holder.txtID.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/robm.ttf"));
row.setTag(holder);
}
else
{
holder = (ImageHolder)row.getTag();
}
SetRows myImage = data.get(position);
holder.txtTitle.setText(myImage.name);
holder.txtID.setText(myImage.id);
int outImage=myImage.image;
if (data.getValue)
holder.imgIcon.setImageResource(deferentIcon);
else
holder.imgIcon.setImageResource(defaultIcon);
return row;
}
如果您想要更改多行图标,您可以发送一个行列表并签入,并为刷新列表使用 adapter.notifyDataSetChanged()
修改强>
根据您的情况,您可以在DS中添加布尔值,如果日期等于设置为true,则另行设置,并在if语句中检查
答案 1 :(得分:0)
在适配器中使用viewHolder然后在适配器中使用notifyDataSetChanged(); 我已经习惯了,它会有所帮助。