我正在尝试使用对象中的“name”数据填充搜索列表。然后我希望能够单击该对象并将该对象发送到下一个活动。我只使用字符串工作,但对于对象,我得到各种错误。有没有人知道如何解决这个问题?这是迄今为止的代码。
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));
}
}
);
}
}
答案 0 :(得分:0)
您必须为此创建自己的适配器。
有关详细信息,请查看以下教程: http://www.vogella.com/articles/AndroidListView/article.html
public class MySimpleArrayAdapter extends ArrayAdapter<FoodItem> {
private final Context context;
private final ArrayList<FoodItem> values;
public MySimpleArrayAdapter(Context context, ArrayList<FoodItem> values) {
super(context, R.layout.YOURLAYOUT, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.YOURLAYOUT, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
textView.setText(values.get(position).NAMEOFFOOD);
return rowView;
}
}