我想在点击时增加和减少数量值。
我使用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) {
}
}
}
答案 0 :(得分:1)
您可以使用 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:
将onClick侦听器设置为getView()方法中的增量减少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);
});
您可以对缩小视图执行相同的操作
答案 1 :(得分:0)
为图像视图创建一个OnLickListener并调用方法, 对于列表视图,您需要创建OnItemSelectedListener或OnItemClickListener
答案 2 :(得分:0)
首先,您需要为ImageView提供ID;然后就这样做 在On Create :::
ImageView img=(ImageView)findViewById(R.id.imageview);
img.setOnClickListener(imgclick);
然后在班级体内::::
OnClickListener imgclick=new OnClickListener() {
@Override
public void onClick(View view) {
//Call The Methods Like Increase Number Or Decrease
}
};