2015-10-03 3 views
0

Я работаю над приложением для покупок. В которой есть кнопка «Плюс» и «Минус» для добавления элементов в корзину.Изменение TextView на кнопке Нажмите

Если пользователь нажимает на кнопку Плюс есть TextView, которая должна меняться от 0 к 1 или более одинаковых вещей должно быть сделано, если пользователь нажимает на кнопку минус то значение TextView должно уменьшаться.

Я изменил значение, но его увеличение и уменьшение для нескольких строк одновременно.

@SuppressLint("InflateParams") 
public class ProductListAdapter extends BaseAdapter { 
    private Activity activity; 
    private LayoutInflater inflater; 
    private List<Product> products; 
    private ProductListAdapterListener listener; 
    int t; 
    int count = 0; 
    Context context; 
    List<Product> rowItems; 
    String[] result; 
    ViewHolder holder; 

    protected static final String TAG = "ReviewAdapter"; 
    ImageLoader imageLoader = AppController.getInstance().getImageLoader(); 
    private HashMap<Integer, Integer> mCountHash; 

    public ProductListAdapter(Activity activity, List<Product> feedItems, 
      ProductListAdapterListener listener) { 
     this.activity = activity; 
     this.products = feedItems; 
     this.listener = listener; 
    } 

    public static class ViewHolder { 
     TextView name, description, price, qty; 
     Button btnAddToCart, btnReduce; 
     NetworkImageView image; 
    } 

    @Override 
    public int getCount() { 
     return products.size(); 
    } 

    @Override 
    public Object getItem(int location) { 
     return products.get(location); 
    } 

    @Override 
    public int getItemViewType(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @SuppressLint("ViewHolder") 
    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 

     if (inflater == null) 
      inflater = (LayoutInflater) activity 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     Resources resources = this.context.getResources(); 
     if (convertView == null) 
      convertView = inflater.inflate(R.layout.list_item_product, null); 
     final View vii = convertView; 

     final Product product = products.get(position); 
     holder = new ViewHolder(); 

     holder.name = (TextView) convertView.findViewById(R.id.productName); 
     holder.description = (TextView) convertView 
       .findViewById(R.id.productDescription); 
     holder.qty = (TextView) convertView.findViewById(R.id.productQty); 

     holder.price = (TextView) convertView.findViewById(R.id.productPrice); 
     holder.image = (NetworkImageView) convertView 
       .findViewById(R.id.productImage); 

     // holder.qty=(TextView) convertView.findViewById(R.id.productQty); 
     holder.btnAddToCart = (Button) convertView 
       .findViewById(R.id.btnAddToCart); 
     holder.btnReduce = (Button) convertView.findViewById(R.id.btnReduce); 

     holder.name.setText(product.getName()); 
     holder.description.setText(product.getDescription()); 
     holder.price.setText("$" + product.getPrice()); 
     // user profile pic 

     holder.image.setImageUrl("http://flavorbaba.com/adminpanel/images/" 
       + product.getImage(), imageLoader); 

     holder.btnAddToCart.setTag(position); 
     holder.qty.setTag(position); 
     holder.btnAddToCart.setTag(holder); 
     // holder.btnReduce.setEnabled(false); 

     // holder.btnReduce.setVisibility(View.GONE)); 
     holder.btnAddToCart.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       count++; 

       listener.onAddToCartPressed(product); 

       View parentView = (View) vii.getParent(); 
       // TextView tvqty = ((TextView) parentView 
       // .findViewById(R.id.textview1)).getText().toString(); 
       TextView tvqty = ((TextView) parentView 
         .findViewById(R.id.productQty)); 
       tvqty.setText(String.valueOf(count)); 
       // Button btnAddToCart = (Button) v; 

       // holder.qty.setText(String.valueOf(count)); 
       // notifyDataSetChanged(); 

       Toast.makeText(activity, String.valueOf(position), 
         Toast.LENGTH_SHORT).show(); 
      } 

     }); 

     holder.btnReduce.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       // t = Integer.parseInt(holder.qty.getText().toString()); 
       // holder.qty.setText(String.valueOf(t - 1)); 

       listener.onReducePressed(product); 
      } 
     }); 
     convertView.setTag(holder); 

     return convertView; 
    } 

    public interface ProductListAdapterListener { 
     public void onAddToCartPressed(Product product); 

     public void onReducePressed(Product product); 
    } 

} 
+0

Вы уже знаете систему рециркуляции 'ListView's? –

+0

Нет, я не знаком, вы можете мне посоветовать, где я ошибаюсь –

+1

http://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works –

ответ

0

Надеюсь, что этот код может вам помочь.

plus = (Button) findViewById(R.id.btn_plus); 
    minus = (Button) findViewById(R.id.btn_minus); 

    plus.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) 
     { 
      // TODO Auto-generated method stub 
      txt_number.setText(Integer.toString(textvalue)); 
      textvalue++; 
     } 
    }); 

    minus.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      textvalue--; 
      txt_number.setText(Integer.toString(textvalue)); 
     } 
    }); 
+0

Я пробовал, но проблема в том, что он меняет текстовое представление значение двух текстовых просмотров в одно и то же время. –

+0

установка текста в одно и то же текстовое окно дважды перезаписывает первый письменный текст. Итак, во второй раз, когда мы используем settext, мы просто добавляем новую строку как 'textview.append (" ")' – prat

+0

Нет успеха. Я нажал кнопку где-то еще, и он изменил какой-то другой файл –

Смежные вопросы