Getting views of a custom view in android - android

Well I have a listview with an OnItemClickListener on it, each item of listview is a customview with two textviews, I want to get text of first textview when an item is clicked, So how can I do that? when an item is clicked I can just get the view that is clicked but how can I get textview on this customview?

Traverse that view's hierarchy:
TextView textView = (TextView) clickedView.findViewById(R.id.first_text_view);
clickedView is provided to you by OnItemClickListener.

yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
textFromFirstView = yourArray.get(position).getTextFromFirst();
}
});
whre getTextFromFirst() is getter for your textview for example

Related

onItemClickListener of ListView not working with RatingBar

I have a list item click listener (onItemClickListener) inside the overridden onViewCreated method of a fragment. The method body is given below:
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
final String uname = getActivity().getIntent().getExtras().getString("uname");
list = (ListView) getActivity().findViewById(R.id.drlistforhome);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Doctors doc = rh.drList.get(position);
imgURL = doc.getDrProPic();
docName = doc.getDrName();
jobTitle = doc.getDrJobTitle();
docSpeciality = doc.getDrSpeciality();
docUname = doc.getDrUname();
startActivity(new Intent(getActivity().getApplicationContext(), DoctorsProfileH.class).putExtra("img", imgURL)
.putExtra("name", docName)
.putExtra("title", jobTitle)
.putExtra("speciality", docSpeciality)
.putExtra("uname", uname)
.putExtra("druname", docUname));
}
});
}
The custom list view layout consists of six views: 4 TextViews, an ImageView and a RatingBar. The onItemClickListener was listening to list item clicks until the RatingBar was not added. But now that I have added a RatingBar in my custom layout, the listener wouldn't listen the clicks anymore. I am using a custom adapter to set all the views. Here is my custom adapter:
class TDsAdapter extends ArrayAdapter<Doctors> {
public TDsAdapter(){
super(getActivity().getApplicationContext(),R.layout.custom_list_view,rh.drList);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (itemView == null) {
itemView = inflater.inflate(R.layout.custom_list_view, parent, false);
}
Doctors currentItem = rh.drList.get(position);
TextView drName = (TextView) itemView.findViewById(R.id.drName);
drName.setText(currentItem.getDrName());
TextView drSpeciality = (TextView) itemView.findViewById(R.id.drSpeciality);
drSpeciality.setText(currentItem.getDrSpeciality());
TextView drJobTitle = (TextView) itemView.findViewById(R.id.drJobTitle);
drJobTitle.setText(currentItem.getDrJobTitle());
ImageView drProPic = (ImageView) itemView.findViewById(R.id.drPropic);
Picasso.with(getContext()).load(currentItem.getDrProPic()).into(drProPic);
TextView distence = (TextView) itemView.findViewById(R.id.dis);
distence.setText(currentItem.getDis());
RatingBar rb = (RatingBar) itemView.findViewById(R.id.ratingBar);
rb.setRating((float) 2.5);
return itemView;
}
}
After running the application the list view appears fine with the RatingBar as expected, but the list items are no longer clickable. When I debug the app with a break point inside the listener, that break point never gets executed. What I understand from my very little knowledge about fragments, this is probably because the onViewCreated is expecting all the views to be created first. But as we can see, all the views have successfully been created. How can I fix my problem? Please do not hesitate to ask for more specific information.
you can try with
android:descendantFocusability="blocksDescendants"
in root layout of your custom row item.
Write below code in your activity/fragment
listview.setItemsCanFocus(true);
Also please add in your adapter row layout root,
android:descendantFocusability="blocksDescendants"
ListView's onClick will not work If any row item of list contains a focusable or clickable view.
If you have your RatingBar's android:isIndicator set to false, then the RatingBar is focusable / clickable.
Set this in your RatingBar node in your xml:
android:isIndicator="true"

Custom Listview - determine if the image is clicked?

I have a custom ListView which has an ImageView, Textview and CheckBox.
In the activity I can register if the ListView Item is pressed by the following code, it they press an item anywhere in the ListView item it just checks a CheckBox and my adapter is updated.
lstView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long row) {
//code
}
But is there a way to determine if the ImageView was pressed? So if they press the image I can do something else?

Change the selected item font size in listview android

In my list view inflator i have an image and a textview. When the user selects the any item in the listview, I want to increase the font size of the text in the textview. I am using a custom adapter. Can I do this in getview method ? And also is it possible to do this without calling notifydatasetchanged() always ?
You can use setTag() and getTag().
setTag() in your adapter's getView()
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView= getLayoutInflater().inflate(R.layout.layoutfile, null);
TextView mTextViewNumber= (TextView) convertView.findViewById(R.id.TvId);
ImageView mImageView= (ImageView) convertView.findViewById(R.id.ImgId)
mTextViewNumber.setText("Hi hello");
mImageView.setBackgroundResource(R.x.xxxx);
convertView.setTag(mTextViewNumber, R.id.TvId);
convertView.setTag(mImageView, R.id.ImgId);
return convertView;
}
Change the Textsize by getTag() OnItemClickListener().
mListView=(ListView) findViewById(R.id.list_call_log_listview);
mListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
TextView mTextView= (TextView) view.getTag(R.id.TvId);
mTextView.setTextSize(20);
ImageView mImageView= (ImageView) view.getTag(R.id.ImgId);
mImageView.setBackgroundResource(R.x.xxxx);
}
});
I think you can use this in your onItemClick method it's rather simple solution
View v=myListView.getChildAt(position);
TextView tv=(TextView)v.findViewById(R.id.myTextView);
tv.setTextSize(...);
in adapter in getView function you can check the current item is selected item then can change the font and color in that if that is ture ...
or can use selector
how to change font color in selected/focused ListView items?

Android: How to extract data from GridView?

Hi all
I have a GridView of FrameLayout made of an image and textview. I want to get the corresponding image and the text when I click a cell of Grid. Mentioned that textview is top of image.
Does anybody have idea how to do it?
First register item click listener of grid view
e.g. gridview.setOnItemClickListener(gridItemClickListener);
Then
OnItemClickListener gridItemClickListener = new OnItemClickListner()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// here you will get your clicked cell of grid and you can find the image and text.
ImageView image = view.findViewById(R.id.grid_raw_image);
TextView textView = view.findViewById(R.id.grid_raw_text);
}
};
If you use the viewHolder technique as recommended by google, use:
gridView.setOnItemClickListener(new OnItemClickListner()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
ViewHolder holder=(ViewHolder)view.getTag();
//use the views you hold in the viewHolder
...
}
});

How to retrieve the right view?

My adapter contain the following Views:
private static class ViewHolder {
ImageView p_Image;
TextView p_Name;
TextView p_Price;
TextView p_Psave;
}
This Is how I implement onItemClick :
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
}
});
How can I know which view from the above 4 was actually pressed (while running OnItemClickListener via GridView) ?
Thanks
The second parameter to onItemClick, View v is a reference to the view that was clicked. You can compare this to your views to see which one it was.

Categories

Resources