显示一个ArrayList的对象的某些参数成Android的一个列表视图 [英] Displaying certain parameters of the objects of an arraylist into a list view in Android

查看:409
本文介绍了显示一个ArrayList的对象的某些参数成Android的一个列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此ArrayList显示在列表视图。现在,它的工作原理,但它显示了一些类型的数组ID寻找这就是为什么我想它在数组列表(字符串名称)来显示每个对象的名称的名称。

每个产品对象都有一个字符串名称,和其他值。

下面是在产品类,其中包含产品的ArrayList和人名调成String数组的方法:

 进口的java.util.ArrayList;公共类产品列表{
    ArrayList的<产品与GT;清单;
公共产品列表(){
    名单=新的ArrayList<产品及GT;();    //此处创建产品
    产品鸡肉;
    list.add(新产品(鸡,10,20,30,40,50,60,70,80,90,80,70,60,50,40,30));    产品水稻;
    list.add(新产品(米,11));
}公众的String [] getNames(){
    INT C = 0;
    INT大小=则为list.size() - 1;
    的String [] =名新的String [大小]    而(大小> = C){
        //names.add(list.get(三).getName());
        名字[C] = list.get(C).getName();
        C ++;
    }    C = 0;    返回名称;
}公众的ArrayList<产品与GT;的GetList(){
    返回列表;
}

}

最后,这里的code我ListActivity:

 公共类ProductListView扩展ListActivity {/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);    最终产品列表PL =新产品列表();    setListAdapter(新ArrayAdapter<产品及GT;(这一点,R.layout.list_item,pl.getList()));    ListView控件LV = getListView();
    lv.setTextFilterEnabled(真);    lv.setOnItemClickListener(新OnItemClickListener(){      公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){        //单击时,显示的文字TextView的敬酒
        Toast.makeText(getApplicationContext(),查看产品,Toast.LENGTH_SHORT).show();        产品产品= pl.getList()获得(位置)。
        //对产品细节的东西意图
        意向意图=新意图(ProductListView.this,productdetail.class);
        intent.putExtra(名,product.getName());
        intent.putExtra(份量,product.getServingSize());
        intent.putExtra(卡路里,product.getCalories());
        intent.putExtra(胖,product.getFat());
        intent.putExtra(饱和脂肪,product.getSaturatedFat());
        intent.putExtra(反式脂肪,product.getTransFat());
        intent.putExtra(胆固醇,product.getCholesterol());
        intent.putExtra(钠,product.getSodium());
        intent.putExtra(碳水化合物,product.getCarbs());
        intent.putExtra(纤维,product.getFiber());
        intent.putExtra(糖,product.getSugar());
        intent.putExtra(蛋白质,product.getProtein());
        intent.putExtra(维生素A,product.getVitaminA());
        intent.putExtra(维生素C,product.getVitaminC());
        intent.putExtra(钙,product.getCalcium());
        intent.putExtra(铁,product.getIron());        ProductListView.this.startActivity(意向);        // startActivity(新意图(kfc.project.productdetail));
      }    });}

}

我也试过让所有ArrayList中每个项目的字符串名称,并把它们放入一个String数组(使用我getNames()方法返回一个字符串数组),然后将其插入列表视图,但它只是表演一个错误,当我运行它:

 最终产品列表PL =新产品列表();setListAdapter(新ArrayAdapter&所述;字符串>(此,R.layout.list_item,pl.getNames()));


解决方案

那么,问题就在这里:

 公共字符串[] getNames(){
    INT C = 0;
    INT大小=则为list.size() - 1; < - 尺码问题
    的String [] =名新的String [大小] < - 好@@    而(大小> = C){
        //names.add(list.get(三).getName());
        名字[C] = list.get(C).getName(); < - ArrayIndexOutOfBoundException,我猜
        C ++;
    }    C = 0;    返回名称;
}

您看到它,不是吗?

下面的修补程序:

 公共字符串[] getNames(){
    INT C = 0;
    INT大小=则为list.size();
    的String [] =名新的String [大小]    而(大小和以及c){
        //names.add(list.get(三).getName());
        名字[C] = list.get(C).getName();
        C ++;
    }    C = 0;    返回名称;
}

I have this ArrayList that is displayed in a listview. Right now it works but it shows some kind of array id looking names thats why I wanted it to display the names of each object in the array list (String name).

Each of the Product object has a String name, and other values.

Here is the ProductList class where it contains the ArrayList of Products and a method to transfer the names into a String array:

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

}

Finally, here's the code for my 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"));
      }

    });

}

}

I also tried getting all of the String name of each item in the ArrayList and putting them into a String array (using my getNames() method which returns a String array) and then plugging it into the listview, but it just shows an error when I run it:

final ProductList pl = new ProductList();

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

解决方案

Well, the problem is here:

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

You see it, don't you?

Here the fix:

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

这篇关于显示一个ArrayList的对象的某些参数成Android的一个列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆