安卓有点问题。这是怎么回事,我有一个带有自定义适配器的 ListView,我想做的是动态添加行,代码如下:
适配器:
public class ProductAdapter extends ArrayAdapter<Product>{
Context context;
int layoutResourceId;
String data[] = null;
public ProductAdapter(Context context, int layoutResourceId,String[] data) {
super(context, layoutResourceId);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data=data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ProductHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ProductHolder();
holder.nameText = (TextView)row.findViewById(R.id.product_name);
holder.quantityText = (EditText)row.findViewById(R.id.quan_text);
row.setTag(holder);
}
else
{
holder = (ProductHolder)row.getTag();
}
Product product = DBAdaptor.getProductByName(data[position]);
holder.img=(ImageView)row.findViewById(R.id.imgIcon);
holder.nameText.setText(product.getName());
holder.quantityText.setText(" ");
return row;
}
static class ProductHolder
{
ImageView img;
TextView nameText;
EditText quantityText;
}
}
这是我的主要活动:
public class Main extends Activity
{
public ListView lstView;
ProductAdapter productListAdapter;
DBAdaptor mDb;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen);
openDB();
productListAdapter = new ProductAdapter(this, R.layout.shoping_list_row,getAllProducts());
Bundle b = this.getIntent().getExtras();
if(b!=null)
{
Product p =(Product) b.getSerializable("Product");
productListAdapter.add(p);
productListAdapter.notifyDataSetChanged();
}
}
}
没有出现错误,但没有任何内容添加到 listView
亲切的问候,