2012-10-22 4 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- [이 예 ...보기] listview-with-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; 
    } 
} 
관련 문제