我正在尝试使用来自对象的“名称”数据填充搜索列表。然后我希望能够单击对象并将对象发送到下一个活动。我只使用字符串让它工作,但使用对象我得到各种错误。有谁知道如何解决这个问题?这是到目前为止的代码。
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));
}
}
);
}
}