Android的ListView和SearchActivity

Android的ListView和SearchActivity,android,listview,search,widget,Android,Listview,Search,Widget,我正在尝试为Android开发一个搜索界面。我的第一个问题与实现listview有关。我正在使用操作栏上的搜索小部件。以下是开场白: public class SearchableActivity extends ListActivity { // class fields @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setC

我正在尝试为Android开发一个搜索界面。我的第一个问题与实现listview有关。我正在使用操作栏上的搜索小部件。以下是开场白:

public class SearchableActivity extends ListActivity {

  // class fields

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);

    ListView lv = (ListView) findViewById(android.R.id.list);
    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {



        }
    });

    // Get the intent, verify the action and get the query
    Intent intent = getIntent();
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        query = intent.getStringExtra(SearchManager.QUERY);

        ProgressDialog dialog = ProgressDialog.show(
                SearchableActivity.this, "Searching",
                "Searching. Please wait...", true);

        performSearch(query);

        dialog.hide();
        dialog.dismiss();
    }
}

public void performSearch(String query) {
    List<String> dataList = new ArrayList<String>();


    dataList = new ArrayList<String>();
    new task().execute(); // Task calls an HttpPost and opens search through php and populates list through JSON


    ArrayAdapter<String> arr = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, dataList);
    this.setListAdapter(arr);

}
公共类SearchableActivity扩展了ListActivity{
//类字段
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
ListView lv=(ListView)findViewById(android.R.id.list);
lv.setOnItemClickListener(新的OnItemClickListener(){
公共链接(AdapterView arg0、视图arg1、内部arg2、,
长arg3){
}
});
//获取意图、验证操作并获取查询
Intent=getIntent();
if(Intent.ACTION_SEARCH.equals(Intent.getAction())){
query=intent.getStringExtra(SearchManager.query);
ProgressDialog=ProgressDialog.show(
SearchableActivity.this,“搜索”,
“正在搜索,请稍候…”,对);
性能搜索(查询);
dialog.hide();
dialog.dismise();
}
}
公共void性能查询(字符串查询){
List dataList=new ArrayList();
dataList=newarraylist();
new task().execute();//task调用HttpPost并通过php打开搜索,通过JSON填充列表
ArrayAdapter arr=新的ArrayAdapter(此,
android.R.layout.simple_list_item_1,dataList);
这个.setListAdapter(arr);
}
  • 在第一种方法中,我正在设置一个ListView,我不确定我需要在单击时执行什么代码?搜索结果在哪里?我希望在用户键入时显示最接近的结果
  • 不确定将xml R.id.list放在哪里?它是否作为下拉菜单内置到小部件中?(例如Play Store中的搜索小部件)
  • 另外,我在这里使用任务并将查询发送到php的实现是否正确
  • 如果您希望对
    列表项执行操作,即一行..就像您有
    产品的列表和
    单击
    一样,您希望显示产品的
    详细信息
    ,然后启动新活动,显示相关产品的详细信息

    Not sure where to put the xml R.id.list? Is it built in to the widget as a drop down? (e.g. the search widget in Play Store)
    
    无需将其添加到
    search.xml
    like

        <ListView
      android:id="@android:id/list"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >
    </ListView> 
    
    不,用

        <ListView
      android:id="@android:id/list"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >
    </ListView> 
    
    Also, is my implementation of using a Task correct in this and sending the query to php?