How to implement search with Spinner in Android? - android

My code for Spinner is below:
String[] countryNames = {"Select Country", "India", "China", "Australia", "Portugle", "America", "New Zealand"};
Spinner spinner = (Spinner) findViewById(R.id.simpleSpinner);
hintAdapter = new CustomArrayAdapter(getApplicationContext(), R.layout.simple_row,countriesList,getApplicationContext());
spinner.setAdapter(hintAdapter);
I want to implement search in Spinner.
How can I achieve that?

Use SearchableSpinner Lib, there is list of SearchableSpinner Library available just pick one of those which is better https://github.com/search?utf8=%E2%9C%93&q=searchable+spinner

Go for AutocompleteTextview
this example will help you

I found the following solution here:
/**
* A modified Spinner that doesn't automatically select the first entry in the list.
*
* Shows the prompt if nothing is selected.
*
* Limitations: does not display prompt if the entry list is empty.
*/
public class NoDefaultSpinner extends Spinner {
public NoDefaultSpinner(Context context) {
super(context);
}
public NoDefaultSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NoDefaultSpinner(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void setAdapter(SpinnerAdapter orig ) {
final SpinnerAdapter adapter = newProxy(orig);
super.setAdapter(adapter);
try {
final Method m = AdapterView.class.getDeclaredMethod(
"setNextSelectedPositionInt",int.class);
m.setAccessible(true);
m.invoke(this,-1);
final Method n = AdapterView.class.getDeclaredMethod(
"setSelectedPositionInt",int.class);
n.setAccessible(true);
n.invoke(this,-1);
}
catch( Exception e ) {
throw new RuntimeException(e);
}
}
protected SpinnerAdapter newProxy(SpinnerAdapter obj) {
return (SpinnerAdapter) java.lang.reflect.Proxy.newProxyInstance(
obj.getClass().getClassLoader(),
new Class[]{SpinnerAdapter.class},
new SpinnerAdapterProxy(obj));
}
/**
* Intercepts getView() to display the prompt if position < 0
*/
protected class SpinnerAdapterProxy implements InvocationHandler {
protected SpinnerAdapter obj;
protected Method getView;
protected SpinnerAdapterProxy(SpinnerAdapter obj) {
this.obj = obj;
try {
this.getView = SpinnerAdapter.class.getMethod(
"getView",int.class,View.class,ViewGroup.class);
}
catch( Exception e ) {
throw new RuntimeException(e);
}
}
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
try {
return m.equals(getView) &&
(Integer)(args[0])<0 ?
getView((Integer)args[0],(View)args[1],(ViewGroup)args[2]) :
m.invoke(obj, args);
}
catch (InvocationTargetException e) {
throw e.getTargetException();
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
protected View getView(int position, View convertView, ViewGroup parent)
throws IllegalAccessException {
if( position<0 ) {
final TextView v =
(TextView) ((LayoutInflater)getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE)).inflate(
android.R.layout.simple_spinner_item,parent,false);
v.setText(getPrompt());
return v;
}
return obj.getView(position,convertView,parent);
}
}
}

After trying a lot of libraries and method, I finally created my custom searchable spinner. The code I am attaching is at a very preliminary level which I will be updating as I do in my project. I am also writing the complete method of how to use it.
All Layouts
searchable_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/spinner_search_bar"
android:layout_width="match_parent"
android:layout_height="35dp"
android:hint="Select A Company"
android:cursorVisible="false"
android:background="#drawable/white_rect_fillet_border"
android:paddingHorizontal="10dp"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/ledger_list"
android:layout_width="match_parent"
android:layout_height="300dp"
android:visibility="gone"
android:layout_marginHorizontal="10dp"/>
</LinearLayout>
</FrameLayout>
actity_or_frag_layout.xml remember to include this as last and align according to your parent layout.
<FrameLayout
android:id="#+id/spinner_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_marginTop="10dp"
android:background="#drawable/white_rect_fillet_border"
app:layout_constraintTop_toTopOf="parent">
<include layout="#layout/searchable_spinner" />
</FrameLayout>
list_adapter_element.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow>
<TextView
android:id="#+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical=5dp"
android:gravity="center_vertical"/>
</TableRow>
</TableLayout>
All Drawables
white_rect_fillet_border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#android:color/white"/>
<corners android:radius="5dp" />
<stroke android:color="#android:color/darker_gray"
android:width="1dp" />
</shape>
CustomListAdapter.java
public class CustomListAdapter extends
RecyclerView.Adapter<CustomListAdapter.ViewHolder> {
private final Activity context;
private Fragment fragment;
private ArrayList<LedgerListObject> ledgerlist; //replace LedgerListObject with your object or simply String everywhere in this code.
public CustomListAdapter(Activity context, Fragment fragment,
ArrayList<LedgerListObject> ledgerlist) {
this.context = context;
this.fragment = fragment;
this.ledgerlist = ledgerlist;
}
public void updateList(ArrayList<LedgerListObject> newList){
ledgerlist = newList;
notifyDataSetChanged();
}
#NonNull
#Override
public CustomListAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_adapter_element, null, true);
return new ViewHolder(rowView);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
holder.txtTitle.setText(ledgerlist.get(position).LedgerName);
holder.txtTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MyFragment.LedgerID = ledgerlist.get(position).LedgerID; //MyFragment can be replaced with the name of your activity or fragment
MyFragment.ledgerListView.setVisibility(View.GONE);
MyFragment.spinnerSearch.setText(ledgerlist.get(position).LedgerName);
MyFragment.spinnerSearch.setCursorVisible(false);
}
});
}
#Override
public int getItemCount() {
return ledgerlist.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView txtTitle;
ViewHolder(#NonNull View itemView) {
super(itemView);
txtTitle = (TextView) itemView.findViewById(R.id.txt);
}
}
}
MyFragment.java
public class MyFragment extends Fragment{
ArrayList<LedgerListObject> ledgerlist = new ArrayList<LedgerListObject>();
public static int LedgerID = 0;
CustomListAdapter ledgerAdapter;
FrameLayout spinnerFrame;
public static EditText spinnerSearch;
public static RecyclerView ledgerListView;
#SuppressLint("ClickableViewAccessibility")
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState){
View root = inflater.inflate(R.layout.fragment_ledger, container, false);
super.onCreate(savedInstanceState);
spinnerFrame = root.findViewById(R.id.spinner_frame);
spinnerSearch = root.findViewById(R.id.spinner_search_bar);
ledgerListView = root.findViewById(R.id.ledger_list);
spinnerSearch.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
ledgerListView.setVisibility(View.VISIBLE);
spinnerSearch.setCursorVisible(true);
return false;
}
});
spinnerSearch.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
try{
filter(s.toString());
}catch (Exception e){e.printStackTrace();}
}
});
GridLayoutManager listgridLayoutManager = new GridLayoutManager(getContext(), 1,
RecyclerView.VERTICAL, false);
ledgerListView.setLayoutManager(listgridLayoutManager);
//todo: your method of adding objects to your ledgerlist
ledgerAdapter = new CustomListAdapter(getActivity(), LedgerFragment.this, ledgerlist);
ledgerListView.setAdapter(ledgerAdapter);
return root;
}
}
Try it and if there is any issue in this, please feel free to ask and I will resolve it

You Can show Searchable Spinner in Dialog. here is all steps
Step 1: make a TextView inside activity_main.xml.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="#+id/text_view_for_dropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:hint="Select Number"
android:background="#android:drawable/editbox_background"
app:drawableEndCompat="#drawable/ic_dropdown" />
<!-- "#drawable/ic_dropdown" is "vector asset" of down arrow sign -->
</LinearLayout>
Step 2: make new layout (res>layout>layout_searchable_spinner.xml) that we will show inside Dialog.
layout_searchable_spinner.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="match_parent"
android:orientation="vertical"
android:layout_margin="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:fontFamily="monospace"
android:text="Select Number"
/>
<EditText
android:id="#+id/editText_of_searchableSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search..."
android:padding="12dp"
android:singleLine="true"
android:background="#android:drawable/editbox_background"
/>
<ListView
android:id="#+id/listView_of_searchableSpinner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
/>
</LinearLayout>
Step 3: write all code in MainActivity.Java
MainActivity.Java
public class MainActivity extends AppCompatActivity {
ArrayList<String> arrayList;
TextView textViewSpinner;
Dialog dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//provide size to arrayList
arrayList=new ArrayList<>();
//call this spinner function
funCustomSpinner();
}
public void funCustomSpinner(){
//we are adding values in arraylist
arrayList.add("Item 1");
arrayList.add("Item 2");
arrayList.add("Item 3");
arrayList.add("Item 4");
arrayList.add("Item 4");
arrayList.add("Item 4");
//provide id to textview and set onClick lister
textViewSpinner=findViewById(R.id.text_view_for_dropdown);
textViewSpinner.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog=new Dialog(MainActivity.this);
//set (our custom layout for dialog)
dialog.setContentView(R.layout.layout_searchable_spinner);
//set transparent background
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
//show dialog
dialog.show();
//initialize and assign variable
EditText editText=dialog.findViewById(R.id.editText_of_searchableSpinner);
ListView listView=dialog.findViewById(R.id.listView_of_searchableSpinner);
//array adapter
ArrayAdapter<String> arrayAdapter=new ArrayAdapter<>(MainActivity.this,
androidx.appcompat.R.layout.support_simple_spinner_dropdown_item,arrayList);
listView.setAdapter(arrayAdapter);
//Textwatcher for change data after every text type by user
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//filter arraylist
arrayAdapter.getFilter().filter(charSequence);
}
#Override
public void afterTextChanged(Editable editable) {
}
});
// listview onitem click listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
textViewSpinner.setText(arrayAdapter.getItem(i));
Toast.makeText(MainActivity.this, "Selected:"+ arrayAdapter.getItem(i), Toast.LENGTH_SHORT).show();
//dismiss dialog after choose
dialog.dismiss();
}
});
}
});
}
Happy Coding:)

Related

initialize empty EditText in ListView Adapter

I have ListView and it include 2 EditTextI want to initialize 6 empty EditText when activity started.How can I do that?
I can not add list null Model and set it so I dont know how can i handle it.
This is listview_row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/SerialNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:ellipsize="end"
android:padding="5dp"
android:singleLine="true">
<requestFocus />
</EditText>
<EditText
android:id="#+id/countItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:padding="5dp"
android:singleLine="true"
android:inputType="number">
</EditText>
This is my layout;
activity_reader.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content""
android:text="btn" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#null" />
</GridLayout>
Here is the ListView Adapter;
public class ListViewAdapter extends BaseAdapter {
public ArrayList<Model> productList;
Activity activity;
ViewHolder holder;
public ListViewAdapter(Activity activity, ArrayList<Census> productList) {
super();
this.activity = activity;
this.productList = productList;
}
#Override
public int getCount() {
return productList.size();
}
#Override
public Object getItem(int position) {
return productList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
EditText SerialNumber;
EditText countItem;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.listview_row, null);
holder = new ViewHolder();
holder.SerialNumber = (EditText) convertView.findViewById(R.id.SerialNumber );
holder.countItem = (EditText) convertView.findViewById(R.id.countItem );;
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.SerialNumber.addTextChangedListener(new TextWatcher() { #Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
holder.SerialNumber.requestFocus();
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
holder.censusRowSerialNumber.requestFocus();
}
#Override
public void afterTextChanged(Editable s) {
//holder.countItem.requestFocus();
}
});
Model item = productList.get(position);
holder.SerialNumber.setText(item.getID().toString());
holder.countItem.setText(String.valueOf(item.getQuantity()));
return convertView;
}
}
Here is the Activity;
public class ReaderActivity extends AppCompatActivity {
EditText countItem,SerialNumber;
private ArrayList<Model> csList;
Model item;
ListViewAdapter adapter;
ListView lview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reader);
csList = new ArrayList<Model>();
lview = (ListView) findViewById(R.id.listview);
adapter = new ListViewAdapter(this, csList);
SerialNumber= findViewById(R.id.SerialNumber);
countItem = findViewById(R.id.countItem );
lview.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
For achieving CRUD operations, its good to go with Custom Layouts.
For instance a LinearLayout, who's default layout is already defined. Now according to runtime requirement/parameters you can add/edit/delete all these. For which the logic can be completely yours. This is handy than creating a static view and manipulating.
Let me show you custom layouts preparation and adding into a rootiew. I have given example where layout landing_hori_view is having an EditText and Button
public class LandingHorizontalView extends LinearLayout implements View.OnClickListener{
public LandingHorizontalView(Context context){
super(context);
initialize();
}
public LandingHorizontalView(Context context, ModelClassObject modelObject,ButtonClicked listener){
super(context);
mTypeContent = modelObject;//If you want to pass some params for population
mListener = listener;
initialize();
}
private void initialize(){
View view = LayoutInflater.from(getContext()).inflate(R.layout.landing_hori_view, this, true);
EditText edtOne =findViewById(R.id.txt_price_list);
Button button= findViewById(R.id.btn_place_order);
button.setTag(count);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v instanceof Button){
int count = (int) v.getTag();
mListener.onButtonClicked(count);
}
}
public interface ButtonClicked{
public void onButtonClicked(int index);
}
}
In your parent activity, implement the interface (here ButtonClicked) and handle click events/manipulating UI etc
#Override
public void onButtonClicked(int index) {
}
Now coming to , how to add these to parent layout, create a ScrollView and include one LinearLayout to which LandinghorizontalView will be added.
Below shows content of my parent Activity
<ScrollView
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#android:color/white"
app:layout_constraintTop_toBottomOf="#id/guideline"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/landingScrollLinearView"
>
</LinearLayout>
Now for adding this vew , the following can be done.
LandingHorizontalView horizontalView=new LandingHorizontalView(getActivity(),modelObject,this);
mScrollLinearView.addView(horizontalView);
Now as per your comment, create few LandingHorizontalView and add
to root. Now according to your logic/user interaction , keep on
modifying mScrollLinearView

Android SetImageResource failed to work in Gridview on button click

I try to display some items in a Gridview and set button click listener. When the item was clicked, it shows a dialog box, if clicking yes, it will change the Imageview.
java coding
public class table extends AppCompatActivity implements AdapterView.OnItemClickListener{
GridView gridView;
my_adapter my_adapter;
String table_names[]={"1a","1b","1c","1d","2a","2b","2c","2d","3a","3b","3c","3d"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_table);
gridView=(GridView)findViewById(R.id.gridview);
my_adapter=new my_adapter(this,table_names);
gridView.setAdapter(my_adapter);
gridView.setOnItemClickListener(this);
}
#Override
public void onItemClick(final AdapterView<?> adapterView, final View view, final int i, long l) {
new AlertDialog.Builder(this)
.setTitle("Table number: "+adapterView.getItemAtPosition(i).toString())
.setMessage("Open Table?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
my_adapter.change_img(); // change to new image
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
my_adapter.change_img2(); // change back to original image
}
})
.show();
}
}
class my_adapter extends BaseAdapter{
ImageView imageView;
LayoutInflater inflater=null;
Context ctx;
String table_names[];
ArrayList store_table_no;
my_adapter(Context ctx, String table_names[]){
this.ctx=ctx;
this.table_names=table_names;
store_table_no=new ArrayList<Integer>();
for (int i=0;i<table_names.length;i++){
store_table_no.add(table_names[i]);
}
}
#Override
public int getCount() {
return store_table_no.size();
}
#Override
public Object getItem(int i) {
return store_table_no.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View row=view;
if(row==null){
inflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row=inflater.inflate(R.layout.single,null);
}
TextView tv_table_no=(TextView)row.findViewById(R.id.table_no);
imageView=(ImageView)row.findViewById(R.id.imageView);
tv_table_no.setText(""+store_table_no.get(i));
return row;
}
public void change_img(){
imageView.setImageResource(R.drawable.table_full);
}
public void change_img2(){
imageView.setImageResource(R.drawable.table3d2);
}
}
single.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="101"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/table_no"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="22sp" />
<ImageView
app:srcCompat="#drawable/table"
android:id="#+id/imageView"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="130dp"
android:layout_height="100dp" />
</RelativeLayout>
Here is a screenshot, please have a look
It simply doesn't change the imageview. Looks like something is wrong with my coding. anyone knows what's happening ?
You are implementing GridView's OnItemClickListener to show a dialog and getting decision from user for each item. When you are showing up a dialog it needs to update current clicked item of grid. But in your case you have change_img() in adapter. No position specification is given to the adapter.
To make it work, move your logic from activity to adapter. Handle the click events with the positions.
In your activity,
public class table extends AppCompatActivity{
GridView gridView;
my_adapter my_adapter;
String table_names[] = {"1a", "1b", "1c", "1d", "2a", "2b", "2c", "2d", "3a", "3b", "3c", "3d"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid);
gridView = (GridView) findViewById(R.id.grid);
my_adapter = new my_adapter(this, table_names);
gridView.setAdapter(my_adapter);
}
class my_adapter extends BaseAdapter {
LayoutInflater inflater = null;
Context ctx;
String table_names[];
ArrayList<String> store_table_no;
my_adapter(Context ctx, String table_names[]) {
this.ctx = ctx;
this.table_names = table_names;
store_table_no = new ArrayList<>();
store_table_no.addAll(Arrays.asList(table_names));
}
#Override
public int getCount() {
return store_table_no.size();
}
#Override
public Object getItem(int i) {
return store_table_no.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(final int i, View view, ViewGroup viewGroup) {
View row = view;
if (row == null) {
inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.single, null);
}
RelativeLayout layout = (RelativeLayout) row.findViewById(R.id.relative_layout);
TextView tv_table_no = (TextView) row.findViewById(R.id.table_no);
final ImageView imageView = (ImageView) row.findViewById(R.id.imageView);
tv_table_no.setText(store_table_no.get(i));
layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new AlertDialog.Builder(GridActivity.this)
.setTitle("Table number: " + store_table_no.get(i))
.setMessage("Open Table?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
imageView.setImageResource(R.mipmap.ic_launcher);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
imageView.setImageResource(R.drawable.icon);
}
})
.show();
}
});
return row;
}
}
}
In your xml file add id to Parent RElative Layout as follows,
single.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:id="#+id/relative_layout"
android:clickable="true"
android:layout_height="match_parent">
<TextView
android:text="101"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/table_no"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="22sp" />
<ImageView
app:srcCompat="#drawable/table"
android:id="#+id/imageView"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="130dp"
android:layout_height="100dp" />
</RelativeLayout>
I hope it will help you.

How to populate data in AutoCompleteTextView?

I have to show two section in AutoCompleteTextView (Something like this):
I have created a custom layout which have two CardViews and each CardView have three TextViews. Right now I am not distributing the section on the basis of type. The whole data is loaded into one section.
Activity
final AutocompleteLocalityAdapter adapterLocalities = new AutocompleteLocalityAdapter(context,
R.layout.support_simple_spinner_dropdown_item, new ArrayList<Locality>());
AutocompleteLocalityAdapter
public class AutocompleteLocalityAdapter extends ArrayAdapter<Locality> {
public AutocompleteLocalityAdapter(Context context, int layout, List<Locality> localities) {
super(context, layout, localities);
this.localities = localities;
updateList("");
}
In updateList method I am making a new network call to fill the data in Locality class.
What do I need to do to categories the search result as per given image? ArrayAdapter is not going to work here for sure.
The possible solution I am thinking here is:
Replace ArrayAdapter to RecyclerViewAdapter.
Any hint will be appreciable.
The possible makeshift to this solution is PopUpWindow. Inside the PopUpWindow I put two RecyclerView and populate them through network calls.
dashboard_profile_popup_window
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="#dimen/margin_low"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/locationCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="#dimen/corner_radius"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/landmark"
android:textStyle="bold" />
<ListView
android:id="#+id/localityView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/landmarkCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="#dimen/corner_radius"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/location"
android:textStyle="bold" />
<ListView
android:id="#+id/landmarkView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
CustomWidget
public class CustomAutoCompleteView extends EditText {
private Context context;
TextListViewAdapter locationAdapter;
TextListViewAdapter landmarkAdaper;
PopupWindow pwindow;
ClickListener clickListener;
public CustomAutoCompleteView(Context context) {
super(context);
this.context = context;
setCustomization();
}
public void closeWindow(){
pwindow.dismiss();
}
public CustomAutoCompleteView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
setCustomization();
}
public CustomAutoCompleteView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
setCustomization();
}
public void updateList(List<LocalityEntity> locationList, List<LocalityEntity> landmarkList) {
if (pwindow == null) {
initPopupWindow();
}
locationAdapter.updateList(locationList);
landmarkAdaper.updateList(landmarkList);
}
public void initPopupWindow() {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.dashboard_profile_popup_window, null);
ListView landmarkRecyclerView = (ListView) layout.findViewById(R.id.localityView);
ListView localityRecyclerView = (ListView) layout.findViewById(R.id.landmarkView);
landmarkRecyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String text = ((TextView) view.findViewById(R.id.localityText)).getText().toString();
String gid = ((TextView) view.findViewById(R.id.localityGID)).getText().toString();
clickListener.placeSelected(gid);
}
});
localityRecyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String text = ((TextView) view.findViewById(R.id.localityText)).getText().toString();
String gid = ((TextView) view.findViewById(R.id.localityGID)).getText().toString();
clickListener.placeSelected(gid);
}
});
landmarkRecyclerView.setAdapter(landmarkAdaper);
localityRecyclerView.setAdapter(locationAdapter);
pwindow = new PopupWindow(context);
pwindow.setContentView(layout);
pwindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
pwindow.setWidth(this.getWidth());
pwindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
pwindow.setFocusable(true);
pwindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
pwindow = null;
}
});
pwindow.showAsDropDown(this);
}
private void setCustomization() {
locationAdapter = new TextListViewAdapter(getContext());
landmarkAdaper = new TextListViewAdapter(getContext());
initPopupWindow();
}
public void setClickListener(ClickListener clickListener) {
this.clickListener = clickListener;
}
public interface ClickListener {
void placeSelected(String gid);
}
}
Now call this customViewWidget through following code:
place_pop_up.setClickListener(this);
place_pop_up.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
private Timer timer = new Timer();
private final long DELAY = 2000;
#Override
public void afterTextChanged(final Editable s) {
if (s.length() > 3) {
timer.cancel();
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
getAutoCompleteSearchResult(s.toString());
}
}, DELAY);
}
}
});
In getAutoCompleteSearchResult make the network call and call place_pop_up.updateList(locality, landmark);

Failing to populate a ListView with an AsyncTask

Trying to get this working... it loads up fine, even tells the application that it completed getting all the data. It does not populate the listview though.
The data response inside mArrayList.toString(): [A, B, C, D]
public class MainActivity extends ActionBarActivity {
private static final String DEBUG_TAG = "MainActivity";
private boolean mAlternateTitle = false;
ListView lv;
private ArrayList<Show> mArrayList;
ShowsAdapter adapter;
AlertDialog mAlertDialog;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mArrayList = new ArrayList<Show>();
lv = (ListView) findViewById(R.id.list);
adapter = new ShowsAdapter(MainActivity.this, android.R.layout.simple_list_item_1, mArrayList);
ShowsList show_list = new ShowsList();
show_list.execute();
lv.setAdapter(adapter);
lv.setOnItemClickListener(new ListClickListener());
}
private class ShowsList extends AsyncTask<Void, Void, List<Show>> {
#Override
protected void onPreExecute() {
mAlertDialog = new AlertDialog.Builder(MainActivity.this).setIcon(R.drawable.ic_action_refresh).setTitle(R.string.fetching_new).show();
}
#Override
protected List<Show> doInBackground(Void... voids) {
final String DEBUG_TAG = "MainActivity$ShowList$doInBackground";
try {
for (Show show : Show.getShows()) {
Log.d(DEBUG_TAG, show.toString());
mArrayList.add(show);
};
return mArrayList;
} catch (Exception e) {
new AlertDialog.Builder(MainActivity.this.getApplicationContext()).setIcon(android.R.drawable.ic_dialog_alert).setTitle(R.string.server_down_title).setMessage(R.string.server_down_message).setPositiveButton(R.string.app_quit, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
MainActivity.this.finish();
}
}).show();
return null;
}
}
#Override
protected void onPostExecute(final List<Show> show_list) {
if (mAlertDialog.isShowing()) {
mAlertDialog.dismiss();
}
adapter.notifyDataSetChanged();
}
}
private class ListClickListener implements AdapterView.OnItemClickListener {
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Show show = mArrayList.get(i);
Toast.makeText(MainActivity.this, "Clicked on a list item: " + show.title, Toast.LENGTH_LONG).show();
}
}
private class ShowsAdapter extends ArrayAdapter<Show> {
final String DEBUG_TAG = "MainActivity$ShowsAdapter";
public ShowsAdapter(Context context, int textViewResourceId, List<Show> shows) {
super(context, textViewResourceId, shows);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Show show = this.getItem(position);
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.list_row_show, parent, false);
}
((TextView) convertView.findViewById(R.id.show_title)).setText(show.title);
//Log.d(DEBUG_TAG, (String)((TextView) convertView.findViewById(R.id.show_title)).getText());
//((TextView) convertView.findViewById(R.id.episode_number)).setText(episode.getGrayLine());
return convertView;
}
}
Just in case it could be an issue with the layout [main.xml]:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</FrameLayout>
list_show_row.xml:
<?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">
<TextView
android:textSize="17.0dip"
android:textStyle="bold"
android:textColor="#ff000000"
android:gravity="center_vertical"
android:id="#+id/show_title"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:text="Show Title"
android:layout_weight="1.0"
/>
<TextView
android:textStyle="italic" android:textColor="#ff666666"
android:id="#+id/episode_number"
android:layout_width="fill_parent" android:layout_height="0.0dip" android:text="Episode Number" android:layout_weight="1.0" />
</LinearLayout>
Don't set fill_parent on the layout_height of the root element of list_row_show.xml layout.

Android: ListView not refreshing on notifyDataSetChanged();

I've got a custom BaseAdapter and an add button in the main activity. The button opens a dialog with a textbox and you can add new elements to the list that way. The problem is that the list is not refreshing. In the onActivityResult() function I print the number of elements in the list and each time I hit OK in the dialog box the number increases, so I know it's just the refreshing that doesn't work. My BaseAdapter and my activity:
class ListaOrase extends BaseAdapter{
private Activity context;
ArrayList<String> orase;
public ListaOrase(Activity context){
this.context=context;
orase=new ArrayList<String>();
}
public void add(String string){
orase.add(string);
this.notifyDataSetChanged();
}
public View getView (int position, View convertView, ViewGroup list) {
View element;
if (convertView == null)
{
LayoutInflater inflater = context.getLayoutInflater();
element = inflater.inflate(R.layout.lista, null);
}
else element = convertView;
TextView elementLista=(TextView)element.findViewById(R.id.elementLista);
elementLista.setText(orase.get(position));
return element;
}
}
public class WeatherAppActivity extends ListActivity {
Button buton;
ListaOrase lista;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lista=new ListaOrase(this);
buton=(Button)findViewById(R.id.buton);
lista.add("Bucuresti");
lista.add("Sibiu");
setListAdapter(lista);
}
public void add(View view){
Intent intent=new Intent();
intent.setClass(this, Adauga.class);
startActivityForResult(intent, 0);
}
public void onActivityResult (int requestCode, int responseCode, Intent data){
System.out.println("Apelata");
if(responseCode==1){
lista.add(data.getStringExtra("oras")); // e chiar getText()
System.out.println(lista.getCount());
lista.notifyDataSetChanged();
}
}
}
As you can see, I'm trying to refresh (notifyDataSetChanged();) both when adding a new element (in the BaseAdapter extending class) and in method onActivityResult, after the dialog passes the new element to the main Activity. I repeat, the element IS added to the list because the count increases, it just doesn't refresh.
Thanks for your answers!
It's normal that it doesn't refresh, you are adding an item to "lista" but the adapter keeps its own copy of that list, so or you set again the list in the adapter and then you call notifyDataChanged or you add the new item to the adapter.
Anyway I see couple of weird things, I thing you could semplify everything using an array adapter, you don't need to implement add,etc. I wrote some code simplyfing yours:
public class WeatherAppActivity extends ListActivity {
Button buton;
ItemsAdapter lista;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
List<String> initialList = new ArrayList<String>();
initialList.add("Bucuresti");
initialList.add("Sibiu");
lista=new ItemsAdapter(this, initialList);
buton=(Button)findViewById(R.id.button1);
buton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
lista.add(""+System.currentTimeMillis()); // e chiar getText()
lista.notifyDataSetChanged();
}
});
setListAdapter(lista);
}
class ItemsAdapter extends ArrayAdapter<String> {
public ItemsAdapter(Context context, List<String> list) {
super(context, R.layout.lista, list);
}
#Override
public View getView(final int position, View row, final ViewGroup parent) {
final String item = getItem(position);
ItemWrapper wrapper = null;
if (row == null) {
row = getLayoutInflater().inflate(R.layout.lista, parent, false);
wrapper = new ItemWrapper(row);
row.setTag(wrapper);
} else {
wrapper = (ItemWrapper) row.getTag();
}
wrapper.refreshData(item);
return row;
}
class ItemWrapper {
TextView text;
public ItemWrapper(View row) {
text = (TextView) row.findViewById(R.id.elementLista);
}
public void refreshData(String item) {
text.setText(item);
}
}
}
}
These are the xml that I have used:
main.xml
<?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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="63dp"
android:text="Button" />
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
</ListView>
</RelativeLayout>
lista.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="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/elementLista"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
This is the version of the adapter using a baseadapter:
class ItemsBaseAdapter extends BaseAdapter {
private List<String> items;
private Context mContext;
public ItemsBaseAdapter(Context context, List<String> list) {
items = list;
mContext = context;
}
public void addItem(String str) {
items.add(str);
}
#Override
public View getView(final int position, View row, final ViewGroup parent) {
final String item = (String) getItem(position);
ItemWrapper wrapper = null;
if (row == null) {
row = getLayoutInflater().inflate(R.layout.lista, parent, false);
wrapper = new ItemWrapper(row);
row.setTag(wrapper);
} else {
wrapper = (ItemWrapper) row.getTag();
}
wrapper.refreshData(item);
return row;
}
class ItemWrapper {
TextView text;
public ItemWrapper(View row) {
text = (TextView) row.findViewById(R.id.elementLista);
}
public void refreshData(String item) {
text.setText(item);
}
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
And this is the version of the list item wich also include an imageview on the left:
<?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="match_parent"
android:orientation="horizontal" >
<ImageView
android:layout_height="wrap_content"
android:src="#android:drawable/btn_star_big_on"
android:scaleType="fitCenter"
android:layout_width="wrap_content"
/>
<TextView
android:id="#+id/elementLista"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</LinearLayout>

Categories

Resources