如何将arraylist的参数显示到listview
我有一个列表视图中显示的数组列表。现在它可以工作,但它显示了一些随机数组ID查找名称,这就是为什么我希望它使用类的参数名称,这是一个字符串产品名称。
这是getters和setters文件,其中包含我要为列表显示的产品名称
包kfc.project;
公共类产品{
String name;
int servingSize;
int calories;
int fat;
int saturatedFat;
int transFat;
int cholesterol;
int sodium;
int carbs;
int fiber;
int sugar;
int protein;
int vitamina;
int vitaminc;
int calcium;
int iron;
public Product (String name, int servingSize, int calories, int fat, int saturatedFat, int transFat, int cholesterol, int sodium, int carbs, int fiber, int sugar, int protein, int vitamina, int vitaminc, int calcium, int iron){
this.name=name;
this.calories=calories;
this.servingSize=servingSize;
this.fat=fat;
this.saturatedFat=saturatedFat;
this.transFat=transFat;
this.cholesterol=cholesterol;
this.fiber=fiber;
this.sugar=sugar;
this.sodium=sodium;
this.carbs=carbs;
this.protein=protein;
this.vitamina=vitamina;
this.vitaminc=vitaminc;
this.calcium=calcium;
this.iron=iron;
}
public Product (String name, int servingSize){
this.name=name;
this.servingSize=servingSize;
}
public String getName() {
return name;
}
public int getServingSize() {
return servingSize;
}
public int getCalories() {
return calories;
}
public int getFat() {
return fat;
}
public int getSaturatedFat() {
return saturatedFat;
}
public int getTransFat() {
return transFat;
}
public int getCholesterol() {
return cholesterol;
}
public int getSodium() {
return sodium;
}
public int getCarbs() {
return carbs;
}
public int getProtein() {
return protein;
}
public int getFiber(){
return fiber;
}
public int getSugar(){
return sugar;
}
public int getVitaminA(){
return vitamina;
}
public int getVitaminC(){
return vitaminc;
}
public int getCalcium(){
return calcium;
}
public int getIron(){
return iron;
}
}
这是我添加产品的产品列表
package kfc.project;
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;
}
}
最后,这是我显示列表
的地方 package kfc.project;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
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<String>(this, R.layout.list_item, PRODUCTS));
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"));
}
});
}
//TEST STRING ARRAY
static String[] PRODUCTS = new String[] {
"Chicken", "Rice", "Coke", "Chicken Burger"
};
}
答案 0 :(得分:0)
只需在productDetail Activity
调用属于getIntent()
类的Activity
方法,您就可以获得启动此活动的意图。然后你可以从中提取数据。
离。
Intent intent = getIntent();
String str = intent.getExtra("name");
然后,您可以将此文本设置为任意TextView
。
答案 1 :(得分:0)
您可以使用getIntent和getExtras。这可能对你有帮助。 P.S - 空检查对于避免null pointer exception
非常有用。
Bundle extras = intent.getExtras();
textView.setText(extras != null ? extras.getString("value"));