适用于Android的ListView和SearchActivity

时间:2012-07-17 01:45:51

标签: android listview search widget

我正在试图找出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);

}
  1. 在第一种方法中,我正在设置ListView,我不确定我需要在onClick上执行哪些代码?搜索结果在哪里?我希望在用户输入时显示最接近的结果。
  2. 不确定将xml R.id.list放在何处?它是作为下拉菜单内置到小部件中的吗? (例如Play商店中的搜索小部件)
  3. 另外,我的实现是否正确使用了Task并将查询发送到php?

1 个答案:

答案 0 :(得分:0)

In first method I am setting up a ListView, I am not sure what code I need to do onClick?
Where does the search results go? I'd like to display closest results as the user types.

如果您想对list item即行进行操作..就像您有一个ProductOnClick的列表,您希望显示details of your product,那么您启动您的新活动,其中显示该相关产品的详细信息。

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中添加它...

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

更多阅读this

Also, is my implementation of using a Task correct in this and sending the query to php?

不,使用AsyncTask