I am trying to use StickyGridHeaders in my Android app and it is working great except when I try to add a clicklistener to a clickable ImageView in the headerview. In getHeaderView() in my BaseAdapter I am trying to do the following:
getHeaderView
#Override
public View getHeaderView(final int pos, View view, ViewGroup viewGroup) {
view = inflater.inflate(R.layout.gallery_item,viewGroup, false);
TextView title = (TextView) view.findViewById(R.id.title);
TextView date = (TextView) view.findViewById(R.id.date);
ImageView settings = (ImageView) view.findViewById(R.id.folder_settings);
settings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(mContext, "The Click Worked.", Toast.LENGTH_SHORT).show();
}
});
GalleryItem galleryItem = galleryItems.get(pos);
icon.setImageResource(setIcon(galleryItem.getMode()));
title.setText(galleryItem.getTitle());
Date da = galleryItem.record.getDate("FILE_DATE");
SimpleDateFormat dateFormat = new SimpleDateFormat("LLLL-dd-yyyy");
String mDate = dateFormat.format(da);
date.setText(mDate);
return view;
}
gallery_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="55dp"
android:background="#color/lightgraymain"
android:id="#+id/linearLayout">
<ImageView
android:layout_width="55dp"
android:layout_height="match_parent"
android:id="#+id/icon"
android:src="#drawable/ic_gallery_mode_tag" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="521 North 7th Street, Lincoln, NE"
android:id="#+id/title"
android:textColor="#color/black"
android:layout_weight="1"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:padding="5dp"
android:fontFamily="sans-serif-condensed"
android:enabled="true"
android:ellipsize="marquee"
android:textIsSelectable="false"
android:singleLine="true"
android:textSize="14dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="August-25-2014"
android:id="#+id/date"
android:layout_gravity="center_vertical"
android:fontFamily="sans-serif-light"
android:textColor="#color/gray" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/folder_settings"
android:src="#drawable/ic_gallery_options"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:padding="10dp"
android:layout_gravity="center"
/>
</LinearLayout>
I cannot get the toast to appear. I have tried implementing onHeaderClick() in the adapter as well to no avail. Any help would be greatly appreciated.
Thank you,
-Zach
You should implement the listener in the fragment/activity who hosts the StickyGrid
I made it to work like this:
#Override
public void onHeaderClick(AdapterView<?> adapterView, View view, long l) {
view.findViewById(R.id.folder_settings).performClick();
}
For a header with just one clickable element it's ok. If the header has more than one element which can be clickable this solution won't work.
Ok so after hours and hours of mind numbing searching for this answer, I finally figured it out. I was looking through the example here and noticed that in the header layout, they didn't specify the layout as
//remove the following from your header layout
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
I removed these properties from my header view completely and viola! it works. I feel pretty dumb after that, but hope it helps someone else.
UPDATE
Here is a little more detail in how I have implemented it.
Fragment Class
public static class PlaceholderFragment extends Fragment implements
StickyGridHeadersGridView.OnHeaderClickListener,
StickyGridHeadersGridView.OnItemClickListener,
StickyGridHeadersGridView.OnItemLongClickListener {
#Override
public void onHeaderClick(AdapterView<?> adapterView, final View view, long l) {
Log.i("asd","THE HEADER IS CLICKED");
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.i("asd","ITEM "+i+" IS CLICKED");
}
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, final View view, int i, long l) {
Log.i("asd","ITEM "+i+" IS LONG CLICKED");
return true;
}
Custom Base Adapter
public class MediaAdapter extends BaseAdapter implements StickyGridHeadersBaseAdapter{
private Context mContext;
ArrayList<MediaItem> mediaitems;
ArrayList<GalleryItem> galleryItems;
LayoutInflater inflater;
HashMap<String,ImageView> imageHolder;
public MediaAdapter(Context c,ArrayList<GalleryItem> l) {
mediaitems = new ArrayList<MediaItem>();
galleryRef = new HashMap<String, Integer>();
mContext = c;
galleryItems = l;
imageHolder = new HashMap<String, ImageView>();
inflater = LayoutInflater.from(c);
}
public GalleryItem getSelectedGallery(String id){
return ((Tidy)mContext.getApplicationContext()).startOrGetFileStore().getGalleryItem(id);
}
public int getCount() {
return mediaitems.size();
}
public Object getItem(int position) {
return mediaitems.get(position);
}
public long getItemId(int position) {
return position;
}
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public View getView(int position, View convertView, ViewGroup parent) {
GridView gv =(GridView)parent;
ViewHolder holder;
if (convertView == null) {// if it's not recycled, initialize some attributes
convertView = inflater.inflate(R.layout.media_item, parent, false);
holder = new ViewHolder();
holder.img = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
imageManager.displayImage(holder.img);
return convertView;
}
#Override
public int getCountForHeader(int i) {
return galleryItems.get(i).getMediaLength();
}
#Override
public int getNumHeaders() {
return galleryItems.size();
}
#Override
public View getHeaderView(final int pos, View view, ViewGroup viewGroup) {
HeaderViewHolder holder = new HeaderViewHolder();
if(view == null) {
view = inflater.inflate(R.layout.gallery_item, viewGroup, false);
holder.title = (TextView) view.findViewById(R.id.title);
holder.date = (TextView) view.findViewById(R.id.date);
holder.icon = (ImageView) view.findViewById(R.id.icon);
holder.settings = (ImageView) view.findViewById(R.id.folder_settings);
view.setTag(holder);
}
else{
holder = (HeaderViewHolder) view.getTag();
}
GalleryItem galleryItem = galleryItems.get(pos);
holder.icon.setImageResource(setIcon(galleryItem.getMode()));
holder.title.setText(galleryItem.getTitle());
Date da = galleryItem.record.getDate("FILE_DATE");
SimpleDateFormat dateFormat = new SimpleDateFormat("LLLL-dd-yyyy");
String mDate = dateFormat.format(da);
holder.date.setText(mDate);
holder.gallery = galleryItem.record;
return view;
}
class HeaderViewHolder {
DbxRecord gallery;
RelativeLayout layout;
TextView title;
TextView date;
ImageView icon;
ImageView settings;
}
class ViewHolder {
ImageView img;
}
}
View XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.tonicartos.widget.stickygridheaders.StickyGridHeadersGridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/imagegrid"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_weight="1"
android:stackFromBottom="false"
android:numColumns="auto_fit"
android:columnWidth="80dp"
/>
</RelativeLayout>
Related
i plan to have a list view which when onlongclick, will be able to select all the subrows/related rows.
Example
Input:
Long click on user name/age/birthday
Output:
Return the username, age and birthday.
Problem
Will only return the user name if you long click on it and not the age and birthday.
I didn't include the age and birthday value to be display in the toast cause the app will display an error and stop functioning.
Latest:
Have shown the Adapter and object class on 26th Nov17
Custom layout of list view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username:"
android:background="#FF2400"
android:textColor="#FFFFFF" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/text_user_name"
android:textColor="#FFFFFF"
android:background="#FF2400"
android:text="Username"
android:gravity="left"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age:"
android:background="#FFFFFF"
android:textColor="#000000" />
<EditText
android:layout_width="match_parent"
android:layout_weight="0.5"
android:textColor="#000000"
android:layout_height="wrap_content"
android:id="#+id/text_user_age"
android:background="#FFFFFF"
android:text="Age"
android:gravity="left"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Birthday:"
android:background="#FFFFFF"
android:textColor="#000000" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:textColor="#000000"
android:background="#FFFFFF"
android:id="#+id/text_user_birthday"
android:text="Birthday"
android:gravity="left"/>
</LinearLayout>
</LinearLayout>
DataListActivity (Listview activity)
listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
public boolean onItemLongClick(AdapterView<?> arg0, View v,
int index, long arg3)
{
AlertDialog.Builder builder = new AlertDialog.Builder(DataListActivity.this);
builder.setTitle("Notice");
builder.setMessage("Launching this missile will destroy the entire universe. Is this what you intended to do?");
final View clicklist = v;
final int position = index;
builder.setPositiveButton("Launch missile", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String selname= ((EditText) clicklist.findViewById(R.id.text_user_name)).getText().toString();
// String selage= ((EditText) clicklist.findViewById(R.id.text_user_age)).getText().toString();
String selbirthday= ((EditText) clicklist.findViewById(R.id.text_user_birthday)).getText().toString();
Toast.makeText(DataListActivity.this,selname + selage + selbirthday,Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton("Cancel", null);
AlertDialog dialog = builder.create();
dialog.show();
return false;
}
});
LayoutAdapter
public class ListDataAdapter extends ArrayAdapter{
List list = new ArrayList();
static class LayoutHandler
{
TextView NAME,AGE,BIRTHDAY;
}
public ListDataAdapter(Context context, int resource) {
super(context, resource);
}
#Override
public void add (Object object)
{
super.add(object);
list.add(object);
}
#Override
public int getCount(){
return list.size();
}
#Override
public Object getItem(int position)
{
return list.get(position);
}
#Override
public View getView (int position, View convertView, ViewGroup parent){
View row = convertView;
LayoutHandler layoutHandler;
if (row==null)
{
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.row_layout,parent,false);
layoutHandler = new LayoutHandler();
layoutHandler.NAME = (TextView) row.findViewById(R.id.text_user_name);
layoutHandler.AGE = (TextView) row.findViewById(R.id.text_user_age);
layoutHandler.BIRTHDAY = (TextView) row.findViewById(R.id.text_user_birthday);
}
else
{
layoutHandler = (LayoutHandler) row.getTag();
}
DataProvider dataProvider = (DataProvider) this.getItem(position);
layoutHandler.NAME.setText(dataProvider.getName());
layoutHandler.AGE.setText(dataProvider.getAge());
layoutHandler.BIRTHDAY.setBIRTHDAY(dataProvider.getBirthday());
return row;
}
#Override
public void clear()
{
list.clear();
}
}
public class DataProvider {
private String Name;
private String Age;
private String Birthday;
....getter and setters method
After analyzing your code, i found following problems :-
In List adapter you didn't set the tag to row so it always return null layoutHandler and your casting EditText to TextView , i didn't get your point here if you want TextView then use same view in XML.
So the ListAdapter Looks like this.
public class ListAdapter extends ArrayAdapter {
List list = new ArrayList();
static class LayoutHandler
{
EditText NAME,AGE,BIRTHDAY;
}
public ListAdapter(Context context, int resource) {
super(context, resource);
}
#Override
public void add (Object object)
{
super.add(object);
list.add(object);
}
#Override
public int getCount(){
return list.size();
}
#Override
public Object getItem(int position)
{
return list.get(position);
}
#Override
public View getView (int position, View convertView, ViewGroup parent){
View row = convertView;
LayoutHandler layoutHandler;
if (row==null)
{
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.custom_layout,parent,false);
layoutHandler = new LayoutHandler();
layoutHandler.NAME = (EditText) row.findViewById(R.id.text_user_name);
layoutHandler.AGE = (EditText) row.findViewById(R.id.text_user_age);
layoutHandler.BIRTHDAY = (EditText) row.findViewById(R.id.text_user_birthday);
row.setTag(layoutHandler);
}
else
{
layoutHandler = (LayoutHandler) row.getTag();
}
DataProvider dataProvider = (DataProvider) this.getItem(position);
layoutHandler.NAME.setText(dataProvider.getName());
layoutHandler.AGE.setText(dataProvider.getAge());
layoutHandler.BIRTHDAY .setText(dataProvider.getBirthday());
return row;
}
#Override
public void clear()
{
list.clear();
}
}
and coming to the actual problem i.e. ListView OnItemLongClickListener crashing. You just have to change
final View clicklist = v;
to
final View clicklist = arg0.getChildAt(index);
and then you go. :)
A better way to this is to
1) Create a Custom Class for example Data and add properties that you
want to display within that class.
2) Create an Adapter and populate your adapter with arraylist of Data class.
3) Retrieve the values from listview using onItemClicklistener.
modify your toast
Toast.makeText(DataListActivity.this,"name"+selname+"age"+
selage+"DOB"+ selbirthday,Toast.LENGTH_LONG).show();
Hellow i've implemented ListView inside the CardView but now what problem i got is, the CardView is not Expanding on Adding list item. Can anybody help me,how to slove the issue? I have gone through different posts on stackoverflow but cannot find the solution :( My code is below
//this is my Listview adapter Class
public class EmploymentHistoryAdapter extends BaseAdapter {
public ArrayList<EmploymentHistory> mEmploymentHistories;
private Context context;
public EmploymentHistoryAdapter(ArrayList<EmploymentHistory> mEmploymentHistories, Context context) {
this.mEmploymentHistories = mEmploymentHistories;
this.context = context;
}
#Override
public int getCount() {
return mEmploymentHistories.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = null;
EmploymentHistory empHistory = mEmploymentHistories.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_employee_history_row, parent, false);
} else {
v = convertView;
}
TextView hospital = (TextView) v.findViewById(R.id.hospital);
TextView department = (TextView) v.findViewById(R.id.department);
TextView startDate = (TextView) v.findViewById(R.id.startDate);
TextView endDate = (TextView) v.findViewById(R.id.endDate);
TextView hospitalName = (TextView) v.findViewById(R.id.hospitalName);
hospitalName.setText(empHistory.getHospital());
TextView departmentName = (TextView) v.findViewById(R.id.departmentName);
departmentName.setText(empHistory.getDepartment());
TextView startDate1 = (TextView) v.findViewById(R.id.startDate1);
startDate1.setText(empHistory.getStartDate());
TextView endDate1 = (TextView) v.findViewById(R.id.endDate1);
endDate1.setText(empHistory.getEndDate());
hospital.setVisibility(View.VISIBLE);
department.setVisibility(View.VISIBLE);
startDate.setVisibility(View.VISIBLE);
endDate.setVisibility(View.VISIBLE);
return v;
}
}
//this is where i populated the ListView
private void populateEmploymentHistoryList() {
listEmployeeHistory = (ListView) findViewById(R.id.listEmployeeHistory);
empHistory = dbHelper.getAllEmploymentHistory();
employmentHistoryAdapter = new EmploymentHistoryAdapter(empHistory,this);
listEmployeeHistory.setAdapter(employmentHistoryAdapter);
}
//this is my Layout file where i added the ListView and I have the custom design for list_row
<android.support.v7.widget.CardView
android:id="#+id/employmentHistoryCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/card_margin"
android:layout_marginLeft="#dimen/card_margin"
android:layout_marginRight="#dimen/card_margin">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/employmentHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#E0E0E0"
android:gravity="center"
android:text="Employment History"
android:textAppearance="#style/TextAppearance.AppCompat.Title"
android:textColor="#4A148C" />
<TextView
android:id="#+id/addEmployment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/employmentHistory"
android:layout_marginTop="15dp"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:drawableLeft="#drawable/ic_add"
android:drawablePadding="20dp"
android:drawableStart="#drawable/ic_add"
android:paddingLeft="30dp"
android:text="Add Employment"
android:textColor="#0e89d0" />
<ListView
android:id="#+id/listEmployeeHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/addEmployment">
</ListView>
</RelativeLayout>
</android.support.v7.widget.CardView>
You need to specify certain height for ListView in the xml code. WRAP_CONTENT will not expand your ListView when data is inflated into it. Specify some height like 200dp or something.
I have Listview that contains a Textview, I populate data from sqlite inside them , now I want to change the textview background color for each row based on the value in each one.
I couldn't do it , can anybody help me for how to get each textview from the listview ??
Edit :
Here is my code to populate the listview:
sql = new SqlCommands(getApplicationContext());
Fields = new String[]{SqlCommands.Group, SqlCommands.Subject, SqlCommands.Body, SqlCommands.DateTime};
int [] Dview = new int []{R.id.grop, R.id.sbj,R.id.bdy,R.id.DT};
Cursor c = sql.GetData();
Adpt = new SimpleCursorAdapter(getApplicationContext(),R.layout.items, c , Fields, Dview ,0 );
NotiLst.setAdapter(Adpt);
this is the XML file for the items :
<?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:orientation="horizontal"
android:background="#drawable/grp_shap" >
<ImageView android:id="#+id/Prio"
android:layout_width="32dip"
android:layout_height="32dip"
/>
<TextView
android:id="#+id/grop"
android:layout_width="64sp"
android:layout_height="40sp"
android:layout_alignBottom="#+id/Prio"
android:layout_alignParentLeft="true"
android:text="S"
android:textSize="12sp" />
<TextView
android:id="#+id/DT"
android:layout_width="wrap_content"
android:layout_height="14sp"
android:layout_alignParentRight="true"
android:layout_alignTop="#id/title"
android:gravity="right"
android:text="Date Rec"
android:layout_marginRight="5dip"
android:textSize="12sp"
/>
<TextView
android:id="#+id/bdy"
android:layout_width="fill_parent"
android:layout_height="28sp"
android:layout_below="#+id/DT"
android:layout_toLeftOf="#+id/hi"
android:layout_toRightOf="#+id/Prio"
android:text="Body"
android:textSize="12sp" />
<TextView
android:id="#+id/sbj"
android:layout_width="fill_parent"
android:layout_height="12sp"
android:layout_alignLeft="#+id/bdy"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/bdy"
android:fontFamily="sans-serif"
android:text="Subject"
android:textSize="12sp" />
<ImageView
android:id="#+id/hi"
android:layout_width="20dip"
android:layout_height="26dip"
android:layout_alignLeft="#+id/DT"
android:layout_below="#+id/DT"
android:layout_marginLeft="15dp"
android:src="#drawable/highprio" />
</RelativeLayout>
and I have a listview on the activity_mail layout which populated whith this items.
I want to change the color for the textview with id grop based on the value inserted on it
You should use a custom adapter:
public class MyAdapter extends BaseAdapter {
private static class ViewHolder {
public final TextView mTv;
}
private final Context mContext;
private final List<String> mList; //you can change this to String array
public MyAdapter (Context context, List<String> list) {
this.mContext = context;
this.mList= list;
}
#Override
public int getCount() {
return mList.size();
}
#Override
public String getItem(int position) {
return mList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder h;
final String string = mList.get(position);
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.item_listview, null);
h = new ViewHolder();
h.mTv= (TextView) convertView .findViewById(R.id.mTvRes);
convertView.setTag(h);
} else {
h = (ViewHolder) convertView.getTag();
}
h.mTv.setText....
h.mTv.setTextColor....
h.mTv.setBackground....
return convertView;
}
}
in your activity:
List<String> list = Arrays.asList(Fields);
MyAdapter adapter = new MyAdapter(this, list);
NotiLst.setAdapter(adapter);
I didn't checked it and you maybe need to do some changes but it should work.
If you have something like this:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(resource, parent, false);
viewHolder = new ViewHolder();
viewHolder.label = (TextView) convertView.findViewById(android.R.id.text1);
viewHolder.label.setBackground(getResources().getColor(color.white));
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.label.setText(activitiesList.get(position).getName());
return convertView;
}
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 need to override List of dropdown spinner items when Spinner id in dialog mode (android:spinnerMode="dialog"). I need this to define my own list divider.
I have found dropDownListViewStyle item in application Theme, which contains divider item. And it works, but only for android:spinnerMode="dropdown".
Can I have the same effect for "dialog" mode?
Try this:
<Spinner
android:id="#+id/spinnerAddToList"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:theme="#style/ThemeOverlay.AppCompat.Light"
android:spinnerMode="dialog"/>
Dropdown layout spinner_item_line_drop.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="3dp"
android:paddingRight="3dp">
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="18sp" />
<LinearLayout
android:id="#+id/separator"
android:layout_height="1dp"
android:layout_width="match_parent"
android:background="#color/colorPrimary"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
</LinearLayout>
Main View layout: spinner_item_line_main.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="wrap_content"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:paddingTop="10dp">
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="18sp" />
</LinearLayout>
SpinnerAdapter:
public class SpinnerLineAdapter extends BaseAdapter {
private List<MyListEntity> values;
public SpinnerLineAdapter(List<MyListEntity> values)
{
this.values = values;
}
public void setValue(List<MyListEntity> list)
{
this.values = list;
}
#Override
public int getCount() {
return values.size();
}
#Override
public MyListEntity getItem(int position) {
return values.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
ViewHolder viewHolder;
Context context = parent.getContext();
if (convertView == null) {
itemView = LayoutInflater.from(context).inflate(R.layout.spinner_item_line_main, parent, false);
viewHolder = new ViewHolder();
viewHolder.textView = itemView.findViewById(R.id.text_view);
itemView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) itemView.getTag();
}
viewHolder.textView.setText(values.get(position).getName());
return itemView;
}
public View getDropDownView(int position, View convertView,ViewGroup parent) {
View itemView = convertView;
ViewHolder viewHolder;
Context context = parent.getContext();
if (convertView == null) {
itemView = LayoutInflater.from(context).inflate(R.layout.spinner_item_line_drop, parent, false);
viewHolder = new ViewHolder();
viewHolder.textView = itemView.findViewById(R.id.text_view);
itemView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) itemView.getTag();
}
viewHolder.textView.setText(values.get(position).getName());
return itemView;
}
private static class ViewHolder {
TextView textView;
}
}
Activity:
SpinnerLineAdapter spinnerAddToListAdapter = new SpinnerLineAdapter(list);
spinnerAddToList.setAdapter(spinnerAddToListAdapter);
spinnerAddToList.setSelection(0, false);
spinnerAddToList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
MyListEntity list = spinnerAddToListAdapter.getItem(position);
viewModel.addList(list.id);
viewModel.update(myEntity);
}
#Override
public void onNothingSelected(AdapterView<?> adapter) {
}
});
Button click to show dialog:
mButtonAdd.setOnClickListener(view -> {
spinnerAddToList.performClick();
});