我在操作栏中有一个搜索框,想要从我的数据库中显示自定义建议。
我阅读谷歌开发者网页关于进行搜索和建议,但每个页面都将我连接到很多其他页面,我很困惑!!
在其他网站上也没有任何好的教程。
请有人给我这里一步一步的教程,介绍如何从serach接收数据,然后如何提出建议并在搜索中显示它们。
我的第二个问题是我的建议数量有限制吗? 例如,如果我返回100个建议,即使使用滚动条,android也会显示所有这些建议吗?
答案 0 :(得分:0)
尝试以下示例。
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();
}
}
}
}
Planet.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();
}
}