我的listview item上有两个按钮 要求button点击 editText值加一,总共有三条数据,但是界面listview初始了以后第一行按钮点击了editText值不变,上下滚动一下再点击就可以了,上下滚动几次后,第三条上button点击会回填到第二行的edittext,值回填错乱,请高手指点啊~!~~这是自定义的adapter
package com.mtickets.adapter;import java.util.List;import com.mtickets.activity.R;
import com.mtickets.vo.ShoppingCartVo;import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;public class ShoppingCartAddAdapter extends BaseAdapter { private class ButtonViewHolder {
Button button_ticket_count_add;
EditText editText__ticket_count;
Button button__ticket_count_decrease;
}
private List<ShoppingCartVo> list = null;
private LayoutInflater inflater;
//private int ticket_count = 1;
private ButtonViewHolder holder;
private EditText[] editTextsCanche;
private int positionTemp;
public ShoppingCartAddAdapter(List<ShoppingCartVo> list,Context context)
{
this.list = list;
this.inflater = LayoutInflater.from(context);
editTextsCanche = new EditText[list.size()];
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
} @Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
} @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
positionTemp = position;
if (convertView != null) {
holder = (ButtonViewHolder) convertView.getTag();
} else {
convertView = inflater.inflate(R.layout.shopping_cart_add_part, null);
holder = new ButtonViewHolder();
holder.button_ticket_count_add = (Button) convertView.findViewById(R.id.button_shopping_cart_add_ticket_count_add);
holder.button__ticket_count_decrease = (Button) convertView.findViewById(R.id.button_shopping_cart_add_ticket_count_decrease);
holder.editText__ticket_count = (EditText) convertView.findViewById(R.id.editText_shopping_cart_add_ticket_count);
convertView.setTag(holder);
}
Log.i("第" + String.valueOf(positionTemp) + "行", holder.editText__ticket_count.getText().toString());
holder.editText__ticket_count.setText(holder.editText__ticket_count.getText().toString());
editTextsCanche[positionTemp] = holder.editText__ticket_count;
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView_ticket_add);
TextView textView_ticket_add_title = (TextView) convertView.findViewById(R.id.textView_ticket_add_title);
TextView textView_ticket_add_subheading = (TextView) convertView.findViewById(R.id.textView_ticket_add_subheading);
TextView textView_ticket_add_time = (TextView) convertView.findViewById(R.id.textView_ticket_add_time);
TextView textView_ticket_add_price = (TextView) convertView.findViewById(R.id.textView_ticket_add_price);
TextView textView_ticket_add_etPrice = (TextView) convertView.findViewById(R.id.textView_ticket_add_etPrice);
if(list != null)
{
ShoppingCartVo infoVo = list.get(position);
textView_ticket_add_title.setText("2011蓝精灵 3D" + String.valueOf(position));
textView_ticket_add_subheading.setText("31家影院60元通用票A");
textView_ticket_add_time.setText("有效期:2011年11月11日");
textView_ticket_add_price.setText("价格:¥60");
textView_ticket_add_etPrice.setText("市场价:¥100");
}
holder.button_ticket_count_add.setOnClickListener(new button_ticket_count_add_Listener(position));
holder.button__ticket_count_decrease.setOnClickListener(new button_ticket_count_decrease_Listener(position));
return convertView;
}
class button_ticket_count_add_Listener implements OnClickListener {
private int position; button_ticket_count_add_Listener(int pos) {
position = pos;
}
@Override
public void onClick(View v) {
int vid=v.getId();
if (vid == holder.button_ticket_count_add.getId())
{
Log.i("点击添加:", String.valueOf(position));
holder.editText__ticket_count = editTextsCanche[position];
int ticketCount = Integer.parseInt(holder.editText__ticket_count.getText().toString());
ticketCount = ticketCount + 1;
holder.editText__ticket_count.setText(String.valueOf(ticketCount));
Log.i(String.valueOf(position)+ "===添加后的值为:", holder.editText__ticket_count.getText().toString());
editTextsCanche[position] = holder.editText__ticket_count;
}
}
}class button_ticket_count_decrease_Listener implements OnClickListener {
private int position; button_ticket_count_decrease_Listener(int pos) {
position = pos;
}
@Override
public void onClick(View v) {
int vid=v.getId();
if (vid == holder.button__ticket_count_decrease.getId())
{
Log.i("点击减:", String.valueOf(position));
holder.editText__ticket_count = editTextsCanche[position];
int ticketCount = Integer.parseInt(holder.editText__ticket_count.getText().toString());
if(ticketCount > 1)
{
ticketCount = ticketCount - 1;
holder.editText__ticket_count.setText(String.valueOf(ticketCount));
Log.i(String.valueOf(position)+ "===减去后的值为:", holder.editText__ticket_count.getText().toString());
editTextsCanche[position] = holder.editText__ticket_count;
}
}
}
}
}
这是Activitypackage com.mtickets.activity;import java.util.ArrayList;
import java.util.List;import com.mtickets.adapter.ShoppingCartAddAdapter;
import com.mtickets.tools.Communal;
import com.mtickets.tools.Constant;
import com.mtickets.tools.InitTopAndBottom;
import com.mtickets.vo.ScreenVo;
import com.mtickets.vo.ShoppingCartVo;import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;public class ShoppingCartAddActivity extends Activity { private ListView listView = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
InitTopAndBottom initTileEnd = new InitTopAndBottom(this,inflate);
setContentView(initTileEnd.initTitleEnd(Constant.shoppingCartAdd));
listView = (ListView) findViewById(R.id.listView_Shopping_cart_add);
listView.setVerticalScrollBarEnabled(true);
List<ShoppingCartVo> list = initData();
ShoppingCartAddAdapter shoppingCartAddAdapter = new ShoppingCartAddAdapter(list,ShoppingCartAddActivity.this);
listView.setAdapter(shoppingCartAddAdapter);
shoppingCartAddAdapter.notifyDataSetChanged();
//计算Listview列表高度
int autoHeight = ScreenVo.getHeight() - Communal.getAutoWidthOrHeight(80+40, ShoppingCartAddActivity.this) - Communal.getTopOrBottomHeight(ShoppingCartAddActivity.this);
ViewGroup.LayoutParams autoLayoutParams = listView.getLayoutParams();
autoLayoutParams.height = autoHeight;
listView.setLayoutParams(autoLayoutParams);
listView.setOnItemClickListener(listView_OnItemClickListener);
}
ListView.OnItemClickListener listView_OnItemClickListener = new ListView.OnItemClickListener(){ @Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.i("========", String.valueOf(arg2));
}};
private List<ShoppingCartVo> initData() {
// TODO Auto-generated method stub
List<ShoppingCartVo> list = new ArrayList<ShoppingCartVo>();
ShoppingCartVo shoppingCartVo = new ShoppingCartVo();
ShoppingCartVo shoppingCartVo1 = new ShoppingCartVo();
ShoppingCartVo shoppingCartVo2 = new ShoppingCartVo();
list.add(shoppingCartVo);
list.add(shoppingCartVo1);
list.add(shoppingCartVo2);
return list;
}
}
package com.mtickets.adapter;import java.util.List;import com.mtickets.activity.R;
import com.mtickets.vo.ShoppingCartVo;import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;public class ShoppingCartAddAdapter extends BaseAdapter { private class ButtonViewHolder {
Button button_ticket_count_add;
EditText editText__ticket_count;
Button button__ticket_count_decrease;
}
private List<ShoppingCartVo> list = null;
private LayoutInflater inflater;
//private int ticket_count = 1;
private ButtonViewHolder holder;
private EditText[] editTextsCanche;
private int positionTemp;
public ShoppingCartAddAdapter(List<ShoppingCartVo> list,Context context)
{
this.list = list;
this.inflater = LayoutInflater.from(context);
editTextsCanche = new EditText[list.size()];
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
} @Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
} @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
positionTemp = position;
if (convertView != null) {
holder = (ButtonViewHolder) convertView.getTag();
} else {
convertView = inflater.inflate(R.layout.shopping_cart_add_part, null);
holder = new ButtonViewHolder();
holder.button_ticket_count_add = (Button) convertView.findViewById(R.id.button_shopping_cart_add_ticket_count_add);
holder.button__ticket_count_decrease = (Button) convertView.findViewById(R.id.button_shopping_cart_add_ticket_count_decrease);
holder.editText__ticket_count = (EditText) convertView.findViewById(R.id.editText_shopping_cart_add_ticket_count);
convertView.setTag(holder);
}
Log.i("第" + String.valueOf(positionTemp) + "行", holder.editText__ticket_count.getText().toString());
holder.editText__ticket_count.setText(holder.editText__ticket_count.getText().toString());
editTextsCanche[positionTemp] = holder.editText__ticket_count;
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView_ticket_add);
TextView textView_ticket_add_title = (TextView) convertView.findViewById(R.id.textView_ticket_add_title);
TextView textView_ticket_add_subheading = (TextView) convertView.findViewById(R.id.textView_ticket_add_subheading);
TextView textView_ticket_add_time = (TextView) convertView.findViewById(R.id.textView_ticket_add_time);
TextView textView_ticket_add_price = (TextView) convertView.findViewById(R.id.textView_ticket_add_price);
TextView textView_ticket_add_etPrice = (TextView) convertView.findViewById(R.id.textView_ticket_add_etPrice);
if(list != null)
{
ShoppingCartVo infoVo = list.get(position);
textView_ticket_add_title.setText("2011蓝精灵 3D" + String.valueOf(position));
textView_ticket_add_subheading.setText("31家影院60元通用票A");
textView_ticket_add_time.setText("有效期:2011年11月11日");
textView_ticket_add_price.setText("价格:¥60");
textView_ticket_add_etPrice.setText("市场价:¥100");
}
holder.button_ticket_count_add.setOnClickListener(new button_ticket_count_add_Listener(position));
holder.button__ticket_count_decrease.setOnClickListener(new button_ticket_count_decrease_Listener(position));
return convertView;
}
class button_ticket_count_add_Listener implements OnClickListener {
private int position; button_ticket_count_add_Listener(int pos) {
position = pos;
}
@Override
public void onClick(View v) {
int vid=v.getId();
if (vid == holder.button_ticket_count_add.getId())
{
Log.i("点击添加:", String.valueOf(position));
holder.editText__ticket_count = editTextsCanche[position];
int ticketCount = Integer.parseInt(holder.editText__ticket_count.getText().toString());
ticketCount = ticketCount + 1;
holder.editText__ticket_count.setText(String.valueOf(ticketCount));
Log.i(String.valueOf(position)+ "===添加后的值为:", holder.editText__ticket_count.getText().toString());
editTextsCanche[position] = holder.editText__ticket_count;
}
}
}class button_ticket_count_decrease_Listener implements OnClickListener {
private int position; button_ticket_count_decrease_Listener(int pos) {
position = pos;
}
@Override
public void onClick(View v) {
int vid=v.getId();
if (vid == holder.button__ticket_count_decrease.getId())
{
Log.i("点击减:", String.valueOf(position));
holder.editText__ticket_count = editTextsCanche[position];
int ticketCount = Integer.parseInt(holder.editText__ticket_count.getText().toString());
if(ticketCount > 1)
{
ticketCount = ticketCount - 1;
holder.editText__ticket_count.setText(String.valueOf(ticketCount));
Log.i(String.valueOf(position)+ "===减去后的值为:", holder.editText__ticket_count.getText().toString());
editTextsCanche[position] = holder.editText__ticket_count;
}
}
}
}
}
这是Activitypackage com.mtickets.activity;import java.util.ArrayList;
import java.util.List;import com.mtickets.adapter.ShoppingCartAddAdapter;
import com.mtickets.tools.Communal;
import com.mtickets.tools.Constant;
import com.mtickets.tools.InitTopAndBottom;
import com.mtickets.vo.ScreenVo;
import com.mtickets.vo.ShoppingCartVo;import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;public class ShoppingCartAddActivity extends Activity { private ListView listView = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
InitTopAndBottom initTileEnd = new InitTopAndBottom(this,inflate);
setContentView(initTileEnd.initTitleEnd(Constant.shoppingCartAdd));
listView = (ListView) findViewById(R.id.listView_Shopping_cart_add);
listView.setVerticalScrollBarEnabled(true);
List<ShoppingCartVo> list = initData();
ShoppingCartAddAdapter shoppingCartAddAdapter = new ShoppingCartAddAdapter(list,ShoppingCartAddActivity.this);
listView.setAdapter(shoppingCartAddAdapter);
shoppingCartAddAdapter.notifyDataSetChanged();
//计算Listview列表高度
int autoHeight = ScreenVo.getHeight() - Communal.getAutoWidthOrHeight(80+40, ShoppingCartAddActivity.this) - Communal.getTopOrBottomHeight(ShoppingCartAddActivity.this);
ViewGroup.LayoutParams autoLayoutParams = listView.getLayoutParams();
autoLayoutParams.height = autoHeight;
listView.setLayoutParams(autoLayoutParams);
listView.setOnItemClickListener(listView_OnItemClickListener);
}
ListView.OnItemClickListener listView_OnItemClickListener = new ListView.OnItemClickListener(){ @Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.i("========", String.valueOf(arg2));
}};
private List<ShoppingCartVo> initData() {
// TODO Auto-generated method stub
List<ShoppingCartVo> list = new ArrayList<ShoppingCartVo>();
ShoppingCartVo shoppingCartVo = new ShoppingCartVo();
ShoppingCartVo shoppingCartVo1 = new ShoppingCartVo();
ShoppingCartVo shoppingCartVo2 = new ShoppingCartVo();
list.add(shoppingCartVo);
list.add(shoppingCartVo1);
list.add(shoppingCartVo2);
return list;
}
}
<?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="@color/commonBackGround"
>
<RelativeLayout android:id="@+id/relativeLayout20" android:layout_width="wrap_content" android:layout_height="200dip"
android:descendantFocusability="blocksDescendants"
android:layout_margin="1dip"
android:gravity="center"
>
<ImageView android:layout_height="150dip" android:id="@+id/imageView_ticket_add" android:layout_width="100dip" android:src="@drawable/picture2"
android:layout_margin="10dip"
></ImageView>
<TextView
android:textSize="17dip"
android:textColor="@color/deep_fount_color"
android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="2011蓝精灵 3D" android:id="@+id/textView_ticket_add_title"
android:layout_marginLeft="120dip"
android:layout_marginTop="20dip"
></TextView>
<TextView
android:textSize="17dip"
android:textColor="@color/deep_fount_color"
android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="31家影院60元通用票A" android:id="@+id/textView_ticket_add_subheading"
android:layout_marginLeft="120dip"
android:layout_marginTop="40dip"
></TextView>
<TextView
android:textSize="17dip"
android:textColor="@color/shallow_fount_color"
android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="有效期:2011年11月11日" android:id="@+id/textView_ticket_add_time"
android:layout_marginLeft="120dip"
android:layout_marginTop="60dip"
></TextView>
<TextView
android:textSize="17dip"
android:textColor="@color/ticketCount"
android:layout_height="wrap_content" android:layout_width="150dip" android:text="价格:¥60" android:id="@+id/textView_ticket_add_price"
android:layout_marginLeft="120dip"
android:layout_marginTop="80dip"
/>
<TextView
android:textSize="17dip"
android:textColor="@color/shallow_fount_color"
android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="市场价: ¥100" android:id="@+id/textView_ticket_add_etPrice"
android:layout_marginLeft="120dip"
android:layout_marginTop="100dip"
></TextView>
<Button
android:textStyle="bold"
android:textSize="30dip"
android:background="@drawable/deep_button_selector"
android:textColor="@color/white_fount"
android:layout_marginLeft="10dip"
android:layout_marginTop="160dip"
android:id="@+id/button_shopping_cart_add_ticket_count_decrease" android:layout_width="40dip" android:layout_height="40dip" android:text="-"></Button>
<EditText
android:textSize="17dip"
android:textColor="@color/shallow_fount_color"
android:layout_marginLeft="60dip"
android:layout_marginTop="160dip"
android:layout_width="50dip" android:id="@+id/editText_shopping_cart_add_ticket_count" android:text="1" android:layout_height="40dip"></EditText>
<Button
android:textStyle="bold"
android:textSize="30dip"
android:background="@drawable/deep_button_selector"
android:textColor="@color/white_fount"
android:layout_marginLeft="120dip"
android:layout_marginTop="160dip"
android:id="@+id/button_shopping_cart_add_ticket_count_add" android:layout_width="40dip" android:layout_height="40dip" android:text="+"></Button>
<Button
android:textSize="20dip"
android:background="@drawable/white_button_selector"
android:layout_marginLeft="170dip"
android:layout_marginTop="160dip"
android:id="@+id/button3" android:layout_width="60dip" android:layout_height="40dip" android:text="已关注"></Button>
<Button
android:textSize="20dip"
android:background="@drawable/white_button_selector"
android:layout_marginLeft="240dip"
android:layout_marginTop="160dip"
android:id="@+id/button4" android:layout_width="60dip" android:layout_height="40dip" android:text="删除"></Button>
</RelativeLayout>
</LinearLayout>
这是listview<?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="@color/commonBackGround"
>
<TableLayout
android:stretchColumns="*"
android:id="@+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TableRow android:id="@+id/tableRow1" android:layout_width="fill_parent" android:layout_height="wrap_content">
<ListView android:layout_width="fill_parent" android:layout_height="320dip" android:id="@+id/listView_Shopping_cart_add"
android:divider="@color/list_divider"
android:dividerHeight="1dip"
android:scrollbars="none"
android:scrollbarSize="0dip"
android:cacheColorHint="#00000000"
android:fastScrollEnabled="true"
></ListView>
</TableRow>
<TableRow android:id="@+id/tableRow2" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
>
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="40dip"
android:gravity="center"
>
<TextView
android:textSize="17dip"
android:textColor="@color/deep_fount_color"
android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="合计:" android:id="@+id/textView5"
android:layout_marginLeft="10dip"
android:layout_marginTop="10dip"
/>
<TextView
android:textSize="17dip"
android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="¥400" android:id="@+id/textView6"
android:textColor="@color/ticketCount"
android:layout_marginLeft="10dip"
android:layout_marginTop="10dip"
/>
<TextView
android:textSize="17dip"
android:textColor="@color/deep_fount_color"
android:layout_height="wrap_content" android:layout_width="wrap_content" android:text=",总共" android:id="@+id/textView7"
android:layout_marginLeft="10dip"
android:layout_marginTop="10dip"
/>
<TextView
android:textSize="17dip"
android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="4" android:id="@+id/textView8"
android:textColor="@color/ticketCount"
android:layout_marginLeft="10dip"
android:layout_marginTop="10dip"
/>
<TextView
android:textSize="17dip"
android:textColor="@color/deep_fount_color"
android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="张票券" android:id="@+id/textView9"
android:layout_marginLeft="10dip"
android:layout_marginTop="10dip"
/>
</LinearLayout>
</TableRow>
<TableRow android:id="@+id/tableRow3" android:layout_width="fill_parent" android:layout_height="80dip"
android:gravity="center"
android:layout_gravity="center"
>
<Button
android:layout_marginLeft="40dip"
android:layout_marginRight="40dip"
android:layout_marginBottom="10dip"
android:layout_marginTop="10dip"
android:textColor="@color/searchButtonFount"
android:background="@drawable/deep_button_selector"
android:textSize="20dip"
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:text="结 算"
></Button>
</TableRow>
</TableLayout>
</LinearLayout>
在线等啊!!!!!
================
这个是不是没有刷新的问题啊。
你将TextView控件改为TextSwitcher控件试试。Whenever setText(CharSequence) is called, TextSwitcher animates the current text out and animates the new text in.
TextView值加一了。就是因为没刷新,不知道为何,更诡异的是上下滑动两下,回填值就错乱了
@Override
public View getView(int position, View convertView, ViewGroup parent) {}