java - 如何使用 ImageView 增加和减少值(数量)?

标签 java android listview

我想增加和减少点击数量的值。 我使用 ImageView 来减少和增加中间的值,我使用 TextView 来显示值。

这件事在 Activity 中完全有效,但我想在 ListView 中执行此操作。

这里是完整的代码,它正在处理简单的 Activity。

qty.java

public class qty extends AppCompatActivity {
    int minteger = 0;
    ImageView decrease;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.raw_order);
        decrease=(ImageView) findViewById(R.id.decrease);
        decrease.setEnabled(false);


    }

    public void increaseInteger(View view) {
        minteger = minteger + 1;
        display(minteger);

    }public void decreaseInteger(View view) {
        minteger = minteger - 1;
        display(minteger);
    }

    private void display(int number) {
        TextView displayInteger = (TextView) findViewById(
                R.id.integer_number);

        displayInteger.setText("" + number);
    }
}

raw_order.xml

<LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

            <ImageView
                android:id="@+id/decrease"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:src="@drawable/minus"
                android:layout_marginTop="05dp"
                android:textColor="#000"
                android:onClick="decreaseInteger"
                />
                <TextView
                    android:id="@+id/integer_number"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0"
                    android:layout_marginRight="10dp"
                    android:layout_marginLeft="10dp"
                    android:textStyle="bold"
                    android:layout_marginTop="16dp"
                    android:textSize="20dp" />
                <ImageView
                    android:id="@+id/increase"
                    android:layout_marginTop="05dp"
                    android:layout_width="30dp"
                    android:onClick="increaseInteger"
                    android:layout_height="wrap_content"
                    android:src="@drawable/plus"
                    android:textColor="#000"/>
                </LinearLayout>

我想在 ListView 中执行同样的操作

ListViewadapterorder.java

     package omcommunication.orderdesign;

import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static android.media.CamcorderProfile.get;

public class ListViewAdapterorder extends BaseAdapter  implements ListAdapter{
    Context cntx;
    SparseIntarray sia; // class variable
   // SessionManager session;
    AlertDialog.Builder alertDialogBuilder;
    View view;
    int minteger = 0;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    HashMap<String, String> resultp = new HashMap<String, String>();
    private boolean status;
    public static final String KEY_EMP_ID = "id";
    public static final String TAG_ID = "o_id";

    ArrayList<String> o_aid = new ArrayList<String>();
    ArrayList<String> o_aproduct = new ArrayList<String>();
    ArrayList<String> o_aqty = new ArrayList<String>();
    ArrayList<String> o_atotal = new ArrayList<String>();

    public ListViewAdapterorder(Context context,
                                ArrayList<String> o_id,
                                ArrayList<String> o_product,
                                ArrayList<String> o_qty,
                                ArrayList<String> o_total
    ) {
// TODO Auto-generated constructor stub

        cntx = context;

        o_aid = o_id;
        o_aproduct = o_product;
        o_aqty = o_qty;
        o_atotal = o_total;

        alertDialogBuilder = new AlertDialog.Builder(context);
        alertDialogBuilder.setMessage("Do You Want To Call....");
     /*   session = new SessionManager(context);
        HashMap<String, String> user = session.getUserDetails();
        uid = user.get(SessionManager.KEY_ID);
*/
    }

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

    @Override
    public Object getItem(int position) {
        return o_aid.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

        public View getView(final int position, View convertView, ViewGroup parent) {

            TextView mname , pmethod2, pamount3, premark4;
            final ImageView increase,decrease;
            final TextView displayInteger;
            inflater = (LayoutInflater) cntx
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            LayoutInflater inflater = LayoutInflater.from(cntx);

            convertView = inflater.inflate(R.layout.raw_order, parent,
                    false);
            decrease = (ImageView) convertView.findViewById(R.id.decrease);
            increase= (ImageView) convertView.findViewById(R.id.increase);
            mname = (TextView) convertView.findViewById(R.id.mname);
            mname.setText("  " + o_aproduct.get(position));
            sia = new SparseIntarray(data.size());

             displayInteger = (TextView) convertView.findViewById(R.id.integer_number);

            increase.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int count = sia.get(position);
                    count++;
                    resultp.put(cout);
                    displayInteger.setText("" + count);
                }
            });


            return convertView;
        }

    private class SparseIntarray {
        public SparseIntarray(int size) {

        }
    }
}

最佳答案

您可以使用SparseIntArray来存储每个位置的值计数

在listAdapter类中定义一个实例变量

 SparseIntArray sia; // class variable

.. listAdapter 构造函数内

sia = new SparseIntArray(data.size());

然后在 OnClick 事件中

public void onItemClick(...) {

     int count = sia.get(position)

     // now increment or decrement as per your requirement
    // coutn++; or count--;   and save it

    sia.put(position, count);

编辑 1:

在 getView() 方法中设置 OnClick 监听器以增加或减少 imageViews,

 final displayInteger = (TextView) convertView.findViewById(R.id.integer_number);

increase.setOnClikcListener(new OnClickListener {   
    @Override
    public void onClick(View view)
    {
   int count = sia.get(position);
   count++;
   sia.put(position, count);
   displayInteger.setText("" + count);

});

您可以对缩小 View 执行相同的操作

关于java - 如何使用 ImageView 增加和减少值(数量)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42872370/

相关文章:

java - 案例中涉及实例化的switch语句如何使用?

javascript - android webview js什么时候会捕获错误?

android - 如何将我的手机连接到 eclipse

android - 检测适配器上的项目 View 已被破坏

java - 多个线程不能同时工作

java - if/else 和 switch 语句的问题

java - 如何检查是否未选择 jcombobox 选择?

android - AlertDialog 未显示在 catch block 中

java - 在 Android 中动态创建多个 ListView

android - ScrollView 中的 ListView 在 Android 上不滚动