我有一个列表视图,其中一行包含一个图像视图和3个布局,每个布局再次包含3个文本视图
xml就像这样
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/llRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/ivImage"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_gravity="center"
android:layout_marginLeft="2dp"
android:layout_marginTop="3dp"
android:layout_weight="0.2"
android:src="@drawable/ic_launcher" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".05"
android:src="@drawable/divider" />
<LinearLayout
android:id="@+id/ll1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="0.25"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/tvVal"
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_marginRight="5dip"
android:layout_marginTop="5dip"
android:background="@drawable/circel"
android:gravity="center"
android:text="0"
android:textColor="@android:color/white"
android:textSize="10sp"
android:visibility="invisible" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="99.99"
android:textColor="#000000"
android:textSize="16dp" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="Laundry"
android:textColor="#3A9ED4"
android:textSize="10dp" />
</LinearLayout>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".05"
android:src="@drawable/divider" />
<LinearLayout
android:id="@+id/ll2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="0.25"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/tvVal1"
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_marginRight="5dip"
android:layout_marginTop="5dip"
android:background="@drawable/circel"
android:gravity="center"
android:text="0"
android:textColor="@android:color/white"
android:textSize="10sp"
android:visibility="invisible" />
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="99.99"
android:textColor="#000000"
android:textSize="16dp" />
<TextView
android:id="@+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="Dry Clean"
android:textColor="#3A9ED4"
android:textSize="10dp" />
</LinearLayout>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".05"
android:src="@drawable/divider" />
<LinearLayout
android:id="@+id/ll3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="0.25"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/tvVal2"
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_marginRight="5dip"
android:layout_marginTop="5dip"
android:background="@drawable/circel"
android:gravity="center"
android:text="0"
android:textColor="@android:color/white"
android:textSize="10sp"
android:visibility="invisible" />
<TextView
android:id="@+id/tv5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="99.99"
android:textColor="#000000"
android:textSize="16dp" />
<TextView
android:id="@+id/tv6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="Stream Iron"
android:textColor="#3A9ED4"
android:textSize="10dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
我采用自定义适配器,我从服务器获取一些字符串值,并在listview中设置值
这里的代码是
public class CustomAdapter extends BaseAdapter {
ArrayList<String> p_id;
ArrayList<String> p_name;
Context context;
ArrayList<String> imageId;
ArrayList<String> wash_v;
ArrayList<String> dry_v;
ArrayList<String> iron_v;
Typeface tf;
ProgressDialog dialog;
Intent i;
float washItemCount = 0.0f;
String iconName;
Handler handler;
Runnable myRunnable;
Vector<BasketDO> vecBasket = new Vector<BasketDO>();
int countVal1, countVal2, countVal3;
public ImageLoader imageLoader;
ArrayList<String> laundry = null;
ArrayList<String> streamIron = null;
ArrayList<String> dryClean = null;
int KEY;
static int value1 = 0;
static int value2 = 0;
static int value3 = 0;
DbaseHandler dbaseHandler;
HashMap<Integer, Integer> laundryHashmap;
HashMap<Integer, Integer> ironHashmap ;
HashMap<Integer, Integer> dryCleanHashmap ;
public CustomAdapter(Context con, ArrayList<String> pid,
ArrayList<String> pname, ArrayList<String> pimg,
ArrayList<String> wash, ArrayList<String> dry,
ArrayList<String> iron, int KEY) {
p_id = pid;
context = con;
p_name = pname;
imageId = pimg;
wash_v = wash;
dry_v = dry;
iron_v = iron;
this.KEY = KEY;
imageLoader = new ImageLoader(context);
laundryHashmap = new HashMap<Integer, Integer>();
ironHashmap = new HashMap<Integer, Integer>();
dryCleanHashmap = new HashMap<Integer, Integer>();
}
@Override
public int getCount() {
return imageId.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
static class ViewHolder {
TextView tv1, tv2, tv3;
ImageView img;
LinearLayout ll1, ll2, ll3, llRow;
}
@SuppressLint("UseValueOf")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder vh = null;
if (convertView == null) {
vh = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.dynamic, parent, false);
vh.ll1 = (LinearLayout) convertView.findViewById(R.id.ll1);
vh.ll2 = (LinearLayout) convertView.findViewById(R.id.ll2);
vh.ll3 = (LinearLayout) convertView.findViewById(R.id.ll3);
vh.llRow = (LinearLayout) convertView.findViewById(R.id.llRow);
vh.tv1 = (TextView) convertView.findViewById(R.id.tv1);
vh.tv2 = (TextView) convertView.findViewById(R.id.tv3);
vh.tv3 = (TextView) convertView.findViewById(R.id.tv5);
vh.img = (ImageView) convertView.findViewById(R.id.ivImage);
convertView.setTag(vh);
}
else
vh = (ViewHolder) convertView.getTag();
vh.tv1.setText(wash_v.get(position));
vh.tv2.setText(iron_v.get(position));
vh.tv3.setText(dry_v.get(position));
ImageView image = vh.img;
imageLoader.DisplayImage(imageId.get(position), image);
final TextView tvVal = (TextView) convertView.findViewById(R.id.tvVal);
final TextView tvVal1 = (TextView) convertView
.findViewById(R.id.tvVal1);
final TextView tvVal2 = (TextView) convertView
.findViewById(R.id.tvVal2);
if (Appconstants.laundryHashmap != null
&& Appconstants.laundryHashmap.size() > 0) {
for (int i = 0; i < Appconstants.laundryHashmap.size(); i++) {
tvVal.setText(""+Appconstants.laundryHashmap.get(i));
}
}
if (Appconstants.ironHashmap != null
&& Appconstants.ironHashmap.size() > 0) {
for (int i = 0; i < Appconstants.ironHashmap.size(); i++) {
tvVal1.setText(""+Appconstants.ironHashmap.get(i));
}
}
if (Appconstants.dryCleanHashmap != null
&& Appconstants.dryCleanHashmap.size() > 0) {
for (int i = 0; i < Appconstants.dryCleanHashmap.size(); i++) {
tvVal2.setText(""+Appconstants.dryCleanHashmap.get(i));
}
}
i = new Intent("com.example.safewash");
vh.ll1.setOnClickListener(new OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(final View v) {
int count = 0;
String washItem = wash_v.get(position);
int washItems = Integer.parseInt(washItem);
if (washItems != 0) {
int colorStart = Color.LTGRAY;
int colorEnd = 0;
ValueAnimator animator = ObjectAnimator.ofInt(v,
"backgroundColor", colorStart, colorEnd);
animator.setDuration(800);
animator.setEvaluator(new ArgbEvaluator());
animator.setRepeatCount(0);
animator.start();
washItemCount = Float.parseFloat(washItem);
tvVal.setVisibility(View.VISIBLE);
count = Integer.parseInt(tvVal.getText().toString());
countVal1 = ++count;
i.putExtra("washItemCount", washItemCount);
i.putExtra("countVal1", countVal1);
context.sendBroadcast(i);
laundryHashmap.put(position, countVal1);
Appconstants.laundryHashmap = laundryHashmap;
if (Appconstants.laundryHashmap != null
&& Appconstants.laundryHashmap.size() > 0) {
for (int i = 0; i < Appconstants.laundryHashmap.size(); i++) {
value1 = Appconstants.laundryHashmap.get(position);
tvVal.setText("" + value1);
}
}
}
}
});
vh.ll2.setOnClickListener(new OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(final View v) {
int count = 0;
String washItem = iron_v.get(position);
washItemCount = Float.parseFloat(washItem);
int washItems = Integer.parseInt(washItem);
if (washItems != 0) {
int colorStart = Color.LTGRAY;
int colorEnd = 0;
ValueAnimator animator = ObjectAnimator.ofInt(v,
"backgroundColor", colorStart, colorEnd);
animator.setDuration(800);
animator.setEvaluator(new ArgbEvaluator());
animator.setRepeatCount(0);
animator.start();
washItemCount = Float.parseFloat(washItem);
tvVal1.setVisibility(View.VISIBLE);
count = Integer.parseInt(tvVal1.getText().toString());
countVal2 = ++count;
// tvVal1.setText("" + countVal2);
i.putExtra("washItemCount", washItemCount);
i.putExtra("countVal2", countVal2);
context.sendBroadcast(i);
tvVal1.setText("" + countVal2);
ironHashmap.put(position, countVal2);
// int val = ironHashmap.get(position);
Appconstants.ironHashmap = ironHashmap;
if (Appconstants.ironHashmap != null
&& Appconstants.ironHashmap.size() > 0) {
for (int i = 0; i < Appconstants.ironHashmap.size(); i++) {
value2 = Appconstants.ironHashmap.get(position);
tvVal1.setText("" + value2);
}
}
}
}
});
vh.ll3.setOnClickListener(new OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(final View v) {
int count = 0;
String washItem = dry_v.get(position);
washItemCount = Float.parseFloat(washItem);
int washItems = Integer.parseInt(washItem);
if (washItems != 0) {
int colorStart = Color.LTGRAY;
int colorEnd = 0;
ValueAnimator animator = ObjectAnimator.ofInt(v,
"backgroundColor", colorStart, colorEnd);
animator.setDuration(800);
animator.setEvaluator(new ArgbEvaluator());
animator.setRepeatCount(0);
animator.start();
washItemCount = Float.parseFloat(washItem);
tvVal2.setVisibility(View.VISIBLE);
count = Integer.parseInt(tvVal2.getText().toString());
countVal3 = ++count;
i.putExtra("washItemCount", washItemCount);
i.putExtra("countVal3", countVal3);
context.sendBroadcast(i);
dryCleanHashmap.put(position, countVal3);
Appconstants.dryCleanHashmap = dryCleanHashmap;
if (Appconstants.dryCleanHashmap != null
&& Appconstants.dryCleanHashmap.size() > 0) {
for (int i = 0; i < Appconstants.dryCleanHashmap.size(); i++) {
value2 = Appconstants.dryCleanHashmap.get(position);
tvVal2.setText("" + value3);
}
}
}
}
});
return convertView;
}
}
但是在这里我的问题是当点击linearlayout时,该值将增加1它工作正常并且该值也存储在基于位置的hashmap中,但是当我上下滚动listview时,值正在清除我们可以发出问题
帮助我们
提前感谢....
答案 0 :(得分:0)
你可以把这个代码放在if块
下面 tvVal = (TextView) convertView.findViewById(R.id.tvVal);
tvVal1 = (TextView) convertView.findViewById(R.id.tvVal1);
tvVal2 = (TextView) convertView.findViewById(R.id.tvVal2);
并在viewholder类中声明textview
TextView tvVal,tvVal1,tvVal2;
答案 1 :(得分:0)
您的ViewHolder
应如下所示:
static class ViewHolder {
TextView tv1, tv2, tv3,tvVal1,tvVal2,tvVal3;
ImageView image;
LinearLayout ll1, ll2, ll3, llRow;
}
现在将以下代码更新为:
if (convertView == null) {
//other viewholder reference
vh.tvVal = (TextView) convertView.findViewById(R.id.tvVal);
vh.tvVal1 = (TextView) convertView.findViewById(R.id.tvVal1);
vh.tvVal2 = (TextView) convertView.findViewById(R.id.tvVal2);
vh.image=(ImageView) convertView.findViewById(R.id.ivImage)
}
现在将您的其他部分更新为:
imageLoader.DisplayImage(imageId.get(position), vh.image);
if (Appconstants.laundryHashmap != null
&& Appconstants.laundryHashmap.size() > 0) {
for (int i = 0; i < Appconstants.laundryHashmap.size(); i++) {
vh.tvVal.setText(""+Appconstants.laundryHashmap.get(i));
}
}
if (Appconstants.ironHashmap != null
&& Appconstants.ironHashmap.size() > 0) {
for (int i = 0; i < Appconstants.ironHashmap.size(); i++) {
vh.tvVal1.setText(""+Appconstants.ironHashmap.get(i));
}
}
if (Appconstants.dryCleanHashmap != null
&& Appconstants.dryCleanHashmap.size() > 0) {
for (int i = 0; i < Appconstants.dryCleanHashmap.size(); i++) {
vh.tvVal2.setText(""+Appconstants.dryCleanHashmap.get(i));
}
}