I am using an custom listview in my application.In my row I have (two images,remaining textviews).
when I click on one of textview,I want to set another textview data.I wrote the code below to do this
likes.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// if(likes.getText().toString().equals("Like")){
TextView t=(TextView)v;
TextView likesnumber1 = (TextView) findViewById(R.id.likesnumber);
// TextView likesnumber = (TextView) convertView.findViewById(R.id.likesnumber);
int i= Integer.parseInt(likescount.get(position));
if(like_or_ulike.get(position).equals("Like")){
Log.e("inlike","like");
like_or_ulike.set(position, "Unlike");
t.setText(like_or_ulike.get(position));
UrltoValue.getValuefromUrl("https://graph.facebook.com/"+objectid.get(position)+"/likes?access_token="+accesstoken+"&method="+"post");
// listView.getAdapter().getItemAt(position);
j=i+1;
String s=Integer.toString(j);
likescount.set(position, s);
likesnumber1.setText(likescount.get(position));
}
else{
Log.e("unlike","unlike");
like_or_ulike.set(position, "Like");
t.setText(like_or_ulike.get(position));
UrltoValue.getValuefromUrl("https://graph.facebook.com/"+objectid.get(position)+"/likes?access_token="+accesstoken+"&method="+"DELETE");
j=i-1;
String s=Integer.toString(j);
likescount.set(position, s);
likesnumber1.setText(likescount.get(position));
}
}
});
How can I get the another textview id of particular row when I click the textview.Is it possible to get the ids of that particular row.
As you want to get hold of the textview in the same row, you can do the following.
with the textview that was clicked get its parent and then using the parentview you can search for your textview:
View parent = (View) textViewThatWasClicked.getParent();
if (parent != null) {
TextView textViewThatYouWantToChange = parent.findViewById(R.id.textViewThatYouWantToChange);
textViewThatYouWantToChange.setText(...);
}
you will need to change the id that you look for to the id you have for that other textview. If you need to set an id you can do this in the xml layout file you used.
I cant follow your code well enough to actually give you the code but this should be enough to get you in the right direction.
If you have custom layout for each row in listview then you can provide id for each of the View in your custom view and then set setOnItemClickListener for your listview and you'll be able to reach each row as a view.
When you have access to your row like a View then you can use view.findViewById and get all of your views from your custom view by their id.
Related
I have an android app in which users can like and unlike an image.I'm using recyclerView.I Just disable the button(Like/Unlike) once user clicked. Problem, when I click on button like , the apps go to main activity and the button Like doesn't change to unlike What I have done :
1 ) layout that holds the each recycler view layout item
2 ) A view holder for creating each layout
3 ) A Model Class to holds the data
4 ) Recycler Adaptor which deals with the data for the Each Layout item
Hier ist my view holder
//Initializing Views
public ViewHolder(View itemView) {
super(itemView);
imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
textViewName = (TextView) itemView.findViewById(R.id.textViewName);
//textViewPublisher = (TextView) itemView.findViewById(R.id.textViewPublisher);
likeImageView = (ImageView) itemView.findViewById(R.id.likeImageView);
likeImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int id = (int)likeImageView.getTag();
if( id == R.drawable.ic_like){
likeImageView.setTag(R.drawable.ic_liked);
likeImageView.setImageResource(R.drawable.ic_liked);
}else{
likeImageView.setTag(R.drawable.ic_like);
likeImageView.setImageResource(R.drawable.ic_like);
}
}
});
I answered this type of questions over and over. You didn't search enough and you didn't understand how ListView or RecycleView works. Changing current state of views ( such as changing text of TextView or changing resource of ImageView) is the wrong thing. You need data set (a list related to items in ListView) and you need to change corresponding data of the list and call notifyDataSetChanged() method of your adapter.
Don't forget. getView() method of your adapter is called every time any view of your list become on the screen and if you update only the view (instead of change data) your view will show the past value of item because your data didn't changed.
Look link below and search much more about how ListView and RecycleView works.
How ListView's recycling mechanism works
I am trying to display a delete button on every list item in my list view when the edit button is clicked. I am using setVisibility elsewhere in the app, so I tried to copy that code, but the issue in that the layout elements in the list items are not part of the layout xml file that the fragment implements, they are in a special one used by my CursorAdapter. I can find the desired hide-able elements using a layout inflator so I no longer get a nullPonterException, however the visibility does not change on button click like it should.
// Onclick method for Edit button
final Button buttonE = (Button) rootView.findViewById(R.id.editNotesButton);
buttonE.setTag(0);
//cannot use rootView as that points to fragment_main
final LayoutInflater factory = getLayoutInflater(savedInstanceState);
final View noteItemView = factory.inflate(R.layout.note_list_view_item, null);
final LinearLayout deleteButton = (LinearLayout) noteItemView.findViewById(R.id.delete_button_group);
final LinearLayout circleButton = (LinearLayout) noteItemView.findViewById(R.id.circle_button_group);
buttonE.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {
final int status =(Integer) v.getTag();
if(status == 1) {
buttonE.setText("Edit");
circleButton.setVisibility(View.VISIBLE);
deleteButton.setVisibility(View.GONE);
v.setTag(0); //pause
} else {
buttonE.setText("Done");
circleButton.setVisibility(View.GONE);
deleteButton.setVisibility(View.VISIBLE);
v.setTag(1); //pause
}
}
}
);
Firstly,I will figure out why your code doesn't work.You inflate noteItemView,but it doesn't bind to view in the screen,that is your 'noteitemview' won't show forever,so your delete button in that view will not visiable.
Then I will show my solution.
As you say,every list item has a delete button,what you should do is to control their visibility,so first make sure in your View binded to listview item has such a button as a childView of listview item view.In your getView of custom CursorAdapter,add logic to handle the delete button's visibility,for example,every list item data has a boolean variable named isDeleteButtonShow,then by control the variable's value to control the delete button's visibility,once visibility should change,update the data bind to listview and call adapter.notifyDataSetChanged.Hope that could help you.
Sorry for the long title.
I have an android application which shows a custom listView with 5 TextView columns. When the user clicks a row, I change the layout to have 3 TextViews and 2 EditTexts. I have different layout files for both of them. Everything worked fine initially, the row layout changes properly and I am able to click on the EditText and input values. However, I want either of the 2 EditText to automatically gain focus based on what is clicked. I already have a working code for this. My problem is that programatically requesting requestFocus() seems to block the part where I change the row layout with the new view.
Here is the code that changes my row layout, it works fine without the requestFocus() line:
private void changeLayout(final View view){
//get views from old row layout
TextView textViewQuantity = (TextView)view.findViewById(R.id.qtyInput);
TextView textViewDiscountReq = (TextView)view.findViewById(R.id.discInput);
TextView textViewName = (TextView)view.findViewById(R.id.dialogItemName);
TextView textViewPrice = (TextView)view.findViewById(R.id.price);
TextView textViewDiscount = (TextView)view.findViewById(R.id.discount);
//store values in strings
String itemName = textViewName.getText().toString();
String itemPrice = textViewPrice.getText().toString();
String itemDiscount = textViewDiscount.getText().toString();
String itemQty = textViewQuantity.getText().toString();
String itemDisc = textViewDiscountReq.getText().toString();
//set the view to gone
textViewQuantity.setVisibility(View.GONE);
textViewDiscountReq.setVisibility(View.GONE);
textViewName.setVisibility(View.GONE);
textViewPrice.setVisibility(View.GONE);
textViewDiscount.setVisibility(View.GONE);
//get the old layout
LinearLayout ll_inflate = (LinearLayout)view.findViewById(R.id.search_result_layout);
//get the inflate/new view
View child = getLayoutInflater().inflate(R.layout.search_result_inflate, null);
//get the views in the new view, populate them
TextView newName = (TextView)child.findViewById(R.id.dialogItemName);
newName.setText(itemName);
TextView newDiscount = (TextView)child.findViewById(R.id.discount);
newDiscount.setText(itemDiscount);
TextView newPrice = (TextView)child.findViewById(R.id.price);
newPrice.setText(itemPrice);
qtyIn = (EditText)child.findViewById(R.id.qtyInputSearchResult);
qtyIn.setText(itemQty);
qtyIn.setFilters(new InputFilter[] {filter});
discIn = (EditText)child.findViewById(R.id.discInputSearchResult);
discIn.setText(itemDisc);
//show new layout
ll_inflate.removeAllViews();
ll_inflate.removeAllViewsInLayout();
ll_inflate.addView(child);
//request focus here
if(focusTarget == 1){
Log.d("hello", "focus target is 1 " );
qtyIn.setFocusable(true);
qtyIn.setFocusableInTouchMode(true);
qtyIn.requestFocus();
}
else if(focusTarget == 2){
Log.d("hello", "focus target is 2 " );
discIn.requestFocus();
}
Log.d("hello", "focus state qtyIn = " + qtyIn.isFocused());
Log.d("hello", "focus state discIn = " + discIn.isFocused());
}
The interesting part is that the Log shows the proper values, it says the proper focus status according to what I want. However, the ll_inflate.addView(child); line does not work at all!
Does anyone know what happened here? I'm really confused as to why the layout didn't change but the lines after the .addView() line executed. Another weird thing is how requestFocus(); prevents the view from changing.
Any help is very much appreciated. Thanks.
You are doing this in wrong way. You should have two type of view in list, one for show data and one for edit it.
First set view type count
final int TYPE_EDIT =0,TYPE_VIEW =1;//EDIT 7/9/016 type need to be start from 0
public int getViewTypeCount (){
return 2;
}
public int getItemViewType (int position){
//return type as needed. simple if you add int for this in data model
}
And inside getView
public View getView (int position, View convertView, ViewGroup parent){
int type = getItemViewType ();
if(null==convertView){
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(type ==TYPE_EDIT){
convertView = inflater.inflate(R.layout.rowEditlayout, parent, false);
}else{
convertView = inflater.inflate(R.layout.rowlayout, parent, false);
}
}
}
Do the changes in list data on edit button click and call notifydatasetchanged on adapter
I basically have 3 different objects in my list view:
TextView1
TextView2
TextView3
I want to get the object ID from the list view that's created dynamically. Ex: How do I set TextView2 with a background image from a list view in position 1?
I've tried using
lv.getItemAtPosition(1);
This will return the whole row and I'm just looking for the object ID TextView2 inside lv.getItemAtPosition(1)? Once I get the objectID from a certain position in list view, I can than change the TextView2 background.
Sorry, if I didn't explain clear enough. Does anyone know what I'm talking about?
If you write about row, I assume something like that can work for you:
TableRow tableRow = lv.getItemAtPosition(1);
for (int i = 0; i < tableRow.getChildCount(); i++) {
View child = tableRow.getChildAt(i);
if ( child instanceof TextView ) {
TextView textView = (TextView) child;
textView.DO_SOMETHIG__WITH_TEXT_VIEV();
textView.requestLayout();
}
}
tableRow.requestLayout();
Of course if you have some other row than tableRow, you can try to change it to that type.
I have a ListView whose rows are formatted by me. Each row has a mix of ImageView and TextView.
I have also implemented my own adapter and am able to draw each row through it.
Now, I would want something like this-
User clicks on an ImageView (not anywhere else on the row, but only this ImageView should respond to clicks)
I get to know the position of the row whose ImageView was clicked.
I have tried many things for this and have wanted my code to be as efficient as possible (in terms of overkill).
Currently i can capture the click event on that particular ImageView only, but I can't know which row was clicked.
I have provided an attribute in the Row XML like this-
<ImageView android:id="#+id/user_image"
android:padding="5dip"
android:layout_height="60dip"
android:layout_width="60dip"
android:clickable="true"
android:onClick="uImgClickHandler"/>
And in my code, I have a method like this:
public void uImgClickHandler(View v){
Log.d("IMG CLICKED", ""+v.getId());
LinearLayout parentRow = (LinearLayout)v.getParent();
}
I can get the parent row (perhaps) but am not sure how to go further from here.
Can someone please help?
Please refer this,
Me just writing the code to give you idea, Not in correct format
class youaddaper extends BaseAdapter{
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflate = LayoutInflater.from(context);
View v = inflate.inflate(id, parent, false);
ImageView imageview = (ImageView) v.findViewById(R.id.imageView);
imageview.setOnClickListener(new imageViewClickListener(position));
//you can pass what ever to this class you want,
//i mean, you can use array(postion) as per the logic you need to implement
}
class imageViewClickListener implements OnClickListener {
int position;
public imageViewClickListener( int pos)
{
this.position = pos;
}
public void onClick(View v) {
{// you can write the code what happens for the that click and
// you will get the selected row index in position
}
}
}
Hope it helped you
Another option is to use the methods setTag() and getTag() of the view. You set it in your getView like this:
imageView.setTag(new Integer(position));
Then in the onClick() you can find the tag by:
Integer tag = v.getTag();
This will then be used to correlate the image view to the position of the listview item.
Note that this approach will give problems if the listview can lose items from the middle, so that the item positions change during the lifetime of the listview.
you can simply do like this:
in the getview method of our adapter
Button btn1 = (Button) convertView.findViewById(R.id.btn1);
btn1.setOnClickListener(mActivity);
further you can handle the onclick event in your activity,,
for the context of the activity here mActivity just pass the this in the constructer of the adapter and cast it here into the activity like
MyActivity mActivity=(MyActivity)context;
in the adapter.
thanx
This appears to work in a ListActivity whose item layout contains an ImageView with android:onClick="editImage":
public void editImage(View v) {
int[] loc = new int[2];
v.getLocationInWindow(loc);
int pos = getListView().pointToPosition(loc[0], loc[1]);
Cursor c = (Cursor) adapter.getItem(pos);
// c now points at the data row corresponding to the clicked row
}