1
我正試圖用來自對象的「名稱」數據填充搜索列表。然後,我希望能夠單擊對象並將對象發送到下一個活動。我只使用了字符串,但是使用這些對象時我會遇到各種各樣的錯誤。有沒有人有如何解決這個問題的想法?這是目前的代碼。將數組中的對象添加到可搜索列表
public class SearchActivity extends Activity
{
public final static String sampleObject = "no.uib.nutritionapplication.dene";
private ListView list;
private EditText edText;
private ArrayList<FoodItem> array_sort= new ArrayList<FoodItem>();
int textlength = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
ArrayList<FoodItem> foodList = new ArrayList<FoodItem>();
FoodItem orange_juice = new FoodItem("Orange juice", 10, 2, 100, 140, "Orange juice from concentrate");
FoodItem bread = new FoodItem("Bread", 12, 5, 150, 160, "Whole grain bread");
FoodItem jarlsberg = new FoodItem("Jarlsberg cheese", 8, 8, 130, 180, "Jarlsberg cheese");
foodList.add(orange_juice);
foodList.add(bread);
foodList.add(jarlsberg);
list = (ListView) findViewById(R.id.ListView01);
edText = (EditText) findViewById(R.id.EditText01);
list.setAdapter(new ArrayAdapter<FoodItem>(this, android.R.layout.simple_list_item_1, foodList));
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View itemClicked, int position,
long rowId) {
Intent intent = new Intent(SearchActivity.this, AddMealActivity.class);
TextView textView = (TextView) itemClicked;
FoodItem message = textView.getName().toString();
intent.putExtra("sampleObject", message);
startActivity(intent);
}
});
edText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s){
// Abstract Method of TextWatcher Interface.
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Abstract Method of TextWatcher Interface.
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
textlength = edText.getText().length();
array_sort.clear();
for (int i = 0; i < foodList.length; i++) {
if (textlength <= foodList.getName()[i].length()) {
if(edText.getText().toString().equalsIgnoreCase((String)foodList.getName()[i].subSequence(0,textlength))){
array_sort.add(foodList.getName()[i]);
}
}
}
list.setAdapter(new ArrayAdapter<String>(SearchActivity.this,android.R.layout.simple_list_item_1, array_sort));
}
}
);
}
}
[查看此示例...](http://samir-mangroliya.blogspot.in/2012/05/android-sectioned-列表視圖與 - search_6865.html) –