i have a listview in my widget that have a button in each row .
i want to visible a view when button clicked .
how i can found which row s button is clicked ?
#Override
public RemoteViews getViewAt(int i) {
// Log.d("MyWidgetViewsFactory", "getViewAt(" + position + "):" + items.get(position));
// Item item = items.get(i);
RemoteViews itemView = new RemoteViews(context.getPackageName(), R.layout.widget_listview_item);
if(wordItems != null) {
itemView.setTextViewText(R.id.ques, wordItems.get(i).getWord());
itemView.setTextViewText(R.id.answer, wordItems.get(i).getMean());
Log.d("word item size", wordItems.size() + " -- ");
itemView.setViewVisibility(i , View.VISIBLE);
// -- how can find which rows is clicked and how visible a layout
Intent intent = new Intent();
itemView.setOnClickFillInIntent(R.id.seeasnwer, intent);
}
return itemView;
}
Updated :
as you see in screen shot i want when i clicked "show answer" button a view set visible
Through ViewHolder concept, we can achieve it..
or
Provide the position for button by using setTag method. Whenever click the button, get the position of that button by using getTag method and play with item what ever you want.
Related
Now I'm using onItemLongClick to delete.
Each listview item have delete button.
I want to delete using the button.
The below code is my onItemLongClick code.
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Aview item = mAviewList.get(position);
mAviewList.remove(position);
aviewDB.execSQL("DELETE FROM " + tableName + " WHERE id = '" + item.getId() + "';");
getAviewData(materialCalendarView.getCurrentDate().getDate());
return true;
}
Typically, the way this is done is that you define an XML layout file for your list item that includes a delete button (you can hide the delete button by default and only reveal it if the user swipes left if you want to be fancy). In your custom list adapter, when you inflate the layout, you will get the Button from the layout and set an onClickListener for that button that uses the Position passed to the getView() method of the adapter. Here is an example that would go in the getView() method of your adapter:
Button btnDelete = (Button) convertView.findViewById(R.id.trip_delete_in_list);
btnDelete.setTag(position);
btnDelete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Integer position = (Integer)v.getTag();
fragment.deleteItemList(position);
}
});
btnDelete.setVisibility(View.GONE);
I just needed help with a tricky situation in listview click listner. The thing is that I have populated a listview with custom adapters from remote servers. My listview contains 4 elements 2 images and 2 textview so the listview is loading perfectly from database but the issue is that I want listview to work only if the certain conditions match. Have a look at my code:
if (convertView == null) {
convertView = inflater.inflate(resource, null);
}
ImageView team1_photo,team2_photo;
TextView tournament;
team1_photo = (ImageView) convertView.findViewById(R.id.team1);
team2_photo = (ImageView) convertView.findViewById(R.id.team2);
tournament=(TextView) convertView.findViewById(R.id.tournament);
status=(TextView) convertView.findViewById(R.id.status);
ImageLoader.getInstance().displayImage(live_conveyerList.get(position).getTeam1_photo(), team1_photo);
ImageLoader.getInstance().displayImage(live_conveyerList.get(position).getTeam2_photo(), team2_photo);
tournament.setText(live_conveyerList.get(position).getTournament());
status.setText(live_conveyerList.get(position).getStatus());
liveListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
if(status.getText().equals("play on"))
{
Toast.makeText(getActivity(),"Result will be declared shortly",Toast.LENGTH_LONG).show();
}
else {
Intent fix1 = new Intent(getActivity(), Leaderboard.class);
startActivity(fix1);
}
break;
case 1:
if(status.getText().equals("play on"))
{
Toast.makeText(getActivity(), "Result will be declared shortly", Toast.LENGTH_LONG).show();
}
else{
Intent fix2 = new Intent(getActivity(), Leaderboard_1.class);
startActivity(fix2);
}
break;
default:
}
getActivity().overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
}
}
);
return convertView;
}
}
So here, status is one of the textviews and it has two type of outputs. One is "play on" and other is "play off" so I want to open new activity only when it has populated play on in that peculiar row. Els,e there should be a toast only,
but it is not working for me. Any help would be nice, thanks.
my json is like this
{"list":[{"tournament":"Xavier college League","team1_photo":"http:\/\/thehostels.in\/judgement_files\/images\/India.png","team2_photo":"http:\/\/thehostels.in\/judgement_files\/images\/Australia.png","status":"play on"},{"tournament":"ITM college League","team1_photo":"http:\/\/thehostels.in\/judgement_files\/images\/India.png","team2_photo":"http:\/\/thehostels.in\/judgement_files\/images\/Australia.png","status":"play off"}]}
In your case 0 you have "Play on" with a capital P, but equals takes the case into account by default, could it be your issue?
Edit : Ok I think I got it
You define the onItemClickListener not once but for each and every item, so that at the end of list population, your listener is based on the status of the last item of your list
You should define onItemClickListener only once, and you will have clicked item with view and position arguments
For instance you may want to store the list of all your items as a field, and in your onItemClickListener you will get clicked item from the position, and thus get its status
With the view parameter, you should be able to access the fields' values through findViewById
Edit 2:
What I meant is the following : there is only one onItemClickListener that needs to be defined and onItemClick method will provide the position and the view of clicked item.
That means that each time user will click any item of the list, the listener will be called, but there is not one different listener for each different row.
With that in mind, in the onItemClick, with the view and/or the position, you can access the status textview, get its value and then do all the stuff you need to do with your toast and intent. I hope it's clearer than before
Edit 3:
That should do your thing : in your listener, before your switch,
TextView currentStatus=(TextView) convertView.findViewById(R.id.status);
And then you replace all the references to status inside your listener by currentStatus. And you will need to define the listener only once outside of your getView method, even outside your adapter, rather where your listView is defined and your adapter is added to it
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.
Is there a way to call notifyDataSetChanged() on a custom adapter without refreshing the list or disturbing the UI?
I have a ListView with a custom Adapter behind it, using a List of Guest objects as its dataset. When a Guest marks his attendance by tapping on his name, a tick is supposed to appear next to the guest's name in the UI. This I can do, but when I call notifyDataSetChanged(), the list of names is pushed all the way to the top, presumably because the list "refreshes".
If I don't call notifyDataSetChanged(), however, the tick disappears when I scroll past the updated entry and scroll back again. This is due to the ListView's "recycling" of Views as I understand, but it sure doesn't make my job any easier.
How would one call notifyDataSetChanged() without making the entire ListView refresh itself?
Better to have one boolean field in your Guest Class :isPresent.
whenever user taps on list item you can get the selected item using adapter.getItemAtPosition().
update the value isPresent to true. and make show the tick mark.
In your adapter class. check for isPresent value. If it is marked to true then show the tick mark else hide it.
This is how you can achieve the both. Show Tick Mark on ListItem click and if you scroll the listview and come back to the same item you tickmark show/hide will be taken care by Adapter.
you could retain the position like this
// save index and top position
int index = mList.getFirstVisiblePosition();
View v = mList.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
// ...
// restore
mList.setSelectionFromTop(index, top);
Actually possible if you don't want to make a "selected item" here is the code
public void updateItem(ListView listView, Activity activity) {
if (mData == null) return;
DebugLog.i("A", "firstCell: " + listView.getFirstVisiblePosition() + " lastCell: " + listView.getLastVisiblePosition());
for (int firstCell = listView.getFirstVisiblePosition(); firstCell <= listView.getLastVisiblePosition(); firstCell++) {
final DataItem item = (DataItem) getItem(firstCell); // in this case I put the this method in the Adapter and call it from Activity where the adapter is global varialbe
View convertView = listView.getChildAt(firstCell);
if (convertView != null) {
final TextView titleTextView = (TextView) convertView.findViewById(R.id.item_title);
// here is the most important to do; you have to use Main UI thread to update the view that is why you need activity parameter in the method
mActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
titleTextView.setText( item + " updated");
}
});
}
}
}
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);
}
});