我在列表视图中使用edittext搜索栏时遇到问题。当我输入数据时,索引是被抛出的索引而不是数据本身。可能是什么问题呢。这是我的代码:
package com.apps.mangkepwengslab;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class SearchListActivity extends Activity {
// List view
private ListView lv;
// Listview Adapter
ArrayAdapter<String> adapter;
// Object obj;
// Search EditText
EditText inputSearch;
// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_list);
onSearchRequested();
// Listview Data
String[] commonName =
{
"Acacia",
"Alfalfa",
"Althea",
"Anise",
"Annatto",
"Antidysenteric Holarrhena",
"Arjuna Myrobalan",
"Artichoke",
"Asparagus",
"Avocado",
"Bael Tree",
"Balloon Vine",
"Bamboo",
"Banana",
"Basil",
"Bauhinia",
"Beetroot",
"Bengal Currants",
"Betel Nut Palm",
"Bilimbi",
"Bitter Gourd",
"Black Mulberry",
"Black Oil Plant",
"Black Pepper",
"Brazil Wood",
"Bread Fruit",
"Broad Bean",
"Broccoli",
"Butter Tree",
"Cabbage",
"Cacao",
"Cactus",
"Camphor",
"Carambola",
"Carrot",
"Cashew",
"Castor Bean",
"Catechu Tree",
"Celery",
"Century Plant",
"Chives",
"Cinnamon",
"Clitoria",
"Clove Tree",
"Cluster Fig",
"Coconut",
"Coffee",
"Coriander",
"Corn",
"Cotton",
"Country Borage",
"Cowhage",
"Creat",
"Crown Plant",
"Cucumber",
"Curry Leaf",
"Custard Apple",
"Cypress",
"Deadly Nightshade",
"Dodder",
"Dwarf Nasturtium",
"Eggplant",
"Elephant Creeper",
"Emblic Myrobalan",
"Eucalyptus",
"Flame of the Forest",
"Four Angled Bean",
"Four OClock Plant",
"French Beans",
"Garlic",
"Ginger",
"Golden Trumpet",
"Grape Fruit",
"Guava",
"Hibiscus",
"Hierba Buena",
"Horse Radish Tree",
"Hyacinth Bean",
"Indian Aloe",
"Indian Privet",
"Indian Spinach",
"Ivy Gourd",
"Jack Tree",
"Jasmine",
"Java Plum",
"Jicama",
"Kohlrabi",
"Lantana",
"Lemon Grass",
"Lentil",
"Mandarin Orange",
"Marigold",
};
lv = (ListView) findViewById(R.id.list_view);
inputSearch = (EditText) findViewById(R.id.inputSearch);
// Adding items to listview
adapter = new ArrayAdapter<String>(this, R.layout.list_items, R.id.product_name, commonName);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
id = id + 1 ;
System.out.println("Text ["+id+"]");
long new_id = lv.getItemIdAtPosition(position);
Intent herbIntent = new Intent(getApplicationContext(), HerbActivity.class);
// Object obj = adapter.getItem(position);
// Creating a uri to fetch country details corresponding to selected listview item
Uri data = Uri.withAppendedPath(HerbContentProvider.CONTENT_URI, String.valueOf(id));
// Setting uri to the data on the intent
herbIntent.setData(data);
// countryIntent.setDataAndType(data, "image/jpeg");
// Open the activity
startActivity(herbIntent);
}
});
/**
* Enabling Search Filter
* */
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
SearchListActivity.this.adapter.getFilter().filter(cs);
// adapter.notifyDataSetChanged();
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
adapter.notifyDataSetChanged();
}
});
}
}
我希望有人可以帮助我。感谢:)
答案 0 :(得分:0)
它可能是因为id得到了响应..在过滤后,Banana的位置将为0 ..所以不要使用id使用视图并从item获取文本。请尝试..