我已经制作了一个列表视图,每行包含以下项目
第三个文本视图位于两个按钮之间(按钮将增加和减少textview中显示的值)。当我增加和减少第一行textview的值,然后我转到第二行时,它取以前的值当我增加和减少它的价值。 我正在使用计数器,并在点击按钮时将计数器增加固定金额。
问题面对:
我使用了适配器类。
如何解决这个问题,因为我是android新手。 告诉我这样做的方法。
这是我的适配器类:
public class CartListViewAdapter extends BaseAdapter{
private Context mcontext;
private static int counter;
private String stringVal;
//private static int i = position;
public CartListViewAdapter(Context c){
mcontext = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length ;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View myView = convertView;
//if (convertView == null) { // if it's not recycled, initialize some attributes
//Inflate the layout
LayoutInflater inflater = (LayoutInflater)mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = inflater.inflate(R.layout.row, null);
// Add The Image!!!
ImageView iv = (ImageView)myView.findViewById(R.id.image);
iv.setImageResource(mThumbIds[position]);
// Add The Text!!!
TextView tv = (TextView)myView.findViewById(R.id.text);
tv.setText(names[position] );
final TextView tv1 = (TextView)myView.findViewById(R.id.text1);
tv1.setText(names1[position]);
TextView tv3 = (TextView)myView.findViewById(R.id.text3);
tv3.setText(names3[position]);
ImageButton button = (ImageButton)myView.findViewById(R.id.addbutton);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText(mcontext,"Button is clicked",Toast.LENGTH_SHORT).show();
// counter+=250;
// stringVal = Integer.toString(counter);
// tv1.setText(stringVal);
for(int position=0;position<mThumbIds.length;position++){
counter = 250;
counter+=250;
stringVal = Integer.toString(counter);
tv1.setText(stringVal);
}
}
});
ImageButton button2= (ImageButton)myView.findViewById(R.id.subbutton);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText(mcontext,"Button is clicked",Toast.LENGTH_SHORT).show();
for(int position=0;position<mThumbIds.length;position++){
counter = 250;
counter-=250;
stringVal = Integer.toString(counter);
tv1.setText(stringVal);
}
}
});
return myView;
}
private Integer[] mThumbIds = {
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher
};
private String[] names={"ab","cd","ef"};
private String[] names1 = {"250","250","250"};
private String[] names3 = {"yhhjd","hnbnn","fdffd"};
}
这是我的xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:orientation="vertical"
android:padding="10sp" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10sp" />
</LinearLayout>
<ImageButton
android:id="@+id/addbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50sp"
android:layout_marginTop="20sp"
android:background="@android:color/background_light"
android:src="@android:drawable/ic_input_add" />
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3sp"
android:layout_marginTop="20sp" />
<ImageButton
android:id="@+id/subbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20sp"
android:background="@android:color/background_light"
android:src="@android:drawable/ic_input_add" />
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10sp"
android:layout_marginTop="20sp"
android:layout_weight="0.3"
android:gravity="right" />
</LinearLayout>
感谢。
答案 0 :(得分:1)
您想减少或增加每个textView的值吗?如果是的话
for(int position=0;position<mThumbIds.length;position++){
counter = Integer.parseInt(tv1.getText());
counter-=250;
stringVal = Integer.toString(counter);
tv1.setText(stringVal);
}
答案 1 :(得分:1)
试试这个:
CartListViewAdapter myAdapter = ((AdapterView)v.getParent()).getAdapter();
for(int i=0;i<myAdapter.getCount();i++) {
counter = Integer.parseInt(names1[i]);
counter -= 250;
names[i] = String.valueOf(counter);
}
myAdapter.notifiyDataSetChanged();
我相信这会有用,虽然已准备好修复一些代码。
答案 2 :(得分:0)
我想将我的建议作为解决方案。
在处理列表视图项目时以及更新列表视图项目的情况下,您必须更新源值的内容(即:arraylist)。
更新arraylist中的值并通知适配器数据集已更改。作为NotifyDataSetChanged()
NotifyDataSetChanged()