I want to Increase and decrease value of qty on clicked. i Used ImageView
for decrease and Increase the value in middle i used TextView for showing the value.
this thing is completely work in the Activity but i want perform this operation in ListView.
Here the complete code and it is working on simple 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>
And i want to perform this same thing in 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) {
}
}
}
you can use SparseIntArray to store the value count for each position
in the listAdapter class define an instance variable
SparseIntArray sia; // class variable
.. inside listAdapter constructor
sia = new SparseIntArray(data.size());
then in OnClick event
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);
Edit 1:
set OnClick listeners to your increase decrease imageViews in the getView() method,
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);
});
you can do the same with your decrease view
为图像视图创建一个OnLickListener并调用方法,对于列表视图,您需要创建一个OnItemSelectedListener或OnItemClickListener
First You Need To Give The ImageView an ID; Then Do It Like This In On Create :::
ImageView img=(ImageView)findViewById(R.id.imageview);
img.setOnClickListener(imgclick);
Then In The Class Body ::::
OnClickListener imgclick=new OnClickListener() {
@Override
public void onClick(View view) {
//Call The Methods Like Increase Number Or Decrease
}
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.