我是android开发的初学者。我有两个按钮,TextView采用线性布局。当我点击加号按钮值增加多个行项目和相同的减号按钮点击时间。当我滚动点击值移动到下一个位置。你能帮我解决如何修复错误。我写了我的代码。{{ 3}}
ListViewAdapter.java
public class ItemListAdapter extends BaseAdapter {
public Context context;
public LayoutInflater inflater;
public ArrayList<JSONObject> al = null;
public ItemListAdapter(Activity context, ArrayList<JSONObject> itemListAl) {
// TODO Auto-generated constructor stub
super();
this.context = context;
this.al = itemListAl;
this.inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return al.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return al.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
class ViewHolder {
TextView txtListItems;
ImageView imgArrow;
TextView qtyview;
TextView qtyvalue;
ImageButton increament;
ImageButton decreament;
RelativeLayout rootLayout;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
int type = getItemViewType(position);
System.out.println("getView " + position + " " + convertView + " type = " + type);
final ViewHolder holder;
if (convertView == null) {
//LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//row = inflater.inflate(R.layout.items_rowview_plus, parent, false);
convertView = inflater.inflate(R.layout.items_rowview_plus,parent, false);
holder = new ViewHolder();
holder.txtListItems = (TextView) convertView.findViewById(R.id.tv_listItems);
holder.imgArrow = (ImageView) convertView.findViewById(R.id.img_arrow);
holder.qtyview = (TextView) convertView.findViewById(R.id.qty_view);
holder.qtyvalue = (TextView) convertView.findViewById(R.id.qty_value);
holder.increament = (ImageButton) convertView.findViewById(R.id.increament_btn);
holder.decreament = (ImageButton) convertView.findViewById(R.id.decreament_btn);
holder.rootLayout = (RelativeLayout) convertView
.findViewById(R.id.root_RelativeLayout);
holder.increament.setTag(holder);
holder.decreament.setTag(holder);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
try {
if(position==0){
//convertView.setBackgroundResource(R.drawable.listview_top);
convertView.setBackgroundResource(R.drawable.listview_header);
}
else if(position==al.size()-1){
convertView.setBackgroundResource(R.drawable.listview_bottom);
}
else{
convertView.setBackgroundResource(R.drawable.listview_middle);
}
if(position == 0){
holder.txtListItems.setText(al.get(position).get("ListviewHeader").toString());
holder.imgArrow.setVisibility(View.GONE);
holder.qtyvalue.setVisibility(View.GONE);
holder.qtyview.setVisibility(View.GONE);
holder.increament.setVisibility(View.GONE);
holder.decreament.setVisibility(View.GONE);
//convertView.setBackgroundResource(R.drawable.listview_top);
holder.txtListItems.setEnabled(false);
holder.txtListItems.setTextColor(context.getResources().getColor(R.color.white));
//holder.rootLayout.setBackgroundResource(R.color.dark_blue);
}
else{
holder.txtListItems.setText(al.get(position).get("c_field_name").toString());
holder.imgArrow.setVisibility(View.VISIBLE);
holder.qtyvalue.setVisibility(View.VISIBLE);
holder.qtyview.setVisibility(View.VISIBLE);
holder.increament.setVisibility(View.VISIBLE);
holder.decreament.setVisibility(View.VISIBLE);
holder.txtListItems.setEnabled(true);
holder.txtListItems.setTextColor(context.getResources().getColor(R.color.Black));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
holder.rootLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!al.get(position).isNull("c_field_name")) {
Intent intent = new Intent(v.getContext(), ItemDetailsActivity.class);
try {
SharedData.sharedInstance().editItemDetails=false;
SharedData.sharedInstance().ListId = al.get(position).getString("c_id").toString();
Log.v("cid",SharedData.sharedInstance().ListId);
SharedData.sharedInstance().ListName = al.get(position).get("c_field_name").toString();
Log.v("ListName",SharedData.sharedInstance().ListName);
SharedData.sharedInstance().CubicValue = al.get(position).get("c_cubic_feet").toString();
Log.v("CubicValue",SharedData.sharedInstance().CubicValue);
SharedData.sharedInstance().strListName = "ListItemView";
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
v.getContext().startActivity(intent);
}
}
});
holder.increament.setOnClickListener(new View.OnClickListener() {
int count=0;
@Override
public void onClick(View v) {
count = Integer.valueOf(holder.qtyvalue.getText().toString());
count = count+1;
holder.qtyvalue.setText(String.valueOf(count));
Log.d("Plus Btn", "Clicked");
}
});
holder.decreament.setOnClickListener(new View.OnClickListener() {
int count=0;
@Override
public void onClick(View v) {
count = Integer.valueOf(holder.qtyvalue.getText().toString());
count = count-1;
holder.qtyvalue.setText(String.valueOf(count));
}
});
return convertView;
}
}
item_rowview_plus.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root_RelativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#E6E6E6" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:id="@+id/tv_listItems"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_weight="0.15"
android:gravity="top"
android:padding="5dp"
android:text="TextView" />
<TextView
android:id="@+id/qty_view"
android:layout_width="wrap_content"
android:layout_height="38dp"
android:layout_alignBottom="@+id/decreament_btn"
android:layout_weight="0.02"
android:text="Qty"
/>
<TextView
android:id="@+id/qty_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.02"
android:text="0"
android:textAlignment="center"
android:textStyle="bold" />
<ImageButton
android:id="@+id/increament_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="@drawable/button_plus_normal" />
<ImageButton
android:id="@+id/decreament_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/button_minus_normal"
android:layout_gravity="top"
android:cropToPadding="true" />
<ImageView
android:id="@+id/img_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:layout_marginTop = "23dp"
android:background="@drawable/common_listview_arrow_right"
/>
</LinearLayout>
请告诉我如何修复错误。