【问题标题】:get data from search and make suggestions and show them从搜索中获取数据并提出建议并显示它们
【发布时间】:2015-05-09 07:26:01
【问题描述】:

我在操作栏中有一个搜索框,并希望根据我在数据库中的数据显示自定义建议。

我阅读了有关进行搜索和建议的谷歌开发者页面,但每个页面都将我连接到许多其他页面,我很困惑!!

其他网站也没有什么好的教程。

请有人在这里给我一个关于如何从 serach 接收数据,然后如何提出建议并在搜索下显示它们的分步教程。

我的第二个问题是我的建议数量有限制吗? 例如,如果我返回了 100 条建议,即使使用滚动条,android 也会显示所有这些建议?

【问题讨论】:

    标签: android search search-suggestion


    【解决方案1】:

    试试下面的例子。

    row_layout.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/name"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" 
            android:textStyle="bold"
            />
    
        <TextView
            android:id="@+id/dist"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="8dp"
            android:textStyle="italic"/>
    
    </LinearLayout>
    

    img_row_layout.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="wrap_content" >
    
        <ImageView
            android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:paddingLeft="0dip"
            android:src="@drawable/planet" />
    
        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/img"
            android:layout_toRightOf="@+id/img"
            android:textStyle="bold" />
    
        <TextView
            android:id="@+id/dist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/name"
            android:layout_gravity="center"
            android:layout_marginTop="2dip"
            android:layout_toRightOf="@id/img"
            android:gravity="right"
            android:textSize="8dp"
            android:textStyle="italic" />
    
    </RelativeLayout>
    

    main.xml

    <LinearLayout 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:orientation="vertical"
        android:focusable="true"
        android:focusableInTouchMode="true">
    
        <EditText
            android:id="@+id/editTxt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:maxLines="1" />
    
        <ListView android:id="@+id/listView" 
                  android:layout_height="match_parent"
                  android:layout_width="match_parent"
                  android:layout_weight="1"/>
    
    
        <Button android:id="@+id/addBtn"
                android:text="Add planet"
                android:onClick="addPlanet"
                android:layout_weight="0.5"
                android:layout_height="80dp"
                android:layout_width="match_parent"/>
    
    
    </LinearLayout>
    

    PlanetAdapter.java

    import java.util.ArrayList;
    import java.util.List;
    
    import android.content.Context;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.BaseAdapter;
    import android.widget.Filter;
    import android.widget.Filterable;
    import android.widget.TextView;
    
    public class PlanetAdapter extends ArrayAdapter<Planet> implements Filterable {
    
        private List<Planet> planetList;
        private Context context;
        private Filter planetFilter;
        private List<Planet> origPlanetList;
    
        public PlanetAdapter(List<Planet> planetList, Context ctx) {
            super(ctx, R.layout.img_row_layout, planetList);
            this.planetList = planetList;
            this.context = ctx;
            this.origPlanetList = planetList;
        }
    
        public int getCount() {
            return planetList.size();
        }
    
        public Planet getItem(int position) {
            return planetList.get(position);
        }
    
        public long getItemId(int position) {
            return planetList.get(position).hashCode();
        }
    
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
    
            PlanetHolder holder = new PlanetHolder();
    
            // First let's verify the convertView is not null
            if (convertView == null) {
                // This a new view we inflate the new layout
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = inflater.inflate(R.layout.img_row_layout, null);
                // Now we can fill the layout with the right values
                TextView tv = (TextView) v.findViewById(R.id.name);
                TextView distView = (TextView) v.findViewById(R.id.dist);
    
    
                holder.planetNameView = tv;
                holder.distView = distView;
    
                v.setTag(holder);
            }
            else 
                holder = (PlanetHolder) v.getTag();
    
            Planet p = planetList.get(position);
            holder.planetNameView.setText(p.getName());
            holder.distView.setText("" + p.getDistance());
    
    
            return v;
        }
    
        public void resetData() {
            planetList = origPlanetList;
        }
    
    
        /* *********************************
         * We use the holder pattern        
         * It makes the view faster and avoid finding the component
         * **********************************/
    
        private static class PlanetHolder {
            public TextView planetNameView;
            public TextView distView;
        }
    
    
    
        /*
         * We create our filter 
         */
    
        @Override
        public Filter getFilter() {
            if (planetFilter == null)
                planetFilter = new PlanetFilter();
    
            return planetFilter;
        }
    
    
    
        private class PlanetFilter extends Filter {
    
    
    
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();
                // We implement here the filter logic
                if (constraint == null || constraint.length() == 0) {
                    // No filter implemented we return all the list
                    results.values = origPlanetList;
                    results.count = origPlanetList.size();
                }
                else {
                    // We perform filtering operation
                    List<Planet> nPlanetList = new ArrayList<Planet>();
    
                    for (Planet p : planetList) {
                        if (p.getName().toUpperCase().startsWith(constraint.toString().toUpperCase()))
                            nPlanetList.add(p);
                    }
    
                    results.values = nPlanetList;
                    results.count = nPlanetList.size();
    
                }
                return results;
            }
    
            @Override
            protected void publishResults(CharSequence constraint,
                    FilterResults results) {
    
                // Now we have to inform the adapter about the new list filtered
                if (results.count == 0)
                    notifyDataSetInvalidated();
                else {
                    planetList = (List<Planet>) results.values;
                    notifyDataSetChanged();
                }
    
            }
    
        }
    }
    

    行星.java

    public class Planet {
    
        private String name;
        private Integer distance;
        private String descr;
        private int idImg;
    
    
    
        public Planet(String name, Integer distance) {
            this.name = name;
            this.distance = distance;
        }
    
        public Planet(String name, String descr) {
            this.name = name;
            this.descr = descr;
        }
    
        public Planet(String name, Integer distance, String descr, int idImg) {
            this.name = name;
            this.distance = distance;
            this.descr = descr;
            this.idImg = idImg;
        }
    
    
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Integer getDistance() {
            return distance;
        }
        public void setDistance(Integer distance) {
            this.distance = distance;
        }
        public int getIdImg() {
            return idImg;
        }
        public void setIdImg(int idImg) {
            this.idImg = idImg;
        }
    
    
    }
    

    Main.java

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.Dialog;
    import android.content.Context;
    import android.text.Editable;
    import android.text.InputType;
    import android.text.TextWatcher;
    import android.view.ContextMenu;
    import android.view.ContextMenu.ContextMenuInfo;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.inputmethod.InputMethodManager;
    import android.widget.AdapterView;
    import android.widget.AdapterView.AdapterContextMenuInfo;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
        // The data to show
        List<Planet> planetsList = new ArrayList<Planet>();
        PlanetAdapter aAdpt;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            initList();
    
            // We get the ListView component from the layout
            ListView lv = (ListView) findViewById(R.id.listView);
    
    
            // This is a simple adapter that accepts as parameter
            // Context
            // Data list
            // The row layout that is used during the row creation
            // The keys used to retrieve the data
            // The View id used to show the data. The key number and the view id must match
            //aAdpt = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, planetsList);
            aAdpt = new PlanetAdapter(planetsList, this);
            lv.setAdapter(aAdpt);
    
            // React to user clicks on item
    //        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    //
    //          public void onItemClick(AdapterView<?> parentAdapter, View view, int position,
    //                  long id) {
    //              
    //              
    //              // We know the View is a <extView so we can cast it
    //              TextView clickedView = (TextView) view;
    //
    //              Toast.makeText(MainActivity.this, "Item with id ["+id+"] - Position ["+position+"] - Planet ["+clickedView.getText()+"]", Toast.LENGTH_SHORT).show();
    //
    //          }
    //         });
    
              // we register for the contextmneu        
              registerForContextMenu(lv);
    
              // TextFilter
              lv.setTextFilterEnabled(true);
              EditText editTxt = (EditText) findViewById(R.id.editTxt);          
    
              editTxt.addTextChangedListener(new TextWatcher() {
    
                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        System.out.println("Text ["+s+"] - Start ["+start+"] - Before ["+before+"] - Count ["+count+"]");
                        if (count < before) {
                            // We're deleting char so we need to reset the adapter data
                            aAdpt.resetData();
                        }
    
                        aAdpt.getFilter().filter(s.toString());
    
                    }
    
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count,
                            int after) {
    
                    }
    
                    @Override
                    public void afterTextChanged(Editable s) {
    
                    }
                });
            }
    
    
         // We want to create a context Menu when the user long click on an item
        @Override
        public void onCreateContextMenu(ContextMenu menu, View v,
                ContextMenuInfo menuInfo) {
    
            super.onCreateContextMenu(menu, v, menuInfo);
            AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) menuInfo;
    
            // We know that each row in the adapter is a Map
            Planet planet =  aAdpt.getItem(aInfo.position);
    
            menu.setHeaderTitle("Options for " + planet.getName());
            menu.add(1, 1, 1, "Details");
            menu.add(1, 2, 2, "Delete");
    
        }
    
    
    
    
        // This method is called when user selects an Item in the Context menu
        @Override
        public boolean onContextItemSelected(MenuItem item) {
            int itemId = item.getItemId();
            AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) item.getMenuInfo();
            planetsList.remove(aInfo.position);
            aAdpt.notifyDataSetChanged();
            return true;
        }
    
    
        private void initList() {
            // We populate the planets
    
            planetsList.add(new Planet("Mercury", 10));
            planetsList.add(new Planet("Venus", 20));
            planetsList.add(new Planet("Mars", 30));
            planetsList.add(new Planet("Jupiter", 40));
            planetsList.add(new Planet("Saturn", 50));
            planetsList.add(new Planet("Uranus", 60));
            planetsList.add(new Planet("Neptune", 70));
    
    
        }
    
    
        // Handle user click
        public void addPlanet(View view) {
            final Dialog d = new Dialog(this);
            d.setContentView(R.layout.dialog);
            d.setTitle("Add planet");
            d.setCancelable(true);
    
            final EditText edit = (EditText) d.findViewById(R.id.editTextPlanet);
            Button b = (Button) d.findViewById(R.id.button1);
            b.setOnClickListener(new View.OnClickListener() {
    
                public void onClick(View v) {
                    String planetName = edit.getText().toString();
                    MainActivity.this.planetsList.add(new Planet(planetName, 0));
                    MainActivity.this.aAdpt.notifyDataSetChanged(); // We notify the data model is changed
                    d.dismiss();
                }
            });
    
            d.show();
        }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多