我试图用三个变量填充一个arraylist。之后,arraylist应该在列表视图中显示其内容。当我运行这个类时,logcat会显示一个我完全不知道的错误。以下是我正在使用的课程。
import java.util.ArrayList;
import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Secondscreen extends Activity {
String pName ;
int pPrice;
String pDisc;
int total;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondscreen);
ListView lv= (ListView) findViewById(R.id.listView1);
//TextView showCartContent = (TextView) findViewById(R.id.showCart);
final Button thirdBtn = (Button) findViewById(R.id.third);
//final LinearLayout lb = (LinearLayout) findViewById(R.id.secondLinear);
final Controller aController = (Controller) getApplicationContext();
final int cartSize = aController.getCart().getCartSize();
final ArrayList<Listitem> arrayList=new ArrayList<Listitem>();
BaseAdapter adapter= new BaseAdapter(){
@Override
public int getCount() {
// TODO Auto-generated method stub
return arrayList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return arrayList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@Override
public View getView(int position, View view, ViewGroup viewgroup) {
if (view == null) {
view=inflater.inflate(R.layout.pattern, null);
}
TextView tv=(TextView) view.findViewById(R.id.nameview);
TextView tv2=(TextView) view.findViewById(R.id.pdesc);
TextView tv3=(TextView) view.findViewById(R.id.priceView);
tv.setText(pName);
tv2.setText(pPrice);
tv3.setText(pDisc);
return view;
}
};
lv.setAdapter(adapter);
if(cartSize >0)
{
for(int i=0;i<cartSize;i++)
{
pName = aController.getCart().getProducts(i).getProductName();
pPrice = aController.getCart().getProducts(i).getProductPrice();
pDisc = aController.getCart().getProducts(i).getProductDesc();
Listitem item=new Listitem(pName, pPrice,pDisc);
arrayList.add(item);
adapter.notifyDataSetChanged();
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
请帮我解决这个问题。
答案 0 :(得分:1)
tv2.setText(pPrice);
pPrice是一个int。 setText(int)在R类中查找String id,如果查找失败则抛出ResourceNotFoundException。快速修复
tv2.setText(""+pPrice);
这样您就可以向String
setText
个对象
编辑:
以这种方式更改你的getView:
@Override
public View getView(int position, View view, ViewGroup viewgroup) {
if (view == null) {
view=inflater.inflate(R.layout.pattern, null);
}
Listitem item = (Listitem)getItem(position)
TextView tv=(TextView) view.findViewById(R.id.nameview);
TextView tv2=(TextView) view.findViewById(R.id.pdesc);
TextView tv3=(TextView) view.findViewById(R.id.priceView);
tv.setText(item. pName);
tv2.setText(""+item.pPrice);
tv3.setText(item.pDisc);
return view;
}
答案 1 :(得分:0)
您永远不会从ArrayList获取对象。将此代码添加到getView()方法:
ListItem item = arrayList.get(position);
tv.setText(item.pName);
tv2.setText(item.pPrice);
tv3.setText(item.pDisc);