How to show Tapped Product Information in WishProductDetails.java - android

Like: we see in Cart Apps, when user selects some item(s) and at very last stage he/she wants to change quantity of an item, here we allow user to Tap on Item in List View to update quantity of an item, and once user tap on item in list view, we show him/her existing detail of an item, which has been tapped....in a same way i want to allow user to tap on item and want to show him existing detail for his item....Just want to show existing product information which has been tapped by user in WishProductDetails.java
Still i am able to show WishProductDetails.java but not able to show Tapped Item Details in Activity..
I am using below code to show existing Item details in WishProductDetails.java which i have clicked in Cart Activity using List View Item row...
HashMap<String, String> item = Constant.wishProducts.get(position);
Log.d("CartAdapter", "onClick :: " + item);
Intent myIntent = new Intent
(activity, WishProductDetails.class);
Log.d("CartAdapter", "Intent :: " + myIntent);
myIntent.putExtra("Item", item);
activity.startActivity(myIntent);
Work related to add item into Cart and accept quantity of an item, all such works i am doing WishProductDetails.java
Now i want whenever user do click on any of the ListView Item row, i need to show that particular item in WishProductDetails.java activity along with existing details.

I am guessing that you are using an ImageButton to remove an item from cart, i have never worked on this kind of project but i am writing what i think, like:
mImgBtnDelete = (ImageButton) vi
.findViewById(R.id.mImgBtnDelete);
mImgBtnDelete.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Constant.wishproducts.remove(position);
notifyDataSetChanged();
}
Edit #2
Code to Update an Item using on ListView Item Row
I think in your Adapter class, you should add code something like below, to update quantity of an item while click on item row, but very frank i don't know how to open that particular item in WishProductDetail.java (where you are allowing user to enter quantity)
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.cart, null);
vi.setClickable(true);
vi.setFocusable(true);
vi.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
HashMap<String, String> prod = Constant.wishproducts.get(position);
Intent mViewCartIntent = new Intent
(activity,ProductInformationActivity.class);
mViewCartIntent.putExtra("product", prod);
activity.startActivity(mViewCartIntent);
}
});

Related

Reset row in listview Android

In ListView, when i selected row it is set background color with reset previous selected one. but when listview scoll down it shows other row selected same backgound means one bye one page scroll down the row is selected.
Plz help me
In this two images i selected only first row but when i scroll the page 23 row is already selected. How to set selected row with scrollable page?
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setBackgroundResource(R.drawable.shape);
// on seleting single voter
// launching Edit Product Screen
lv.setOnItemClickListener(new OnItemClickListener() {
//mOnDoubleTapListener = listener;
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
boolean netFlag = new validationClass().haveNetworkConnection(context);
if(!netFlag) {
Toast.makeText(getApplicationContext(), "Data Connection is not available.", Toast.LENGTH_LONG).show();
return;
}
if (currentSelectedView != null && currentSelectedView != view) {
TextView textViewName = (TextView) currentSelectedView.findViewById(R.id.name);
TextView textViewPart = (TextView) currentSelectedView.findViewById(R.id.partno);
textViewName.setTextColor(getResources().getColor(android.R.color.darker_gray));
textViewPart.setTextColor(getResources().getColor(android.R.color.darker_gray));
} currentSelectedView =view
TextView textViewName = (TextView) view.findViewById(R.id.name);
TextView textViewPart = (TextView) view.findViewById(R.id.partno);
textViewName.setTextColor(getResources().getColor(R.color.text_color));
textViewPart.setTextColor(getResources().getColor(R.color.text_color));
}
}
There are two says to solve this:
A press/click on an item just sets the item as selected, and the user needs to click a button to act on the item.
Since your listview is fullscreen, have the onClick() record the position of the selected item. On a subsequent click, compare the current position to the last. If they aren't the same, just record the new position. If they are the same, go ahead with the operation. Or you could require the user to do a longclick (onItemLongClick()) to act on the selection if the current position is the same as the last.
Either way can help your app avoid detecting a click when the user is really trying to scroll. Android won't call the onClick() if the user starts scrolling right away. You can't control this.

listview getting new instance when swiping the listview

I have one listview in my application,it contains two rows one for task and another one for alarm,date,severity. Initially first row of the list item only displayed for all list item and the second one is invisible. When i click the list item the second row displayed for that item as well as click another list item at that time the above list item closed that second row. Its working fine for me...My problem is if i open one list item and then swipe the listview at then i click the another list item at that time the above one cannot be closed because the above list item instance will be chnaged.please any one help me how to solve this problem...
int lastselectedPosition == -1
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
TextView textviewDate=(TextView)view.findViewById(R.id.taskTimeidDaytoDay);
selectedtaskDate=textviewDate.getText().toString().trim();
if (lastselectedPosition == -1) {
Log.i(TAG,"Loopif:"+lastselectedPosition);
TextView twTaskTime = (TextView) view
.findViewById(R.id.taskTimeidDaytoDay);
TextView twSeverity = (TextView) view
.findViewById(R.id.severityidDaytoDay);
TextView twAlarm = (TextView) view
.findViewById(R.id.alarmidDaytoDay);
twAlarm.setVisibility(view.VISIBLE);
twSeverity.setVisibility(view.VISIBLE);
twTaskTime.setVisibility(view.VISIBLE);
lastselectedPosition = position;
lastSelectedItem = arg0.getChildAt(position);
} else {
// Log.i(TAG,"LoopElse:"+lastselectedPosition);
lastSelectedItem.findViewById(R.id.taskTimeidDaytoDay)
.setVisibility(lastSelectedItem.GONE);
lastSelectedItem.findViewById(R.id.severityidDaytoDay)
.setVisibility(lastSelectedItem.GONE);
lastSelectedItem.findViewById(R.id.alarmidDaytoDay).setVisibility(
lastSelectedItem.GONE);
if (lastselectedPosition != position) {
view.findViewById(R.id.taskTimeidDaytoDay).setVisibility(
view.VISIBLE);
view.findViewById(R.id.severityidDaytoDay).setVisibility(
view.VISIBLE);
view.findViewById(R.id.alarmidDaytoDay).setVisibility(
view.VISIBLE);
lastselectedPosition = position;
lastSelectedItem = arg0.getChildAt(position);
} else {
lastselectedPosition = -1;
lastSelectedItem = null;
}
}
GetView():
#Override
public View getView(int position, View view, ViewGroup parent) {
Log.i("XXXX", "Inside getView");
final DaytoDayTaskGetterSetter objDaytoDaygetset=getItem(position);
TextView textviewTask;
TextView txtviewAlarm ,txtviewTaskTime ,txtviewSeverity;
Log.i(TAG,"InsideGetView:"+position);
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if(view==null)
{
view=inflater.inflate(R.layout.daytodaylistlayout,null);
}
Log.i("XXXX", "before first test");
textviewTask=(TextView)view.findViewById(R.id.tasknameidDaytoDay);
txtviewAlarm=(TextView)view.findViewById(R.id.alarmidDaytoDay);
txtviewSeverity=(TextView)view.findViewById(R.id.severityidDaytoDay);
txtviewTaskTime=(TextView)view.findViewById(R.id.taskTimeidDaytoDay);
return view;
}
In first i click the "gdfgdtet" list item it show another row and then i click the second list item "dfgsdgsd" at that time the above list item "gdfgdtet" closed the second row.This is a normal output.Suppose if i open the "gdfgdtet" list item and then swipe the listview at that time both of "gdfgdtet" "dfgsdgsd" will be opened and crashed...because the above one list item reference changed when i am swiping please how to solve this problem...
I'll try to provide you a good answer that explains why you are having this problems, but the general idea is that you have to see this video - http://www.youtube.com/watch?v=wDBM6wVEO70
please take my words kindly - you don't seems to understand what ListView + BaseAdapter recycling mechanism is all about, and I strongly recommend you see the full video I linked you to, and read more about that.
in general, the specific problem in your code is that you are holding reference to listview item (lastSelectedItem), then trying to use it latter assuming it's still representing same list item. that's wrong. in that stage (after scrolling) the view already been recycled to represent another item in the list (based on the adapter implementation).
listView's number of childs is not the size of adapter.getCount()!!!!!!!!
listViews's number of childs = number of visible list items on screen + 1 + headers + footers
let's say you have the 5 first items visible on screen, then you are scrolling down. when you see the 7 item you actually see the same view instance that used to show the first list item and been recycled.
getView will call in this stage with convertView != null and position in the adapter to let you reuse the item by putting new values such different text/image to the same instance
this mechanism provides ability to display list of "infinite" number of items in the list, and holding in memory only a few number of views. imagine that you have list of 5000 items in the list, and each one of them have different view instance - you would get outOfMemory exception in a sec!
complete explanation about that would be hard to write in stackoverflow answer's context.
it just too long trying to explain one of the most important and complex UI components in android, but this links would be a good start:
http://www.youtube.com/watch?v=wDBM6wVEO70
How ListView's recycling mechanism works
http://mobile.cs.fsu.edu/the-nuance-of-android-listview-recycling-for-n00bs/
if you are interstead in "quick" fix for your specific problem, the solution would be:
hold in the data structure represents your list item additional field indicating if it in "close" or "open state. when item been clicked - change the data accordinly and call notifyDatasetChanged(). inside the getView() check if item is open or close and populate it accordinly
by the way - it's not only "quick fix" solution, but also the right thing to do anyway
You should pay attention to Tal Kanel's answer and consider this one to be an extension to it. His advice will help you in the long run.
Add a boolean field to DaytoDayTaskGetterSetter class:
public class DaytoDayTaskGetterSetter {
....
....
boolean open;
public DaytoDayTaskGetterSetter (.., .., boolean o) {
....
....
open = o;
}
....
....
public boolean shouldOpen() {
return open;
}
public void setOpen(boolean o) {
open = o;
}
}
In your getView(), check if the object has its open value set:
DaytoDayTaskGetterSetter obj = (DaytoDayTaskGetterSetter) getItem(position);
if (obj.shouldOpen()) {
// Set visibility to true for the items
} else {
// Set visibility to false for the items
}
On list item click, traverse the list and set open for all list items to false. Use the position to retrieve DaytoDayTaskGetterSetter and set its open to true:
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
for (DaytoDayTaskGetterSetter obj : listContainingObjects) {
obj.setOpen(false);
}
DaytoDayTaskGetterSetter clickedItem = (DaytoDayTaskGetterSetter)
yourAdapter.getItem(position);
clickedItem.setOpen(true);
yourAdapter.notifyDataSetChanged();
}
Edit 1:
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
DaytoDayTaskGetterSetter clickedItem = (DaytoDayTaskGetterSetter)
yourAdapter.getItem(position);
if (clickedItem.shouldOpen()) {
clickedItem.setOpen(false);
} else {
for (DaytoDayTaskGetterSetter obj : listContainingObjects) {
obj.setOpen(false);
}
clickedItem.setOpen(true);
}
yourAdapter.notifyDataSetChanged();
}

How to change other items in a Android ListView when one item is clicked

I have a ListView that contains items with checkboxes that should behave sometimes like a CHOICE_MODE_MULTIPLE and sometimes like a CHOICE_MODE_SINGLE. What I mean is for certain items in the list, when selected certain other items needs to be deselected whilst other can remain selected.
So when item A is checked I can find in my data the item B that needs to be unchecked but how do I get the UI to refresh to show this as I (I believe) cannot find the actual View that represents B but just it's data?
It sounds like you're off to a good start. You're right that you should be manipulating the underlying data source for item B when A is clicked.
Two tips that may help you:
Your getView() method in the Adapter should be looking at your data source and changing convertView based on what it finds. You cannot find the actual View that represents B because in a ListView, the Views are recycled and get reused as different data needs to be displayed. Basically, when an item is scrolled off the list, the View that was used gets passed to the getView() function as convertView, ready to handle the next element's data. For this reason, you should probably never directly change a View in a ListView based on user input, but rather the underlying data.
You can call notifyDataSetChanged() from within your adapter to signal that somewhere the underlying data has been changed and getView() should be called again for the elements currently displayed in your list.
If you're still having trouble, feel free to post some code that illustrates the specific problem that you're having. It's much easier to provide concrete advice when the problem is better defined. Hope this helps!
you can use singleChoice alartDialog, i have used like:
private int i = 0; // this is global
private final CharSequence[] items = {"Breakfast", "Lunch", "Dinner"}; // this is global
Button settings = (Button)view.findViewById(R.id.settings);
settings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
//Title of Popup
builder.setTitle("Settings");
builder.setSingleChoiceItems(items, i,
new DialogInterface.OnClickListener() {
// When you click the radio button
public void onClick(DialogInterface dialog, int item){
i=item;
}
});
builder.setPositiveButton("Confirm",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (i == 0) {
//it means 1st item is checked, so do your code
}
if (i == 1) {
//it means 2nd item is checked, so do your code
} /// for more item do if statement
}
});
//When you click Cancel, Leaves PopUp.
builder.setNegativeButton("Cancel", null);
builder.create().show();
}
});
i have initialized i=0, so that for the very first time when user click on settings button, the first item is selected. and after then when user select other item, i have saved the i value so that next time when user click settings button, i can show user his/her previously selected item is selected.
I come across and solve this question today.
public class ItemChooceActivity extends Activity implements OnItemClickListener {
private int chosenOne = -1;
class Madapter extends BaseAdapter {
.....
.....
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
if (chosenOne != position) {
set the view in A style
} else {
set the view in B style
}
return convertView;
}
}
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long arg3) {
,,,,
chosenOne = position;
adapter.notifyDataSetChanged();
,,,
}
}

Custom Listview Index automatically changes

I've listview on which when a user clicks an item I show an image that user has clicked this item.The listview gets populated from notifications.
Suppose an item gets added in listview.Now I click that item and image is shown in front of it.Now when I enter the new item in the listview nothing will happen(there are 2 items in the listview now). When I add the third item in the listview the image from the item at position 0 hides and is shown in position 1. Why this is happening?
Now to add an image in the listview item. I have to first perform some checks on the next opening activity and if those checks are successful then I place an image in front of that clicked item in the activity.
So to do this what i did is I am sending the clicked item position in the next activity and perform those checks, if those checks are successful I place and image on the onActivityResult function of the previous activity by using that position which I sent to this activity.Meaning that I send that position value back to the previous activity in the onActivityResult function where I place the Image.
The placement of image works fine.But while I recieve notification I add the item in the listview which results in the getView function being called for each item in the listview again to populate the listview.Here the problem occurs i.e the image from the position 0 item in the list goes to the position 1 item.
Please ask if more explanation is needed or something is not clear.
Here's the code for listview Item Click:
TextView tv=(TextView)view.findViewById(R.id.clientip);
Intent i=new Intent(getApplicationContext(),ChatPageActivity.class);
i.putExtra("AdminEmail",ClientListActivity.AdminEmail);
i.putExtra("ClientIp",tv.getText().toString());
i.putExtra("ChildAt", position);
startActivityForResult(i, 200);
ChildAt is the position I am sending.
After performing checks in the next activity.
#Override
protected void onActivityResult(int requestCode,int resultCode,Intent data)
{
try {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode!= 23)
{
Integer integer=data.getExtras().getInt("ChildAt");
ImageView imageView=(ImageView)lv.getChildAt(integer).findViewById(R.id.onlineImg);
imageView.setImageResource(R.drawable.online);
}
}catch(Exception e)
{
Log.e("ClientList_onActivityResult",e.getMessage());
}
}
This is the onActivityResult if the code is not equal to 23 it means all the checks in the next activity are correct and you can place the image at the position which is sent back again to this activity from the next activity.
Here's the getView code of my BaseAdapter class:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=convertView;
if(convertView==null)
view=inflater.inflate(R.layout.activeclientlist,null);
TextView ip=(TextView)view.findViewById(R.id.clientip);
String clientip=this.iplist.get(position);
ip.setText(clientip);
return view;
}
In the baseADapter I insert item in existing listview using this piece of code..
this.iplist.add(ip);
this.notifyDataSetChanged();
Important:
Also I wanted to mention that this behaviour happens in a pattern i.e as i've told above the image position changes when i insert the third item.After that it changes again when I'll add the fifth item and so on.
You should put the code to update the image view within the adapter itself.
The adapter should also be knowing all the info he needs to draw the data. So instead of having it store a list of Strings, store a list of objects like:
class Client {
String ipAddress;
boolean isConnected;
}
You then have to change your adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=convertView;
if (convertView==null) {
view=inflater.inflate(R.layout.activeclientlist,null);
}
TextView ip=(TextView)view.findViewById(R.id.clientip);
ImageView status = (ImageView )view.findViewById(R.id.status);
Client c = getItem(position);
ip.setText(c.getIpAddress());
status.setImageDrawable(c.isConnected() ? R.drawable.ic_status_connected : 0);
return view;
}
Then just change the code to update the image when the connection status changes:
if (resultCode!= 23) {
Integer i = data.getExtras().getInt("ChildAt");
((Client)adapter.getItem(i)).isConnected = true;
adapter.notifyDataSetChanged();
}
This way you keep all the code related to representing your data within the adapter. Side effect, it should be correcting your code.
Also have a look at the ViewHolder pattern to avoid doing findViewById on each row when you have a convertView available.

How to get the previous item of clicked listviewitem

I have two activities. In activity1 , I have list view which is populated from the database. On item click it should go to activity2. Activity2 contains two buttons (next and previous) and display the product details. In stead of "next" and "previous" button text, i was trying to get the previous item of clicked listview item and set text in button. and so for the "next" button.
myList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
MyClass item = (MyClass) adapter.getItem(position-1);
Intent i = new Intent(this, Activity2.class);
i.putExtra("item",item.toString());
startActivity(Activity2);
}
}
and in activity2.class
buttonprevious = (Button) findviewById(R.id.previous);
Bundle b = getIntent().getExtras();
String preitem = b.getString("item");
buttonprevious.setText(preitem);
Is this the correct way or am i doing something wrong??
How can I display product details of next and previous items??
How to use the view flipper in this case?
Thanks ..
You can use getItem(int position) method of the adapter, which return you the list item of the specified position.
(OR)
Get and Store all the database data into one static List.
Set adapter with that list.
In Activity2 access data from that list.

Categories

Resources