this is the image of layout tile.xml
i have created a list using custom adapter it has 2 textviews and 2 buttons inside it now what i want is to change the visiblity of one of textview on button click.
I am handling the button clicks outside the custom adapter.I want to toggle visiblity toggle for second textview with id tvstatus using on and off buttons.
this is code for customadapter
package slide.apptech.com.rpiconnect;
/**
* Created by MOHIT on 09-06-2016.
*/
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
//custom adapter class extends a arrayadapter
public class customadapter2 extends ArrayAdapter<String> {
private final Context context;
private final ArrayList values;
private String stv = "Ststus";
public customadapter2(Context context, ArrayList values) {
//for super constructor pass
// context files
//layout file required for list
//arraylist that has strings to be displayed
super(context, R.layout.tile, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final int pos = position;
final ViewGroup par = parent;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.tile, parent, false);
final TextView textView = (TextView) rowView.findViewById(R.id.tvappname);
Button on = (Button) rowView.findViewById(R.id.bon);
Button off = (Button) rowView.findViewById(R.id.boff);
final TextView status = (TextView) rowView.findViewById(R.id.tvstatus);
on.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
status.setVisibility(View.VISIBLE);
notifyDataSetChanged();
((ListView) par).performItemClick(v, pos, 0); // Let the event be handled in onItemClick()
}
});
off.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) ;
((ListView) par).performItemClick(v, pos, 0); // Let the event be handled in onItemClick()
}
});
//get(position method is used to access the elements of arraylist)
String val = (String) values.get(position);
textView.setText(val);
// Change the icon for Windows and iPhone
String s = (String) values.get(position);
return rowView;
}
public void myClickHandler(View v)
{
}
}
this is code for my xml file that i am using as listelement
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_margin="2dp"
android:layout_height="100dp"
card_view:cardCornerRadius="4dp"
android:layout_alignParentStart="true"
android:background="#611818"
android:paddingBottom="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OFF"
android:id="#+id/boff"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginStart="37dp"
android:focusable="false"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bon"
android:text="ON"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="28dp"
android:focusable="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ttile"
android:id="#+id/tvappname"
android:textSize="20sp"
android:layout_above="#+id/boff"
android:layout_centerHorizontal="true"
android:textColor="#color/abc_input_method_navigation_guard" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ON"
android:id="#+id/tvstatus"
android:layout_below="#+id/tvappname"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textColor="#060505"
android:visibility="invisible"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
please comment if you want any other file
No, don't do that. Changing the value directly of any view inside listview is a bad idea. Just make changes in your arraylist and then call notifyDataSetChanged() to reflect changes.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final int pos = position;
final ViewGroup par = parent;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.tile, parent, false);
final TextView textView = (TextView) rowView.findViewById(R.id.tvappname);
Button on = (Button) rowView.findViewById(R.id.bon);
Button off = (Button) rowView.findViewById(R.id.boff);
final TextView status = (TextView) rowView.findViewById(R.id.tvstatus);
//get(position method is used to access the elements of arraylist)
String val = (String) values.get(position);
textView.setText(val);
if(val.equalsIgnoreCase("off")){
status.setVisibility(View.INVISIBLE);
}else{
status.setVisibility(View.VISIBLE);
}
on.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
values.set(position, "On");
notifyDataSetChanged();
((ListView) par).performItemClick(v, pos, 0); // Let the event be handled in onItemClick()
}
});
off.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
values.set(position, "Off");
notifyDataSetChanged();
((ListView) par).performItemClick(v, pos, 0); // Let the event be handled in onItemClick()
}
});
// Change the icon for Windows and iPhone
String s = (String) values.get(position);
return rowView;
}
Hope it will help :)
Related
I have a Spinner that uses a custom adapter where the getDropDownView() is overridden. Each item in the custom drop-down view is made up of a TextView and a Button.
But, when I run my code, the spinner drop-down items display fine, but clicking them does nothing. The spinner drop-down remains open and spinner.onItemSelected() is not triggered.
drop_down_item.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/dropdown_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:singleLine="true" />
<Button
android:id="#+id/dropdown_button"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:text="Remove"/>
</RelativeLayout>
Custom adapter code
public View getDropDownView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.drop_down_item, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.dropdown_text);
textView.setText(mValues.get(position));
Button buttonView = (Button) rowView.findViewById(R.id.dropdown_button));
return rowView;
}
I create my spinner and adapter with this code:
spinner = (Spinner) findViewById(R.id.my_spinner);
MyAdapter adapter = new MyAdapter(getViewContext(), R.layout.spinner_item, values);
adapter.setDropDownViewResource(R.layout.drop_down_item);
spinner.setAdapter(adapter);
...
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// Do something here - but this never runs
}
});
So I don't know why onItemSelected() is no longer called?
I was wondering if I need to put a click listener on the drop-down TextView which should in turn trigger onItemSelected() using maybe spinner.setSelection(pos)?
The Events is basically a interface that Activity implements to recieve the callBack by clicking on the LinearLayout of the DropDown View of Spinner.
public class MyArrayAdapter extends BaseAdapter {
String[] values;
int CustomResource;
Context context;
Events events;
public MyArrayAdapter(Context baseContext, int customspinnerview,
String[] stringArray, Events events) {
values = stringArray;
context = baseContext;
this.events = events;
CustomResource = customspinnerview;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return values.length;
}
#Override
public Object getItem(int position) {
if (position < values.length)
return values[position];
else {
return null;
}
}
#Override
public View getView(final int position, final View convertView,
ViewGroup parent) {
View rowView = convertView;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (rowView == null) {
rowView = inflater.inflate(CustomResource, parent, false);
}
TextView textView = (TextView) rowView.findViewById(R.id.dropdown_text);
textView.setText(values[position]);
Button button = (Button) rowView.findViewById(R.id.Button_text);
return rowView;
}
#Override
public View getDropDownView(final int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = convertView;`enter code here`
if (rowView == null) {
rowView = inflater.inflate(CustomResource, parent, false);
}
final LinearLayout parentRelative = (LinearLayout) rowView
.findViewById(R.id.parent);
final TextView textView = (TextView) rowView
.findViewById(R.id.dropdown_text);
textView.setText(values[position]);
Button button = (Button) rowView.findViewById(R.id.Button_text);
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
events.onItemSelectedLister(
(AdapterView<?>) parentRelative.getParent(),
parentRelative, position, (long) 0);
}
});
// Button buttonView = (Button)
// rowView.findViewById(R.id.dropdown_button);
return rowView;
}
Events Inteface Its a interface that the Activity implement in order to recieve the callbacks from the Adapter.
import android.view.View;
import android.widget.AdapterView;
public interface Events {
public void onItemSelectedLister(AdapterView<?> parent, View view,
int position, long id);
}
Activity Implementation.
onItemSelected Implementation is the place where you can do your task.....
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import com.example.adapter.MyArrayAdapter;
public class MainActivity extends Activity implements Events {
Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(new MyArrayAdapter(getBaseContext(),
R.layout.customspinnerview, getResources().getStringArray(
R.array.values), this));
}
#Override
public void onItemSelectedLister(AdapterView<?> parent, View view,
final int position, long id) {
//perform your Task.......
Method method;
try {
method = Spinner.class.getDeclaredMethod("onDetachedFromWindow");
method.setAccessible(true);
try {
method.invoke(spinner);
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
spinner.post(new Runnable() {
#Override
public void run() {
spinner.setSelection(position);
spinner.setSelected(true);
((MyArrayAdapter) spinner.getAdapter()).notifyDataSetChanged();
}
});
}
}
Activity xml File for setContentView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.customspinner.MainActivity" >
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</Spinner>
</RelativeLayout>
Spinner View which is passed to Adapter as layout file.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#99000000"
android:id="#+id/parent"
android:orientation="horizontal">
<TextView
android:id="#+id/dropdown_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/Button_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="remove"/>
</LinearLayout>
the Code works perfectly fine :) .I have the code perfectly running.
Solution is to set android:focusable="false" in the layout for both the TextView and the Button.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/dropdown_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:focusable="false"
android:singleLine="true" />
<Button
android:id="#+id/dropdown_button"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:focusable="false"
android:text="Remove"/>
</RelativeLayout>
Alternatively can also do this in the code:
textView.setFocusable(false);
buttonView.setFocusable(false);
Found the answer here. This works because the Spinner implementation only allows one focusable item in the view. That's why I wasn't able to select the item.
I create a custom listview with a TextView and 2 ImageButton. But onitemclicklistner is not calling. Here is my code
police_station_list = (ListView) findViewById(R.id.police_station_listView_xml);
adapter = new ArrayAdapter<String>(this, R.layout.coustome_listview, R.id.district_listview_xml1, police_station_name);
PSAdapter = new Police_Station_Adapter(this);
police_station_list.setAdapter(PSAdapter);
police_station_list.setOnItemClickListener(PSAdapter);
Here is my custom ArrayAdapter class:
public class Police_Station_Adapter extends ArrayAdapter<String> implements OnItemClickListener{
Context context;
public Police_Station_Adapter(Context context) {
//super(context, R.layout.police_station_listview, R.id.police_station_listView_textView, police_station_name);
super(context, R.layout.police_station_listview, police_station_name);
this.context = context;
}
private class ViewHolder {
TextView tv;
ImageButton mobile, phone;
public ViewHolder(View v) {
tv = (TextView) v.findViewById(R.id.police_station_listView_textView);
mobile = (ImageButton) v.findViewById(R.id.police_station_listView_imageButton_mobile);
phone = (ImageButton) v.findViewById(R.id.police_station_listView_imageButton_phone);
}
}
ViewHolder holder;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.police_station_listview, parent, false);
holder = new ViewHolder(v);
v.setTag(holder);
}
else {
holder = (ViewHolder) v.getTag();
}
holder.tv.setText(police_station_name[position]);
holder.mobile.setTag(position);
holder.mobile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new Message(context, police_station_name[(Integer) v.getTag()]+" button", 500);
}
});
return v;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
new Message(context, "Item Clicked", 500); //Toast message
}
}
Here is the custom ListView xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<TextView
android:id="#+id/police_station_listView_textView"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="8"
android:textSize="16sp"
android:layout_marginRight="10dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageButton
android:id="#+id/police_station_listView_imageButton_mobile"
android:layout_gravity="center_vertical"
android:layout_width="27dp"
android:layout_height="30dp"
android:layout_weight="1"
android:layout_marginRight="15dp"
android:background="#drawable/mobile_icon" />
<ImageButton
android:id="#+id/police_station_listView_imageButton_phone"
android:layout_gravity="center_vertical"
android:layout_width="27dp"
android:layout_height="30dp"
android:layout_weight="1"
android:layout_marginRight="10dp"
android:background="#drawable/telephone_icon" />
</LinearLayout>
it's seems like i can add onClikcListner for textview but it doesn't show any pressed animation. It may not be the appropriate solution. I need the proper way to setOnItemClickListner.
You need to set OnItemClickListener in your Activity. then change this
police_station_list.setOnItemClickListener(PSAdapter); to
police_station_list.setOnItemClickListener(this);
Remove implementation of OnItemClickListener from adapter.
Check this tutorial.
add this attribute to imageButton:
android:focusable="false"
I have an interface designed as a ListView (but I can change if needed). Each row has 2 buttons: MS and DROP.
Clicking on the buttons should affect only the corresponding row. For example, on the last row (where int "3" is displayed), clicking on MS should call function1(rowPosition) and clicking on DROP should call function2(rowPosition).
I have 2 problems:
Even without any click listener, clicking on any row activate both corresponding buttons. Maybe ListView is not the correct choice for this kind of interface.
I can't figure out how to implement the Click listener.
Here is the main code:
package com.thomas.calculation.app;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class Memory_listview extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.memory_listview);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new MyAdapter(this));
}
}
class MyAdapter extends BaseAdapter {
Context context;
ArrayList<String> singleRow_list;
MyAdapter(Context c) {
context = c;
singleRow_list = new ArrayList<String>();
for (int i=1; i<10; i++)
singleRow_list.add(i, Integer.toString(i) );
}
#Override
public int getCount() {
return singleRow_list.size();
}
#Override
public Object getItem(int i) {
return singleRow_list.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
class MyViewHolder
{
TextView myTextView;
MyViewHolder(View view) {
myTextView = (TextView) view.findViewById(R.id.textView);
}
}
#Override
public View getView(int i, View view, ViewGroup viewGroup)
{
View view_single_row = view;
MyViewHolder myViewHolder = null;
if (view_single_row==null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view_single_row = inflater.inflate(R.layout.memory_listview_row, viewGroup, false);
myViewHolder = new MyViewHolder(view_single_row);
view_single_row.setTag(myViewHolder);
}
else
{
myViewHolder = (MyViewHolder) view_single_row.getTag();
}
String newData = singleRow_list.get(i);
//This is not working:
//Button button_MS = (Button) findViewById(R.id.button_MS); //ERROR: findViewById undefined
//Button button_MS = (Button) this.findViewById(R.id.button_MS); //ERROR: findViewById undefined
//Button button_MS = (Button) view.findViewById(R.id.button_MS); //APP CRASH
//Button button_MS = (Button) view_single_row.findViewById(R.id.button_MS); //APP CRASH
//Button button_MS = (Button) viewGroup.findViewById(R.id.button_MS); //APP CRASH
myViewHolder.myTextView.setText(newData);
return view_single_row;
}
}
Here is the row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/bgStack"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView"
android:text=""
style="#style/memory_monitor"
android:weightSum="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/linearView" />
<LinearLayout
android:id="#+id/linearView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentRight="true">
<TextView
android:id="#+id/button_MS"
style="#style/memory_tiny"
android:text="#string/calc_MS" />
<TextView
android:id="#+id/button_del"
style="#style/memory_tiny"
android:text="#string/calc_del" />
</LinearLayout>
</RelativeLayout>
Inside your getView.
MyViewHolder.button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
// do for the other button too..
// yout methods here.
}
});
And change the TextView to Button in your xml
I'm trying to dynamically add click listeners to a custom array adapter for a list view. The idea is that I have list objects with text on them (eg "Group 1", "Group 2" etc.), and then when I click on the object a drop down appears ("expandable_section") with two buttons on it ("Members" and "Settings"). When I click on the members button I want to be able to access the specific text on the original group object. For example, clicking "Members" under the "Group 1" tab would give me access to the text "Group 1."
Here is my XML layout for the custom adapter object:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- The list element for GROUP SETTINGS tab sub-section -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >
<ImageView
android:id="#+id/activeIcon"
android:layout_width="34dp"
android:layout_height="34dp"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="-7dp"
android:layout_marginTop="5dp"
android:gravity="center_vertical" />
<ImageView
android:id="#+id/modeIcon"
android:layout_width="34dp"
android:layout_height="34dp"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:gravity="center_vertical" />
<TextView
android:id="#+id/groupName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:gravity="center_vertical"
android:textColor="#000000"
android:textSize="22dp" />
</LinearLayout>
<!-- SPECIAL HIDDEN TOOLBAR FOR DEMO LIBRARY LIST MODES TODO: Change to whitelist/blacklist GROUP TAB! -->
<!-- EXPANDABLEFOR GROUP-SETTINGS FRAGMENT -->
<LinearLayout
android:id="#+id/expandableSubsection"
android:layout_width="fill_parent"
android:layout_height="90dp"
android:layout_marginBottom="-100dp"
android:background="#dddddd"
android:gravity="center"
android:visibility="gone" >
<Button
android:id="#+id/groupMembersButton"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginRight="20dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Members" />
<Button
android:id="#+id/groupSettingsButton"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginRight="20dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Settings" />
</LinearLayout>
</LinearLayout>
Here is the custom adapter code:
package com.sapphire.view;
import java.util.List;
import com.sapphire.model.Mode;
import com.sapphire.view.ModeAdapter.ModeHolder;
import android.R;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class GroupAdapter extends ArrayAdapter<String>{
private Context context;
private int resource;
public GroupAdapter(Context context, int resource, int textViewResourceId,
List<String> objects) {
super(context, resource, textViewResourceId, objects);
this.context = context;
this.resource = resource;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi =(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(this.resource, null);
}
Button but= (Button) v.findViewById(R.id.button1);
if( but!=null){
but.setId(position);
but.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("customgroup clicked !");
}
});
}
return v;
}
}
and here is where the custom adapter is actually used to display the list of groups:
public void displayGroupsOptions(){
final ListView lView = (ListView) thisView.findViewById(R.id.GroupsView);
final ArrayList<String> groupNameList = new ArrayList<String>();
lView.setAdapter(null); //empty the list view
//Get all groups from database table
System.out.println("customgroup about to get all groups from database");
List<Group> allGroups = db.getAllGroups();
if(!allGroups.isEmpty()){
for (Group g: allGroups){
groupNameList.add(g.getName());
}
//Convert group arraylist to group array for use in adapter for list view
String[] groupNameArray = (String[]) groupNameList.toArray(new String[0]);
GroupAdapter adapter = new GroupAdapter(getActivity().getApplicationContext(), R.layout.group_list_row, R.id.groupName, groupNameList);
lView.setAdapter(adapter);
// Creating an item click listener, to open/close the mode options toolbar for each item
lView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
View groupOptions = view.findViewById(R.id.expandableSubsection);
// Creating the expand animation for the item
ExpandAnimation ea = new ExpandAnimation(groupOptions, ANIMATION_DELAY);
// Start the animation on the toolbar
groupOptions.startAnimation(ea);
}
});
}
}
Any help would be appreciated! I just can't figure out for the life of me how to get the proper text to show up on the tab (right now it's blank) and how to make the onclick listener actually attach and work. Thanks!
You can set tag for your Button in getView method to pass the position argument, and get the tag in th ClickListener.
btn.setTag(position);
#Override
public void onClick(View v) {
int position = (Integer)v.getTag();
}
Even you can set other view in the same adapter item as a tag and pass it to the listener.
use this piece of code where you used getView in adapter class
public class ViewHolder {
Button b;
}
and also this
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.xyz,
parent, false);
holder.b = (Button) convertView.findViewById(R.id.button1);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
Item it = item.get(position);
holder.b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("customgroup clicked !");
}
});
return convertView;
}
Hi all I have custom grid view with imageButton and textView as follows
Here i have used imageButton.. The problem is the grid is not clickable.. But if i use here imageView it works fine but UI is not appropriate.
Here is my layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="5dp"
>
<ImageButton
android:id="#+id/profilePic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/newlocation"
android:background="#drawable/btnbackground"
/>
<TextView
android:id="#+id/SpeakerName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="15sp"
android:text="Some Speaker"
/>
Here is my adapter
package com.tt.cc;
import java.util.List;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class SavedConferencesGridAdapter extends ArrayAdapter<String>{
Context mContext;
List<String> confernceList;
public SavedConferencesGridAdapter(Context context, int textViewResourceId, List<String> confernceList)
{
super(context,textViewResourceId,confernceList);
this.confernceList = confernceList;
this.mContext = context;
Log.e("TAG", confernceList.size()+"");
}
int[] picIds = new int[] { R.drawable.newschedule,R.drawable.newexhibitors,
R.drawable.newsponsers, R.drawable.newlocation,
R.drawable.newfavourites, R.drawable.newreminder,R.drawable.info,R.drawable.info,R.drawable.info,R.drawable.info,R.drawable.info,R.drawable.info };
#Override
public int getCount() {
// TODO Auto-generated method stub
return confernceList.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v;
if(convertView==null){
LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.entity, null);
TextView tv = (TextView)v.findViewById(R.id.SpeakerName);
tv.setText("tt");
/*ImageView iv = (ImageView) v.findViewById(R.id.profilePic);
iv.setImageResource(picIds[position]);*/
}
else
{
v = convertView;
}
return v;
}
}
and here is my activity
public class SavedConferencesActivity extends Activity{
GridView confereneceGridView;
ArrayAdapter<String> conferneceAdapter;
Button btnUpdate;
List<String> conferenceList;
TextView txtHeading;
Boolean result = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
protected void onStart() {
super.onStart();
setContentView(R.layout.homegridlayout);
initialize();
confereneceGridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(SavedConferencesActivity.this, conferenceList.get(position)+"", Toast.LENGTH_SHORT).show();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
conferneceAdapter = new SavedConferencesGridAdapter(this, android.R.layout.simple_list_item_1, conferenceList);
confereneceGridView.setAdapter(conferneceAdapter);
}
private void initialize() {
confereneceGridView = (GridView) findViewById(R.id.gridOfConference);
btnUpdate = (Button) findViewById(R.id.btnUpdate);
txtHeading = (TextView) findViewById(R.id.txtTag);
conferenceList = new ArrayList<String>();
conferenceList.add("AA");
conferenceList.add("BB");
conferenceList.add("CC");
if (conferenceList.size() == 0)
txtHeading.setText("No conferences saved");
else
txtHeading.setText("Selected Conferences");
}
}
Please help me in solving this problem. Or if any alternative available..
Simple. Provide this attribute for the ImageButton ,
android:focusable="false"
This problem usually occurs when there is a focussable element in the Grid.(Eg:Buttons, ImageButton etc).
Reason of this ImageButton is by Default is clickable and focusable, so Focus and click event never goes to ItemView.
Set Properties
android:focusable-"false"
and
android:clickable="false"
Your item's Layout should be:
<LinearLayout ...
android:descendantFocusability="blocksDescendants">
<ImageButton ...
android:focusable="false"
android:focusableInTouchMode="false"/>
</LinearLayout>
Don't forget setOnClickListener for ImageButton in Adapter.
in your adapter class,
before you return the view "v", add the "onClickListener" to "v"
v.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, position, Toast.LENGTH_SHORT).show();
}
});