我正在使用base.i在列表视图中成功地调整了我的适配器。但我有两个问题。在基本适配器的行xml包含imageview,在这个imageview中我想按位置更改背景图像(使用getTag) 这是我的来源 BaseAdapter
public class DealBoxXmlAdapterByPosition extends BaseAdapter {
Activity activity;
int layoutResourceId;
private MyCustomImageLoader ILoader;
UserHolder holder = null;
private ArrayList<DealBoxXmlComponentsByPosition> myadapter;
public DealBoxXmlAdapterByPosition(Activity act, int layoutResourceId,
ArrayList<DealBoxXmlComponentsByPosition> data) {
this.layoutResourceId = layoutResourceId;
this.activity = act;
this.myadapter = data;
ILoader = new MyCustomImageLoader(act.getApplicationContext());
notifyDataSetChanged();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = LayoutInflater.from(activity);
holder = new UserHolder();
row = inflater.inflate(layoutResourceId, parent, false);
holder.mainpageadapterimg = (ImageView) row
.findViewById(R.id.mainpageadapterimg);
holder.listvieaddtuchart = (ImageView) row
.findViewById(R.id.listvieaddtuchart);
holder.EndDate = (TextView) row.findViewById(R.id.EndDate);
holder.OriginalPrice = (TextView) row
.findViewById(R.id.OriginalPrice);
row.setTag(holder);
} else {
holder = (UserHolder) row.getTag();
}
ILoader.DisplayImage(myadapter.get(position).getKEY_Image(),
holder.mainpageadapterimg);
holder.OriginalPrice.setText(myadapter.get(position)
.getKEY_OriginalPrice() + " ლ");
holder.EndDate.setText(myadapter.get(position).getKEY_EndDate());
return row;
}
@Override
public int getCount() {
return myadapter.size();
}
@Override
public Object getItem(int position) {
return myadapter.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
public void removeObject(int position) {
this.myadapter.remove(position);
}
public static class UserHolder {
public TextView OriginalPrice, EndDate;
public ImageView mainpageadapterimg, listvieaddtuchart;
}
}
这是我的列表视图的onclicklistener方法
private UserHolder holder;
holder = new UserHolder();
holder.listvieaddtuchart
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Hello",
Toast.LENGTH_SHORT).show();
holder.listvieaddtuchart
.setBackgroundResource(R.drawable.ic_launcher);
if (holder.listvieaddtuchart.getDrawable().getConstantState() == getResources().getDrawable( R.drawable.ic_launcher).getConstantState())
{
Toast.makeText(getActivity(), "Image is ic_launcher", Toast.LENGTH_LONG).show();
// new RegisterAsyntaskNew().execute();
}
else
{
Toast.makeText(getActivity(), "Image isn't ic_launcher", Toast.LENGTH_LONG).show();
// new RegisterAsyntask().execute();
}
}
});
正如我所说,我有两个问题 首先,我评论了这一部分
if (holder.listvieaddtuchart.getDrawable().getConstantState() == getResources().getDrawable( R.drawable.ic_launcher).getConstantState())
{
Toast.makeText(getActivity(), "Image is ic_launcher", Toast.LENGTH_LONG).show();
// new RegisterAsyntaskNew().execute();
}
else
{
Toast.makeText(getActivity(), "Image isn't ic_launcher", Toast.LENGTH_LONG).show();
// new RegisterAsyntask().execute();
}
}
});
我只能在第二次点击时更改图像视图的背景。我无法在第一次点击时更改背景
我的第二个问题。 我想检查我的图像视图的背景资源。但我有nullpointexception
这是我的基本适配器xml代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#ffffff" >
<ImageView
android:id="@+id/mainpageadapterimg"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="115dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/relativeLayout1"
android:background="#ffffff" >
<TextView
android:id="@+id/mainpageadaptertitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/EndDate"
android:layout_alignLeft="@+id/EndDate"
android:layout_alignParentTop="true"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_toLeftOf="@+id/Price"
android:maxLines="3"
android:text="TextView"
android:textColor="#3e3e3e"
android:textSize="16dp" />
<TextView
android:id="@+id/OriginalPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="14dp"
android:text="priceeee"
android:textColor="#787878"
android:textSize="16dp" />
<ImageView
android:id="@+id/listvieaddtuchart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/OriginalPrice"
android:layout_marginBottom="3dp"
android:src="@drawable/mainpageadapterchartimg" />
<TextView
android:id="@+id/EndDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView2"
android:layout_alignParentBottom="true"
android:layout_marginLeft="20dp"
android:layout_marginBottom="5dp"
android:text="TextView"
android:textColor="#3e3e3e"
android:textSize="16dp" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="7dp"
android:layout_marginLeft="8dp"
android:src="@drawable/mainpageclockimage" />
<TextView
android:id="@+id/Price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/OriginalPrice"
android:layout_alignLeft="@+id/OriginalPrice"
android:layout_marginBottom="5dp"
android:text="TextView"
android:textColor="#f0812b"
android:textSize="16dp" />
</RelativeLayout>
如果有人知道解决方案,请帮助我 感谢
答案 0 :(得分:0)
就这样做..
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = LayoutInflater.from(activity);
holder = new UserHolder();
row = inflater.inflate(layoutResourceId, parent, false);
holder.mainpageadapterimg = (ImageView) row
.findViewById(R.id.mainpageadapterimg);
holder.listvieaddtuchart = (ImageView) row
.findViewById(R.id.listvieaddtuchart);
holder.EndDate = (TextView) row.findViewById(R.id.EndDate);
holder.OriginalPrice = (TextView) row
.findViewById(R.id.OriginalPrice);
row.setTag(holder);
} else {
holder = (UserHolder) row.getTag();
}
ILoader.DisplayImage(myadapter.get(position).getKEY_Image(),
holder.mainpageadapterimg);
holder.listvieaddtuchart
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Hello",
Toast.LENGTH_SHORT).show();
holder.listvieaddtuchart
.setBackgroundResource(R.drawable.ic_launcher);
if (holder.listvieaddtuchart.getDrawable().getConstantState() == getResources().getDrawable( R.drawable.ic_launcher).getConstantState())
{
Toast.makeText(getActivity(), "Image is ic_launcher", Toast.LENGTH_LONG).show();
// new RegisterAsyntaskNew().execute();
}
else
{
Toast.makeText(getActivity(), "Image isn't ic_launcher", Toast.LENGTH_LONG).show();
// new RegisterAsyntask().execute();
}
}
});
holder.OriginalPrice.setText(myadapter.get(position)
.getKEY_OriginalPrice() + " ლ");
holder.EndDate.setText(myadapter.get(position).getKEY_EndDate());
return row;
}
在getView()方法中设置onClickListener,无论convertView是否为null。快乐的编码! ; d
答案 1 :(得分:0)
尝试使用以下代码
holder.listvieaddtuchart
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Hello",
Toast.LENGTH_SHORT).show();
ImageView imageView = (ImageView) v;
if (imageView.getDrawable().getConstantState() == getResources().getDrawable( R.drawable.ic_launcher).getConstantState())
{
Toast.makeText(getActivity(), "Image is ic_launcher", Toast.LENGTH_LONG).show();
imageView.setImageResource(R.drawable.img1);
// new RegisterAsyntaskNew().execute();
}
else
{
Toast.makeText(getActivity(), "Image isn't ic_launcher", Toast.LENGTH_LONG).show();
imageView.setImageResource(R.drawable.ic_launcher);
// new RegisterAsyntask().execute();
}
}
});
如果您使用的是android:background =“@ drawable / mainpageadapterchartimg”,请按照以下代码
holder.imageViewIcon.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ImageView imageView = (ImageView) v;
if (imageView.getBackground().getConstantState() == mContext.getResources().getDrawable(R.drawable.ic_launcher).getConstantState()) {
imageView.setBackground(mContext.getResources().getDrawable(R.drawable.img1));
}else
{
imageView.setBackground(mContext.getResources().getDrawable(R.drawable.R.drawable.ic_launcher));
}
}
}
});