First of all Happy New Year to everyone!! Second I would like to know if somenone can help me with this problem. I have a ListActivity in which every row has a spinner to select a batch number.
<?xml version="1.0" encoding="utf-8"?>
<com.picking.utils.RelativeLayout
android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relativeLayoutPickingRow"
android:layout_width="match_parent"
android:layout_height="70dp"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal"
>
<LinearLayout
android:id="#+id/linearLayoutPickingRow"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:baselineAligned="false"
android:orientation="horizontal"
android:paddingLeft="10dp"
>
//TextViews and other things
<Spinner
android:id="#+id/spinnerLote"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="40dp"
android:layout_marginTop="5dp"
android:background="#drawable/spinner_bg"
android:focusable="false"
android:spinnerMode="dialog"
android:visibility="visible"
/>
</LinearLayout>
what I want to achive is to "simulate" the onListItemClick method when a bacthnumber is selected
protected void onListItemClick(ListView l, View view, int position, long id)
I have a custom spinner adapter and a custom adapter too for the list with a holder pattern
package com.picking.adapters;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.picking.POJOS.LotesAbiertos;
import com.pickingR;
import java.util.ArrayList;
public class SpinnerCustomAdapter<L> extends ArrayAdapter<LotesAbiertos> {
private ArrayList<LotesAbiertos> list;
private Activity context;
Resources resources;
public SpinnerCustomAdapter(Activity activity, int resource, int textViewResourceId, ArrayList<LotesAbiertos> list) {
super(activity, resource, textViewResourceId, list);
this.list = list;
this.context = activity;
resources = activity.getResources();
}
#Override
public int getCount() {
return list.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View row = inflater.inflate(R.layout.custom_spinner, parent, false);
LotesAbiertos item = list.get(position);
TextView tvLote = (TextView) row.findViewById(android.R.id.text1);
if (item != null) { // Parse the data from each object and set it.
if (item.status.equalsIgnoreCase("2")) {
tvLote.setText("SIN LOTES");
} else {
tvLote.setText(item.loteFormateado);
}
}
return row;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) { // This view starts when we click the spinner.
final int positionRow = position;
View row = convertView;
if (row == null) {
LayoutInflater inflater = context.getLayoutInflater();
row = inflater.inflate(R.layout.custom_spinner_drop, parent, false);
}
TextView tvLote = (TextView) row.findViewById(android.R.id.text1);
TextView tvAcceso = (TextView) row.findViewById(android.R.id.text2);
LotesAbiertos item = list.get(position);
if (item != null) { // Parse the data from each object and set it.
if (position == 0) {
tvLote.setText("Lotes");
row.setBackgroundColor(resources.getColor(R.color.green_ligth));
} else {
if (item.status.equalsIgnoreCase("2")) {
tvLote.setText("SIN LOTES");
} else {
tvLote.setText(item.loteFormateado.substring(0, 5));
Spannable secondWord = new SpannableString(item.loteFormateado.substring(item.loteFormateado.length() - 3));
secondWord.setSpan(new StyleSpan(Typeface.BOLD), 0, secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tvLote.append(secondWord);
}
row.setBackgroundColor(resources.getColor(R.color.white));
}
if (item.status.equalsIgnoreCase("0") || item.status.equalsIgnoreCase("2")) {
tvLote.setTextColor(resources.getColor(R.color.green_dark));
tvAcceso.setVisibility(View.INVISIBLE);
} else {
tvLote.setTextColor(resources.getColor(R.color.dark_grey));
tvAcceso.setVisibility(View.VISIBLE);
}
}
return row;
}
}
Considering I have the adapters and the activity in different packages I can`t call the method as it is protected, but I can have a method like this.
public void fireOnListItemClick(View view, int position, long id) {
onListItemClick(getListView(), view, position, id);
}
I can call this method from the spinnner adapter but the question is how I get the
view, position, id
parameters of the row of the listview to pass them to the method? Shoud I have in the spinner adapter a setOnClickListener like this?
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
/*..........*/
row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/*Call fireOnListItemClick*/
}
});
return row;
}
Thanks in advance for you time
Have a look at the OnItemSelectedListener
Example usage
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
tv.setText("Spinner selected : ");
tv.setText(tv.getText() + parent.getItemAtPosition(position).toString());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
//Another interface callback
}
});
Related
I am implementing an android application that has a GridView where every item is an ImageView with TextView below, I implemented the adapter and the GridView displays correctly, but it is supposed that when I click an image an other screen (Activity is launched) but unfortunately it is not the case here is my code:
the gridView_layout:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
>
<GridView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/videosGrid"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:numColumns="auto_fit"
android:background="#000"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:columnWidth="120dp"
android:stretchMode="spacingWidthUniform"></GridView>
</GridLayout>
the grid_item_layout:
<?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">
<ImageView
android:id="#+id/video_thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:minWidth="170dp"
android:minHeight="140dp"
android:scaleType="fitXY"
android:clickable="true"/>
<!-- android:src="#drawable/alternative_rock_music" -->
<TextView android:id="#+id/video_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:textColor="#color/red"
android:textStyle="bold"
/>
</LinearLayout>
the adapter:
package com.app.utils;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.app.R;
import com.app.entities.VideoEntity;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.List;
public class MyGridViewAdapter extends BaseAdapter {
private static final String TAG = MyGridViewAdapter.class.getSimpleName();
private Context context;
private List<VideoEntity> videosList;
public MyGridViewAdapter(Context context) {
this.context = context;
}
#Override
public int getCount() {
return videosList.size();
}
#Override
public Object getItem(int position) {
return videosList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public List<VideoEntity> getVideosList() {
return videosList;
}
public void setVideosList(List<VideoEntity> videosList) {
this.videosList = videosList;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d(TAG," getView(int, View, viewGroup - Ini ");
View row = convertView;
ViewHolder holder = null;
if(row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.video_item ,parent, false);
holder = new ViewHolder(row);
row.setTag(holder);
}
else
{
holder = (ViewHolder) row.getTag();
}
VideoEntity tmp = videosList.get(position);
Picasso.with(context).load(tmp.getThumbnailURL()).into(holder.myVideo);
holder.title.setText(tmp.getTitle());
Log.d(TAG, " getView(int, View, viewGroup - Fi ");
return row;
}
class ViewHolder {
ImageView myVideo;
TextView title;
public ViewHolder(View v) {
Log.d(TAG," ViewHolder(View) - Ini ");
myVideo = (ImageView) v.findViewById(R.id.video_thumbnail);
title = (TextView) v.findViewById(R.id.video_title);
Log.d(TAG," ViewHolder(View) - Fi ");
}
}
}
and here is the activity where i am supposed to catch the click event on every item of the gridView:
package com.app.activities
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.GridView;
import com.app.entities.VideoEntity;
import com.app.utils.MyGridViewAdapter;
import java.util.ArrayList;
import static android.widget.AdapterView.*;
public class VideosCatalogActivity extends Activity implements AdapterView.OnItemSelectedListener, AdapterView.OnClickListener{
private static final String TAG = VideosCatalogActivity.class.getSimpleName();
private GridView videosGrid;
private ArrayList<VideoEntity> videosList;
#Override
protected void onCreate(Bundle savedInstanceState){
Log.d(TAG, " onCreate(Bundle) - Ini ");
super.onCreate(savedInstanceState);
setContentView(R.layout.videos_catalog_layout);
Bundle bundle = getIntent().getExtras();
videosList = bundle.getParcelableArrayList("com.app.entities.VideoEntity" );
videosGrid = (GridView) findViewById(R.id.videosGrid);
MyGridViewAdapter adapter = new MyGridViewAdapter(this);
adapter.setVideosList(videosList);
videosGrid.setAdapter(adapter);
videosGrid.setOnItemSelectedListener(this);
Log.d(TAG, " onCreate(Bundle) - Fi ");
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, " onItemClick(AdapterView<?>, View, int, long) - Ini ");
Intent intent = new Intent(getApplicationContext(),YoutubePlayerActivity.class);
intent.putExtra("VIDEO_ID",videosList.get(position).getId());
startActivity(intent);
Log.d(TAG, " onItemClick(AdapterView<?>, View, int, long) - Fi ");
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, " onItemClick(AdapterView<?>, View, int, long) - Ini ");
Intent intent = new Intent(getApplicationContext(),YoutubePlayerActivity.class);
intent.putExtra("VIDEO_ID",videosList.get(position).getId());
startActivity(intent);
Log.d(TAG, " onItemClick(AdapterView<?>, View, int, long) - Fi ");
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
#Override
public void onClick(View v) {
}
/*#Override
public void onNothingSelected(AdapterView<?> parent) {
} */
}
I tried to implement two different onClickListeners methods but none is working.
I don't know what i did wrong ???
Any help will be very appreciated.
Note: i can confirm that it is not a manifest problem, because i debugged and nothing happens when i click an image.
Change
`implements AdapterView.OnItemSelectedListener, AdapterView.OnClickListener`
to
`implements AdapterView.OnItemClickListener`
Change
`videosGrid.setOnItemSelectedListener(this)`
to
`videosGrid.setOnItemClickListener(this)`
Remove android:clickable="true" in ImageView
Remove both interfaces
AdapterView.OnItemSelectedListener
AdapterView.OnClickListener and respective methods.
and implement the AdapterView.OnItemClickListener
You need to clarify that AdapterView.OnItemClickListener is different to AdapterView.OnItemSelectedListener and AdapterView.OnClickListener also.
Your activity should implement AdapterView.OnItemClickListener, override it and you will get item click event. Remember to add this line: videosGrid.setOnItemClickListener(this).
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.
This is the Select.java file that populates the spinners
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class select extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select);
final Spinner s1 = (Spinner) findViewById(R.id.s1);
ArrayAdapter s1adapter = ArrayAdapter.createFromResource(this, R.array.s1,R.layout.spin);
s1adapter.setDropDownViewResource(R.layout.spin);
s1.setAdapter(s1adapter);
final ArrayList<String> array = new ArrayList<>();
s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
array.add("1");
long iid = parent.getSelectedItemId();
if (iid == 0) {
array.clear();
array.add("id 1");
} else if (iid == 1) {
array.clear();
array.add("id 2");
array.add("id 3");
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Spinner s2 = (Spinner) findViewById(R.id.s2);
Adapter s2adapter = new Adapter(this,R.layout.spin,array);
s2.setAdapter(s2adapter);
final String l = null;
s2.setSelection(0);
s2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Intent less = new Intent(getBaseContext(),less.class);
Toast.makeText(getBaseContext(),"in",Toast.LENGTH_SHORT);
if (parent.getItemAtPosition(position).equals("id 1")) {
less.putExtra("lesson_1", l);
Toast.makeText(getBaseContext(),"in",Toast.LENGTH_SHORT);
} else if (parent.getItemAtPosition(position).equals("id 2")) {
less.putExtra("lesson_2", l);
} else if (parent.getItemAtPosition(position).equals("id 3")) {
less.putExtra("lesson_3", l);
}
startActivity(less);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
The following is spin.xml the custom view for spinner
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/testing"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top"
android:singleLine="true"
android:textSize="20sp"
android:typeface="sans"
android:textStyle="bold"
android:textColor="#android:color/white"
android:background="#android:color/black"/>
The following is the adapter to inflate the view in the spinner
import android.content.Context;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class Adapter extends ArrayAdapter<String> {
Context c;
ArrayList<String> list;
public Adapter(Context context, int resource, ArrayList<String> list) {
super(context,R.layout.spin, list);
this.c=context;
this.list=list;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
LayoutInflater inf =(LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inf.inflate(R.layout.spin,null);}
TextView a =(TextView)convertView.findViewById(R.id.testing);
a.setText(list.get(position));
return convertView;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
LayoutInflater inf =(LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inf.inflate(R.layout.spin,null);}
TextView a =(TextView)convertView.findViewById(R.id.testing);
a.setText(list.get(position));
return convertView;
}
}
The spinner doesn't show any items initially but does on click. Items are not getting selected. Proof is the makeToast isn't working.
Kindly suggest something.
try this
allusers.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// clicked item will be shown as spinner
Toast.makeText(getApplicationContext(),""+parent.getItemAtPosition(position).toString(),Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Refer here:-
http://coderzpassion.com/android-spinner-get-data-from-sqlite-database/
I have created a spinner. In which, i get spinner item from JSON file.
Here, is Adapter.java
package adapter;
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;
import android.widget.Toast;
import com.angelnx.cricvilla.cricvilla.R;
import java.util.List;
public class ScheduleSpinnerAdapter extends ArrayAdapter<String>{
private Context context;
private List<String> arr_data;
public ScheduleSpinnerAdapter(Context context,int resource,List<String> arr_data){
super(context,resource,arr_data);
this.context=context;
this.arr_data=arr_data;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.spinner_row,parent,false);
TextView txtname=(TextView)convertView.findViewById(R.id.spinner_value);
txtname.setText(this.arr_data.get(position));
// convertView.setOnClickListener(new View.OnClickListener() {
// #Override
// public void onClick(View v) {
// Toast.makeText(context, "Selected"+position, Toast.LENGTH_SHORT).show();
// }
// });
return convertView;
}
#Override
public View getDropDownView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.spinner_row,parent,false);
TextView txtname=(TextView)convertView.findViewById(R.id.spinner_value);
txtname.setText(this.arr_data.get(position));
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Selected" + position, Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}
Here is my spinner_row.xml which inflate in adapter's both method's.
<?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="40dp">
<TextView
android:id="#+id/spinner_value"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="what"
android:padding="10dp"
android:gravity="center|start"
android:textColor="#color/txtColor"/>
</LinearLayout>
Here, is set the values to adapter
ArrayAdapter adapter=new ScheduleSpinnerAdapter(getContext(),android.R.layout.simple_spinner_item,item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Problem is after selecting an item form drowpdown. Drowpdown is not hide in my case.
Please help me to solve out this problem.
Calling onDetachedFromWindow() will hide the Spinner dropdown.
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Spinner spinner = (Spinner) context.findViewById(R.id.mySpinner);
if (spinner != null) {
try {
Method method = Spinner.class.getDeclaredMethod("onDetachedFromWindow");
method.setAccessible(true);
method.invoke(spinner);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
I've created a ListView that has an EditText in each row. The ListView also allows the user to delete an item in the list by swiping horizontally using SwipeDismissListViewTouchListener.java from https://github.com/romannurik/Android-SwipeToDismiss.
Both functionality work well, except when the user tries to do the swiping gesture with the initial ACTION_DOWN event occurring over the EditText. It looks like the EditText is consuming the ACTION_DOWN event before it can be passed to the SwipeDismissListViewTouchListener, which needs the ACTION_DOWN event to initialize the gesture.
Question:
How can I get the SwipeDismissListViewTouchListener to work when the swipe gesture is initiated on the EditText?
SampleListFragment.java:
import android.app.Activity;
import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class SampleListFragment extends ListFragment {
protected AddGroupAdapter adapter;
protected ListView lv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new AddGroupAdapter(getActivity());
setListAdapter(adapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_sample, container, false);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
lv = getListView();
SwipeDismissListViewTouchListener listener =
new SwipeDismissListViewTouchListener(
lv,
new SwipeDismissListViewTouchListener.DismissCallbacks() {
#Override
public boolean canDismiss(int position) {
return true;
}
#Override
public void onDismiss(ListView listView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
adapter.removeRecord(position);
}
adapter.notifyDataSetChanged();
}
});
lv.setOnTouchListener(listener);
lv.setOnScrollListener(listener.makeScrollListener());
super.onActivityCreated(savedInstanceState);
}
public class AddGroupAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<String> data;
public AddGroupAdapter(Activity activity) {
mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
data = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
data.add("Sample Text " + String.valueOf(i));
}
notifyDataSetChanged();
}
public void removeRecord(int position) {
data.remove(position);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.row, null);
holder.edit = (EditText) convertView.findViewById(R.id.edit_text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.edit.setText(data.get(position));
holder.edit.setId(position);
holder.edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if (!hasFocus) {
final int position = view.getId();
final EditText edit = (EditText) view;
if (edit.getText() != null && !edit.getText().toString().isEmpty())
if (position < getCount())
data.set(position, edit.getText().toString());
}
}
});
return convertView;
}
}
static class ViewHolder {
EditText edit;
}
}
row.xml:
<?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">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="#+id/edit_text"
android:hint="Enter Text"
android:gravity="center_horizontal"
android:singleLine="true" />
</LinearLayout>
*fragment_sample.xml:*
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#android:id/list"
android:name="com.example.app.SampleListFragment"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1.0"
android:descendantFocusability="beforeDescendants"
tools:context=".SampleActivity"
tools:layout="#android:layout/list_content"/>
</LinearLayout>
Trying adding this to your LinearLayout in the row.xml:
android:descendantFocusability="blocksDescendants"
Edit:
So apparently a EditText is trickier than buttons to have inside a listview. Maybe this answer can put you in the right direction if you must have them in yours: https://stackoverflow.com/a/2680077/362298