I have a listview containing items name and price i made my code so when i click on one item it displays its price in a small dialog box at the bottom of the screen but now when i click another item i want to add that item's price to the first one and so on..
Here is my code for now:
#Override
public View getView(final int position, final View convertView, ViewGroup parent) // inflating the layout and initializing widgets
{
View rowView = convertView;
ViewHolder viewHolder = null;
if (rowView == null) {
LayoutInflater inflater = getLayoutInflater();
rowView = inflater.inflate(R.layout.listcontent, parent, false);
viewHolder = new ViewHolder();
viewHolder.textName = rowView.findViewById(R.id.name);
viewHolder.textData = rowView.findViewById(R.id.details);
viewHolder.textImage = rowView.findViewById(R.id.price);
viewHolder.producticon = rowView.findViewById(R.id.producticon);
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
// here setting up names and images
viewHolder.textName.setText(parkingList.get(position).getProname() + "");
viewHolder.textData.setText(parkingList.get(position).getData());
viewHolder.textImage.setText(parkingList.get(position).getImage());
Picasso.with(context).load(parkingList.get(position).getProducticon()).into(viewHolder.producticon);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
//What happens when you click on a place!
// Intent intent = new Intent(LoggedIn.this,MapsActivity.class);
// startActivity(intent);
final int count = 0;
LayoutInflater inflater2 = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final Dialog mBottomSheetDialog = new Dialog(context, R.style.MaterialDialogSheet);
View content = inflater2.inflate(R.layout.activity_main2, null);
mBottomSheetDialog.setContentView(content);
TextView textView = (TextView) content.findViewById(R.id.mebuyss);
final TextView itemcount = (TextView) content.findViewById(R.id.itemcount);
Button plus = (Button) content.findViewById(R.id.plus);
Button minus = (Button) content.findViewById(R.id.minus);
Button finish = (Button) content.findViewById(R.id.finishgettingitem);
textView.setText(parkingList.get(position).getProname());
plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter = counter + 1;
itemcount.setText(String.valueOf(counter));
}
});
minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter --;
if(counter<0){
counter=0;
}
itemcount.setText(String.valueOf(counter));
}
});
final Dialog mBottomSheetDialog2 = new Dialog(context, R.style.MaterialDialogSheet);
final View content2 = inflater2.inflate(R.layout.smalldialog, null);
final TextView total = (TextView) content2.findViewById(R.id.totalpriceofsmalldialog);
total.setText(null);
finish.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String get = itemcount.getText().toString();
Float last = Float.parseFloat(get) * Float.parseFloat(parkingList.get(position).getImage());
mBottomSheetDialog.dismiss();
mBottomSheetDialog2.setContentView(content2);
mBottomSheetDialog2.setCancelable(false);
mBottomSheetDialog2.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
mBottomSheetDialog2.getWindow().setGravity(Gravity.BOTTOM);
total.setText(String.valueOf(last));
mBottomSheetDialog2.show();
mBottomSheetDialog2.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
mBottomSheetDialog2.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
doneonce = true;
}
});
// if (doneonce = true){
// Float priceofitem = parseFloat(parkingList.get(position).getImage());
// Float currentprice = parseFloat(total.getText().toString());
// Float finalfloat = priceofitem * currentprice;
// total.setText(String.valueOf(finalfloat));
//
// }
mBottomSheetDialog.setCancelable(true);
mBottomSheetDialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
mBottomSheetDialog.getWindow().setGravity(Gravity.BOTTOM);
if (!mBottomSheetDialog.isShowing()){
counter = 1;
}
//
mBottomSheetDialog.show();
}
});
return rowView;
}
I tried using boolean but it didn't go as well as i expected.
I will appreciate any help!
Is all of your code (the getView() method) within your ArrayAdapter class? If so, move you listener register out into your Activity, and then the listener needs only pull the required value from the selected item and append it to a property of your Activity class, and then raise the view to show the running total.
Related
Iam working on a simple invoice app which invloes methods like selecting products. Im having the problem where i use custom adapter to listview to view my product details and since i use custom adapter listview onclicklistner is not working.
All i need is to close the alertdialog while i click and select a product from the listview.
I used customadapter to make listview dynamic.
Here is my code for alertdialog box,
public void selectProduct(View v) {
showProducts();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.select_product_list, null);
alertDialog.setView(convertView);
ArrayList<String> categoryListReverce = reverse(categoryList);
categorySpinner = (Spinner) convertView.findViewById(R.id.spinner1);
ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.orderlist_spinner_row, categoryListReverce);
arrayAdapter.setDropDownViewResource(R.layout.orderlist_spinner_row);
categorySpinner.setAdapter(arrayAdapter);
categorySpinner.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this);
ListView lv = (ListView) convertView.findViewById(R.id.listView);
listAdapter = new CustomNewInvoiceAdapter(this, R.layout.batch_sales_list, new ArrayList<OrderListModel>());R.id.productprice});
lv.setAdapter(listAdapter);
textPartySearch = (EditText) convertView.findViewById(R.id.searchContainer);
textContactSearch = (EditText) convertView.findViewById(R.id.contactno);
footer = (LinearLayout) convertView.findViewById(R.id.footer1);
getCashPartyName = (TextView) convertView.findViewById(R.id.partyname);
getCashPartyContact = (TextView) convertView.findViewById(R.id.contactno);
submit = (Button) convertView.findViewById(R.id.submit);
final AlertDialog ad = alertDialog.show();
textPartySearch.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
adapter2.getFilter()
.filter(textPartySearch.getText().toString());
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
footer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ad.dismiss();
}
});
}
And here is my customAdapter getView,
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
CustomNewInvoiceAdapter.Holder holder = null;
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
holder = new CustomNewInvoiceAdapter.Holder();
holder.pack = items.get(position);
if (items.get(position).getType().equals("header")) {
row = inflater.inflate(headerLayoutResourceId, parent, false);
holder.itemName = row.findViewById(R.id.productName);
holder.itemName.setText(String.valueOf(holder.pack.getItemName()));
row.setTag(holder);
} else {
row = inflater.inflate(itemLayoutResourceId, parent, false);
holder.itemCode = row.findViewById(R.id.itemCode);
holder.itemName = row.findViewById(R.id.productName);
holder.CostTxt = row.findViewById(R.id.ed_cost);
holder.ItemDis = row.findViewById(R.id.ed_ItemDis);
holder.unitBtn = row.findViewById(R.id.unitbtn);
holder.stockTxt = row.findViewById(R.id.stock);
clickUnitBtn(holder);
if(Constants.isPriceEdit){
holder.price = row.findViewById(R.id.productprice);
holder.price.setClickable(false);
holder.price.setFocusable(false);
holder.price.setCursorVisible(false);
}else {
holder.price = row.findViewById(R.id.productprice);
holder.price.setSelectAllOnFocus(true);
}
row.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
}
});
row.setTag(holder);
setupItem(holder);
}
return row;
}
And here is a screenshot for reference
enter image description here
Are you able to dismiss alert dialog on your footer click as given on your code?
What is want is to update total number in textview in one activity. i have a custom adapter which calls Arraylist in this activity then populates in listview, this adapter also has image view which removes the list item and does notifydatasetchanged().
this is my customadapter
private ArrayList<DataModel> dataSet;
Context mContext;
private static class ViewHolder {
TextView txtName;
TextView txtType;
Button remove;
}
public CustomAdapterForData(ArrayList<DataModel> data, Context context) {
super(context, R.layout.fields, data);
this.dataSet = data;
this.mContext=context;
}
#Override
public void onClick(View view) {
int position = (Integer) view.getTag();
Object object = getItem(position);
DataModel dataModel = (DataModel) object;
switch (view.getId())
{
case R.id.btnRemove:
remove(dataModel);
// dataSet.remove(position);
//dataSet.remove(position);
notifyDataSetChanged();
break;
}
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
DataModel dataModel = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.fields, parent, false);
viewHolder.txtName = (TextView) convertView.findViewById(R.id.fieldName);
viewHolder.txtType = (TextView) convertView.findViewById(R.id.tvPrize);
viewHolder.remove = (Button) convertView.findViewById(R.id.btnRemove);
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
viewHolder.txtName.setText(dataModel.getFieldName());
viewHolder.txtType.setText(dataModel.getType());
viewHolder.remove.setOnClickListener(this);
viewHolder.remove.setTag(position);
return convertView;
}
after that this is my onstart()
protected void onStart() {
//if ((dataModels.size()!=0)){
ListView listView = (ListView) findViewById(R.id.listView);
adapter = new CustomAdapterForData(dataModels, getApplicationContext());
listView.setAdapter(adapter);
dataModelsnew = dataModels;
TextView tv = (TextView) findViewById(R.id.textViewTotal);
double sum = 0;
for (int i = 0 ; i < dataModelsnew.size(); i++){
sum = sum + dataModelsnew.get(i).getPrize();
}
tv.setText("Total : " + String.valueOf(Math.round(sum)*100d/100d));
//}
super.onStart();
}
what i want is populate latest value in textview which is sum of double (one element of ArrayList).Please help me to resolve this.
Thanks in advance. The variable i want to update after i remove something from list view otherwise it is working fine.
You can use DataSetObservers to detect any changes in your data:
final TextView tv = (TextView) findViewById(R.id.textViewTotal);
adapter.registerDataSetObserver(new DataSetObserver() {
#Override
public void onChanged() {
super.onChanged();
double sum = 0;
for (int i = 0 ; i < dataModels.size(); i++){
sum = sum + dataModels.get(i).getPrize();
}
tv.setText("Total : " + String.valueOf(Math.round(sum)*100d/100d));
}
});
I need to open a frameLayout details into a listView, but when I press the buttonDetails, it opens a wrong frameLayout.
here is the code of my adapterView
public class SitesAdapter extends ArrayAdapter<AtlantisSite> {
ImageLoader imageLoader;
DisplayImageOptions options;
FrameLayout frameLayout;
public SitesAdapter(Context ctx, int textViewResourceId, List<AtlantisSite> sites) {
super(ctx, textViewResourceId, sites);
//Setup the ImageLoader, we'll use this to display our images
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(ctx).build();
imageLoader = ImageLoader.getInstance();
imageLoader.init(config);
//Setup options for ImageLoader so it will handle caching for us.
options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.build();
}
/*
* (non-Javadoc)
* #see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
*
* This method is responsible for creating row views out of a AtlantisSite object that can be put
* into our ListView
*/
#Override
public View getView(final int pos, final View convertView, final ViewGroup parent){
RelativeLayout row = (RelativeLayout)convertView;
Log.i("AtlantisSites", "getView pos = " + pos);
//ViewHolder mainViewHolder = null;
final ViewHolder viewHolder;
if(null == row){
//No recycled View, we have to inflate one.
LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = (RelativeLayout)inflater.inflate(R.layout.row_site, null);
viewHolder = new ViewHolder();
viewHolder.btnDownload = (Button) row.findViewById(R.id.btnDownload);
viewHolder.btnDownload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Integer pos=(Integer)v.getTag();
Log.i("AtlantisSites", "getView pos = " + pos);
//String url = getItem(pos).getLink();
DownloadZip zipActivity = new DownloadZip();
zipActivity.DownloadFromUrlZip();
//Intent i = new Intent(Intent.ACTION_VIEW);
//i.setData(Uri.parse(url));
//Intent zipActivity = new DownloadZip();
//zipActivity.start...;
//Toast.makeText(getContext(), "Downloading " + pos, Toast.LENGTH_SHORT).show();
}
});
viewHolder.btnDetails = (Button) row.findViewById(R.id.btnDetails);
viewHolder.btnDetails.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Integer pos = (Integer) v.getTag();
//Log.i("AtlantisSites", "getView pos = " + pos);
getItemId(pos);
if(frameLayout.getVisibility()==View.GONE){
showFrameLayout();
} else {
hideFrameLayout();
}
}
});
row.setTag(viewHolder);
} else{
viewHolder = (ViewHolder) row.getTag();
}
//Get our View References
final ImageView iconImg = (ImageView)row.findViewById(R.id.iconImg);
TextView nameTxt = (TextView)row.findViewById(R.id.nameTxt);
TextView titleTxt = (TextView)row.findViewById(R.id.titleTxt);
//Button btnDownload = (Button)row.findViewById(R.id.btnDownload);
TextView summaryIta = (TextView)row.findViewById(R.id.textViewSummaryItaRow);
TextView summaryEng = (TextView)row.findViewById(R.id.textViewSummaryEngRow);
TextView priceItaTxt=(TextView)row.findViewById(R.id.txtViewPriceIta);
TextView priceEngTxt=(TextView)row.findViewById(R.id.txtViewPriceEng);
final ProgressBar indicator = (ProgressBar)row.findViewById(R.id.progress);
frameLayout = (FrameLayout)row.findViewById(R.id.frame_layout_listview);
//Set the relavent text in our TextViews
nameTxt.setText(getItem(pos).getName());
titleTxt.setText(getItem(pos).getTitle());
summaryIta.setText(getItem(pos).getSummaryIta());
summaryEng.setText(getItem(pos).getSummaryEng());
viewHolder.btnDetails.setTag(pos);
return row;
}
public class ViewHolder{
Button btnDownload;
Button btnDetails;
}
private void showFrameLayout(){
frameLayout = (FrameLayout) frameLayout.findViewById(R.id.frame_layout_listview);
frameLayout.setVisibility(View.VISIBLE);
}
private void hideFrameLayout(){
frameLayout = (FrameLayout) frameLayout.findViewById(R.id.frame_layout_listview);
frameLayout.setVisibility(View.GONE);
}
}
Something goes wrong with the position.
please help me to find error o solutions.
thanks
in the method showFrameLayout() you dont have any reference to the current frame layout you want to show, remove the methods show/hide frameLayout and inside the listener for the details btn
viewHolder.btnDetails.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Integer pos = (Integer) v.getTag();
//Log.i("AtlantisSites", "getView pos = " + pos);
getItemId(pos);
FrameLayout detailsFrame = row.findViewById(R.id.frame_layout_listview);
if(detailsFrame.getVisibility()==View.GONE){
detailsFrame.setVisibility(View.VISIBLE);
} else {
detailsFrame.setVisibility(View.GONE);
}
}
});
As a general suggestion i would put my FrameLayout inside the viewholder, wich is exactly whats for.
I found the solution . I have to declare the Frame Layout final. It works
final FrameLayout detailsFrame = (FrameLayout)row.findViewById(R.id.frame_layout_listview);
I have the following code:
static class ViewHolder {
TextView camera;
TextView players;
TextView max_players;
ImageView privata;
Button Buton;
}
ViewHolder holder;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
String variabile[] = getItem(position).split("\\s+");
if(convertView == null)
{
LayoutInflater linflater = LayoutInflater.from(getContext());
convertView = linflater.inflate(R.layout.custom_row, parent, false);
holder = new ViewHolder();
holder.camera = (TextView) convertView.findViewById(R.id.Nume);
holder.players = (TextView) convertView.findViewById(R.id.players);
holder.max_players = (TextView) convertView.findViewById(R.id.max_players);
holder.privata = (ImageView) convertView.findViewById(R.id.privata);
holder.Buton = (Button) convertView.findViewById(R.id.Buton);
holder.camera.setText(variabile[0]);
if (!variabile[1].equals("true")) {
parola = false;
holder.privata.setVisibility(View.INVISIBLE);
}
holder.players.setText(variabile[2]);
holder.max_players.setText(variabile[3]);
room_id = variabile[4];
nume = variabile[5];
holder.Buton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
hash = new HashMap<String, String>();
hash.put("name", nume);
hash.put("room", room_id);
if (intra) {
holder.Buton.setText("Iesi");
site = siteul + "/join";
intra = false;
} else {
holder.Buton.setText("Intra");
site = siteul + "/leave";
intra = true;
}
new ATask().execute(site);
}
});
convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();
return convertView;
}
I'm trying to access a row from an AsyncTask on PostExecute and modify it like this:
TextView players_mare = holder.players;
players_mare.setText(rez.substring(2));
No matter the button pressed it seems to modify the textview of the last item in the list.
This is happening because you are using holder. After rendering complete ListView it has reference of the last item in the ListView, so only last item is updated irrespective of the item on which button was clicked. You need to pass the reference of item in which button was clicked to the ATask class.
Add
holder.Buton.setTag(holder);
after
convertView.setTag(holder);
in getView() method
Create a class level variable in ATask clas like this
ViewHolder myHolder;
Add this in ATask
public ATask(ViewHolder view) {
myHolder = view;
}
Change ATask call like this
new ATask((ViewHolder) v.getTag()).execute(site);
In PostExecute replace every holder with myHolder
I have a custom ListView. This ListView contain of 1 Image and 3 TextView. How can I click on ImageView and get current position. (Do not click to other elements, only ImageView)
In your custom adapter class, you are havinggetView(.. .. ..) method :
#Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
// >>> >>> ^^^^^^ This is your posotion = index
View row = convertView;
Your_Holder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(resId, parent, false);
holder = new Your_Holder();
....
....
holder.yourImageView = (ImageView) row.findViewById(R.id.yourImageID);
....
....
row.setTag(holder);
}
else{
holder = (Your_Holder)row.getTag();
}
// Set here your images click event
holder.yourImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Position = " + position)
}
});
}