2012-10-22 86 views
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)); 
      } 
     } 
    ); 


    } 

} 
+0

[查看此示例...](http://samir-mangroliya.blogspot.in/2012/05/android-sectioned-列表視圖與 - search_6865.html) –

回答

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; 
    } 
}