Custom listview onclicklistener on inner view - java

i currently create an app that needs an custom listview. Everything with my listview is fine, but now i neet to know, how to set an onClickListener to an view, defined in my list_row.xml. i just want the onclicklistener on the whole item, and on this one inner view. I attach a picture to demonstrate my problem, because it is so hard to describe >.<
Picture (dropbox): https://www.dropbox.com/s/72xdxuwz47vl7s5/problem.png
I need a function that is called when clicking into the view [my Problem] indicates. its an ImageView filled with an image.

Here's something I've done before that seems pretty similar to what you want to accomplish.
First, you declare an onItemClickListener for your ListView. This will handle standard list item taps (that is, taps inside a list item but outside the inner view region that you're concerned about). You can do this in a variety of places in your code, but onCreate() is a common one.
Example:
mListView.setOnItemClickListener( new OnItemClickListener() {
#Override
public void onItemClick( AdapterView<?> parent, View view, int position, long id ) {
// Handle standard list item tap
// ...
}
} );
Then, you can just declare whatever onClickListeners you need for your inner view(s) inside your adapter's getView() method to handle click/tap events on your inner view.
Example:
#Override
public View getView( int position, View convertView, ViewGroup parent ) {
LinearLayout itemView;
// Inflate layout XML, etc.
// ...
// Find subviews in layout
ImageView innerView = (ImageView) itemView.findViewById( R.id.myInnerViewId );
// ...
// Set up onClickListener for inner view
innerView.setOnClickListener( new OnClickListener() {
#Override
public void onClick( View v ) {
// Handle inner view tap
// ...
}
} );
// ...
}

To set an OnClickListener in each row simply extend your current Adapter and override the getView() method. In there you can define specific listeners as you normally would.
This is discussed in great detail in this Google Talk by Romain Guy.

Related

Execute code from Spinner's onItemSelected that's in custom listView

I have a custom listView with 3 spinners. I want to execute a code in onItemSelcted but I cannot do this in adapter because some part of the code that I need to execute won't work in a adapter.
I had this code pinned to a button before but sometimes there are over 20-30 elements in the list and I need do this "automaticly" right after user picks a value from spinner to save some time.
For a button I had this code:
((ListView) parent).performItemClick(v, position, 0);
in setOnClickListener that I created inside adapter. After using this code in adapter I could exetue any code outside adapter by writing something like this:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
long viewId = view.getId();
if(viewId == R.id.button){
myCode(); }
I found no way to do something similar because there was no "link" between inside adapter onItemSelcted and outside adapter onItemSelcted. Of course writing same line of code inside adapter's onItemSelcted won't work because spinner "doesn't click".
Any idea how to solve this?
In a fewer words: I need to execute onItemSelected for a spinner that's inside custom listView element but need to do this outside the adapter in onCreate method.
You can create an CallbackInterface and create an instance of it "outside" and pass it down to the "inside", where you call the callback function with the needed arguments.
public interface Callback {
void onAction (int a, int b, String c);
}

Refreshing a RecyclerView when all items are removed

I have a RecyclerView that shows a list of items.
If there is no item to show, The recyclerview shows one item with a specific view (to tell the user there is no item instead of a white screen).
Within HistoryFragment:
private void initRecyclerView(Boolean isNoResult){
HistoryRecyclerViewAdapter adapter = new HistoryRecyclerViewAdapter(mContext, mRecords, **isNoResult**);
mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
mRecyclerView.setAdapter(adapter);
}
Within HistoryRecyclerViewAdapter:
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view;
if(**isEmpty**) {
**view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem_prhistory_empty, parent, false);**
} else {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem_prhistory, parent, false);
}
ViewHolder holder = new ViewHolder(view);
return holder;
}
So, it's possible to remove items one by one, if we click on them.
I would like to set isEmpty to true and refresh the RecyclerView when the dataSet is null.
I already know where to call that method but I really don't know how I can do that? (i.e. refresh the RecyclerView with isEmpty = true so I can display the cell that explain to the user that there is no record anymore).
Don't inflate different view-holders, because when the adapter has no items, not a single one of them will ever be inflated. Instead one can wrap the RecyclerView together with a "no data" Fragment into a ViewFlipper, which then can be switched to the Fragment, when the RecyclerView adapter has no items.
Best practice is to use an empty view outside of RecyclerView but in case you like to do what you want:
1.in onCreateViewHolder only inflate one layout which has empty and item views
on item delete check if your array is empty and then add a null item
then in onBindViewHolder check the model if model is Null visible empty view otherwise show item view
summary:
onBind:
model is null : empty View visible
model is not null: item View visible
use interface to refresh the RecyclerView after remove something like this
public interface RefreshRecyclerView {
public void Refresh();
}
then in activity or fragment implement the interface
Fragment implements RefreshRecyclerView
you will have override method like this
#Override
public void Refresh() {
// set adapter again here
}
then pass the interface to adapter like this
RefreshRecyclerView refresh = (RefreshRecyclerView) this;
yourRecycler.setadapter(refresh);
fially when user clicked on adapter use this
refresh. Refresh();

Android Studio gridView

Is it possible to have methods and actual coding inside each item for a GridView?
The app that I am attempting to create is a currency converter, and I am currently displaying 3 images in the gridView: Euros, Pesos, and Rupees.
Once the user clicks on one, I want the open to open up a new XML which displays a textView. The user enters the value of US dollars in the textView and clicks the compute button. The app then displays the converted amount in the bottom of the screen.
The problem is that I am unable to figure out how to open up a new XML every time a picture is clicked on in the gridView. Assuming that I am able to do this, I am also unsure of where to place the code that goes behind the conversions. Would I make a new .java or just place is all in MainActivity.java?
Thanks.
What you might be best doing is when the user clicks on a currency it takes them to another activity where you would then load another xml for whatever you want to show.
In order to detect which item had been clicked you can implement an onItemClickListener for example
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//this assumes you give the gridview a list of currency which it then displays. Here we get the currency selected to then pass to our new activity
String selectedCurrency = myArrayOfCurrencies.get(position);
//then start new activity and give it the currency. This means we won't have to create an activity for each currency. You just need to create 1 and then based on what currency you give it will change the functionality
Intent intent = new Intent(this, Converter.class);
Intent.putExtra("currency", selectedCurrency);
startActivity(intent);
}
First you should be able to detect the clicks on each item of the GridView by calling the setOnItemClickListener() method.
If you set the clicklistener and you still can't detect the clicks, then most probably you need to add those attribtutes to your imageView in the xml
android:focusable="false"
android:focusableInTouchMode="false"
Second, once you are able to detect the clicks you can start new activity or add fragment that contains that edit text that will promote the user to enter the value.
Third, I would suggest to put the code responsible for the currency conversion in a class separately and create static methods that takes a value and convert it to the other curreny such as:
public class CurrencyConverter {
public static double convertToRupees (String currencyType, double currencyValue){
....
return currencyInRupees;
}
}
and by the way I would suggest you to use RecyclerView with grid layout manager instead of GridView.
I would create more classes.
You asked how to open a different XML file for each gridView item.
Create a custom adapter that extends BaseAdapter.
Override getView and for each view attach the right Xml file, according to the position.
For example:
YourActivity.java:
GridView gridView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (GridView) findViewById(R.id.gridView);
gridView.setAdapter(new MyAdapter(getApplicationContext());
}
MyAdapter.java:
...
#Override
public int getCount() {
return XmlArr.length;
}
#Override
public Object getItem(int position) {
return XmlArr[position];
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Xml myXml = (Xml) getItem(position);
Holder holder;
if (convertView == null) {
// Set your view's layout. Consider using LayoutInflater.
// Use a static holder to prevent re-initialization:
holder = new Holder();
// holder.textView = ...
// holder.Xml = ...
// Or whatever you decided to have in each gridView item.
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.Xml = myXml;
...
return convertView;
}
static class Holder() {
TextView tv;
Xml xml;
...
}
I assumed you would used an Xml array (xmlArr).
Now you have option to play with each gridView item as you wish. You can set each view/button/textView an onItemClickListener, or you can also set the whole gridView an onItemClickListener (from YourActivity.java).
Hope this helps.

How to set the background color of a specific item in listview by position?

I want to set the background color of a specific item in the listview.
My listview is generated by ArrayAdapter using a ArrayList.
I have a specific item in the listview that I plan to change the background color.
I know the item's position in the list.
This is my code for generating the listview.
respondMessageListView = (ListView) findViewById(R.id.respondMessageListView);
respondMessageListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, autoRespondMessages.getMessages()));
Thank you!
[edit]
According to this post, using setSelection makes no effect if is used in onCreate(), the work around is "remove the method onAttachedToWindow in PullToRefreshListView". I am not quite understanding the solution. May I ask how should I accomplish this? I am a subclass of Activity, so I cannot subclass any other class anymore.
You will have to subclass ArrayAdapter and override the getView(...) method. For simplicity's sake you could just call through to the base class implementation and set the background color for the returned View.
Edit:
The following example colors the items' backgrounds alternating black and white.
private class MyAdapter extends ArrayAdapter {
...
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
v.setBackgroundColor(position % 2 == 0 : 0xff000000, 0xffffffff);
}
}
This code is for the when you select the listitem.
Try this code...
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> myAdapter, View myView, int pos, long mylng) {
if( pos == 1) {
// to change the listview background
listview.setBackgroundColor(getResources().getColor(R.color.your_color_id));
// to change the selected item background color
myView.setBackgroundColor(getResources().getColor(R.color.your_color_id));
}
}
});
Good luck.

Android multiple column list/grid with adapter and OnItemClickListener?

I have a simple grid with rows of items and columns of options on each item. Of course I can manually link OnClick actions to cell items through specific resource IDs:
views.setOnClickPendingIntent(R.id.row1column1, launchA);
views.setOnClickPendingIntent(R.id.row1column2, launchB);
// ...
views.setOnClickPendingIntent(R.id.row2column1, launchC);
// ...
What I'd prefer to to is use a ListView or a GridView and an Adapter. Is there a way to get cell id's from one of those? I was hoping that ListView setOnItemClickListener() would call me with the view id of a view within row 'position':
onItemClick(AdapterView<?> parent, View view, int position, long id) { ... }
but no go. Maybe a different approach?
Thanks.
Update:
I started with this:
contactsView.setAdapter(new ContactAdapter(this.getApplicationContext(), R.layout.contact, contacts));
contactsLV.setOnItemClickListener(new OnItemClickListener() { ... });
I'd assumed that I couldn't go into my Adapter and set child view OnClickListeners. Bad assumption. When I went back at this I gave it a try:
Button x = (Button)newView.findViewById(R.id.contact_x);
Button y = (Button)newView.findViewById(R.id.contact_y);
x.setOnClickListener(new OnClickListener() { ... });
y.setOnClickListener(new OnClickListener() { ... });
...
Voila!

Categories