2011-12-01 3 views
1

나는이 ArrayList를 listview에 표시했습니다. 지금은 작동하지만 배열 ID (배열 이름)에 각 객체의 이름을 표시하려고했기 때문에 이름을 찾는 배열 ID를 표시합니다.arraylist의 객체의 특정 매개 변수를 Android의 목록보기에 표시

각 Product 객체에는 String 이름과 다른 값이 있습니다. 여기

는이 제품의 ArrayList를하고 문자열 배열에 이름을 전송하는 방법이 포함 된 제품 목록 클래스 :

import java.util.ArrayList; 

public class ProductList { 
    ArrayList<Product> list; 


public ProductList(){ 
    list = new ArrayList<Product>(); 

    //CREATE PRODUCT HERE 
    Product chicken; 
    list.add(new Product("Chicken", 10, 20, 30, 40, 50, 60, 70, 80, 90, 80,70,60,50,40,30));  

    Product rice; 
    list.add(new Product("Rice",11)); 
} 

public String[] getNames(){ 
    int c = 0; 
    int size = list.size() - 1; 
    String[] names = new String[size]; 

    while (size >= c) { 
     //names.add(list.get(c).getName()); 
     names[c] = list.get(c).getName(); 
     c++; 
    } 

    c = 0; 

    return names; 
} 

public ArrayList<Product> getList(){ 
    return list; 
} 

} 마지막

을, 여기 내 ListActivity에 대한 코드입니다 :

public class ProductListView extends ListActivity { 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState); 

    final ProductList pl = new ProductList(); 

    setListAdapter(new ArrayAdapter<Product>(this, R.layout.list_item, pl.getList())); 

    ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 

    lv.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

     // When clicked, show a toast with the TextView text 
     Toast.makeText(getApplicationContext(), "View Product", Toast.LENGTH_SHORT).show(); 

     Product product = pl.getList().get(position); 
     // intent stuff for product detail 
     Intent intent = new Intent(ProductListView.this, productdetail.class); 
     intent.putExtra("name",product.getName()); 
     intent.putExtra("serving size", product.getServingSize()); 
     intent.putExtra("calories", product.getCalories()); 
     intent.putExtra("fat", product.getFat()); 
     intent.putExtra("saturated fat", product.getSaturatedFat()); 
     intent.putExtra("trans fat", product.getTransFat()); 
     intent.putExtra("cholesterol", product.getCholesterol()); 
     intent.putExtra("sodium", product.getSodium()); 
     intent.putExtra("carbs", product.getCarbs()); 
     intent.putExtra("fiber", product.getFiber()); 
     intent.putExtra("sugar", product.getSugar()); 
     intent.putExtra("protein", product.getProtein()); 
     intent.putExtra("vitamina", product.getVitaminA()); 
     intent.putExtra("vitaminc", product.getVitaminC()); 
     intent.putExtra("calcium", product.getCalcium()); 
     intent.putExtra("iron", product.getIron()); 

     ProductListView.this.startActivity(intent); 

     //startActivity(new Intent("kfc.project.productdetail")); 
     } 

    }); 

} 

}

,

또한 ArrayList의 각 항목에 대한 String 이름을 모두 가져 와서 String 배열로 가져 오려고했습니다 (String 배열을 반환하는 getNames() 메서드 사용). 그런 다음 listview에 연결했지만 오류가 나는 그것을 실행하면

final ProductList pl = new ProductList(); 

setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, pl.getNames())); 
+0

그래서 무슨 오류가 로그 캣에서 보여? –

답변

0

일을 시도해보십시오. 대신 pl.getList의

setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, pl.getNames())); 

()를 getNames를().

+0

문제가 아닙니다. –

+0

죄송합니다. 질문에 해당 코드를 잘못 입력했습니다. 그게 내가 뭘 해봤지만, 내가 목록보기에 액세스하려고하면 응용 프로그램이 그냥 충돌 – 10834346

3

음, 문제는 여기에 있습니다 : 당신이 그것을 볼

public String[] getNames(){ 
    int c = 0; 
    int size = list.size() - 1; <-- size problem 
    String[] names = new String[size]; <-- well @@ 

    while (size >= c) { 
     //names.add(list.get(c).getName()); 
     names[c] = list.get(c).getName(); <-- ArrayIndexOutOfBoundException, I guess 
     c++; 
    } 

    c = 0; 

    return names; 
} 

,하지 당신은? 여기

수정 :

public String[] getNames(){ 
    int c = 0; 
    int size = list.size(); 
    String[] names = new String[size]; 

    while (size > c) { 
     //names.add(list.get(c).getName()); 
     names[c] = list.get(c).getName(); 
     c++; 
    } 

    c = 0; 

    return names; 
} 
+0

감사합니다. 나는 그것을 간과했다 – 10834346

1

표준 ArrayAdapter와는 자동으로 ArrayList를 제공 한 객체의 toString 메소드를 사용합니다. 즉, Java에서 toString 메소드의 기본 구현을 사용하고 있습니다.이 구현은 객체에 대해 무의미한 말투를 표시합니다. 제품 이름을 표시하는 가장 간단한 방법은 제품 오브젝트 내부의 toString 메소드를 대체하여 오브젝트의 이름 만 리턴하는 것입니다.

예는 :

public class Product{ 

      //Some of your current code for product object 

      @Override 
      public String toString(){ 

        return name; 

      } 
    } 
+0

고마워, 정말 잘 했어. – 10834346

관련 문제