您好我正在尝试构建一个列表视图并从Mysql
数据库中的值填充它。使用SimpleAdapter的列表有效但我决定更改listview并使用我自己的扩展ArrayAdapter的适配器并创建我自己的listItem而不是HashMap。代码在
public void ListDrawer() {
customList = new ArrayList<StockList>();
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("metoxes");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("name");
String price = jsonChildNode.optString("price");
String price1 = jsonChildNode.optString("price1");
String price2 = jsonChildNode.optString("price2");
String price3 = jsonChildNode.optString("price3");
String price4 = jsonChildNode.optString("price4");
String price5 = jsonChildNode.optString("price5");
String price6 = jsonChildNode.optString("price6");
String price7 = jsonChildNode.optString("price7");
String price8 = jsonChildNode.optString("price8");
String price9 = jsonChildNode.optString("price9");
String price10 = jsonChildNode.optString("price10");
String price11 = jsonChildNode.optString("price11");
String price12 = jsonChildNode.optString("price12");
String price13 = jsonChildNode.optString("price13");
String price14 = jsonChildNode.optString("price14");
String price15 = jsonChildNode.optString("price15");
String image = jsonChildNode.optString("image");
Bitmap bmp = getBitmapFromUrl(image);
StockList item = new StockList(name, price, bmp);
customList.add(item);
// customList.add(createStockList(name, price, price1, price2,
// price3, price4, price5, price6, price7, price8, price9,
// price10, price11, price12, price13, price14, price15));
}
} catch (Exception e) {
Intent intent1 = new Intent(ListLoaderActivity.this,
RefreshActivity.class);
startActivity(intent1);
ListLoaderActivity.this.finish();
}
ArrayAdapter adapter = new MyStocksAdapter(ListLoaderActivity.this, R.layout.list_item, customList);
adapter.notifyDataSetChanged();
startList.setAdapter(adapter);
// String[] from = {"name", "price"};
// int[] to = {R.id.stock_name, R.id.stock_price};
// SimpleAdapter simpleAdapter = new SimpleAdapter(this, customList,
// R.layout.list_item, from, to);
}
/*public HashMap<String, String> createStockList(String name, String price,
String price1, String price2, String price3, String price4,
String price5, String price6, String price7, String price8,
String price9, String price10, String price11, String price12,
String price13, String price14, String price15) {
HashMap<String, String> stockNameNo = new HashMap<String, String>();
stockNameNo.put("name", name);
stockNameNo.put("price", price);
stockNameNo.put("price1", price1);
stockNameNo.put("price2", price2);
stockNameNo.put("price3", price3);
stockNameNo.put("price4", price4);
stockNameNo.put("price5", price5);
stockNameNo.put("price6", price6);
stockNameNo.put("price7", price7);
stockNameNo.put("price8", price8);
stockNameNo.put("price9", price9);
stockNameNo.put("price10", price10);
stockNameNo.put("price11", price11);
stockNameNo.put("price12", price12);
stockNameNo.put("price13", price13);
stockNameNo.put("price14", price14);
stockNameNo.put("price15", price15);
return stockNameNo;
}*/
我的自定义列表项目:
public class StockList {
private String stockCurrentName;
private String stockCurrentPrice;
private Bitmap stockImage;
public StockList(String stockCurrentName, String stockCurrentPrice, Bitmap stockImage) {
this.stockCurrentName = stockCurrentName;
this.stockCurrentPrice = stockCurrentPrice;
this.stockImage = stockImage;
}
public String getStockCurrentName() {
return stockCurrentName;
}
public void setStockCurrentName(String stockCurrentName) {
this.stockCurrentName = stockCurrentName;
}
public String getStockCurrentPrice() {
return stockCurrentPrice;
}
public void setStockCurrentPrice(String stockCurrentPrice) {
this.stockCurrentPrice = stockCurrentPrice;
}
public Bitmap getStockImage() {
return stockImage;
}
public void setStockImage(Bitmap stockImage) {
this.stockImage = stockImage;
}
}
我的自定义适配器
public class MyStocksAdapter extends ArrayAdapter<StockList>{
private View stockView;
private List<StockList> stocksList;
private RoundImage roundedImage;
public MyStocksAdapter(Context context, int resource, List<StockList> objects) {
super(context, resource, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(stockView == null){
LayoutInflater li = LayoutInflater.from(getContext());
stockView = li.inflate(R.layout.list_item, parent, false);
StockList current = getItem(position);
if(current != null){
TextView stockName = (TextView)stockView.findViewById(R.id.stock_name);
stockName.setText(current.getStockCurrentName());
TextView stockPrice = (TextView)stockView.findViewById(R.id.stock_price);
stockPrice.setText(current.getStockCurrentPrice());
ImageView stockImage = (ImageView)stockView.findViewById(R.id.imagestartinglist);
stockImage.setImageBitmap(current.getStockImage());
}
}
return stockView;
}
}
我用于每个列表行的xml布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ccc"
android:fitsSystemWindows="true"
android:padding="10dp"
android:descendantFocusability="blocksDescendants"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/stock_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/fake_name_day"
android:layout_marginLeft="28dp"
android:textColor="#000"
android:layout_toRightOf="@+id/fake_name_day"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/stock_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/textView2"
android:padding="6dp"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000" />
<TextView
android:id="@+id/fake_name_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/stock_name"
android:text="@string/current_price"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/stock_name"
android:layout_alignBottom="@+id/stock_name"
android:layout_marginLeft="23dp"
android:layout_toRightOf="@+id/imagestartinglist"
android:text="@string/stock_name"
android:textColor="#000" />
<ImageView
android:id="@+id/imagestartinglist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@drawable/ic_launcher_stock_custom_icon" />
</RelativeLayout>
问题是一切正常,但它只显示数据库中的第一项,我不知道为什么,因为我在for循环中添加项目。
任何想法???
答案 0 :(得分:1)
这种情况正在发生,因为您每次都使用相同的对象stockView
来检查null。当第一个项目被渲染时,它不是null,因此每次使用相同的对象时。
使用此...
public class MyStocksAdapter extends ArrayAdapter<StockList>{
private List<StockList> stocksList;
private RoundImage roundedImage;
public MyStocksAdapter(Context context, int resource, List<StockList> objects) {
super(context, resource, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater li = LayoutInflater.from(getContext());
View stockView = li.inflate(R.layout.list_item, parent, false);
StockList current = getItem(position);
if(current != null){
TextView stockName = (TextView)stockView.findViewById(R.id.stock_name);
stockName.setText(current.getStockCurrentName());
TextView stockPrice = (TextView)stockView.findViewById(R.id.stock_price);
stockPrice.setText(current.getStockCurrentPrice());
ImageView stockImage = (ImageView)stockView.findViewById(R.id.imagestartinglist);
stockImage.setImageBitmap(current.getStockImage());
}
return stockView;
}
}
答案 1 :(得分:0)
我认为问题是第二次stockView
不为空,你可以通过调试getView
方法来确保这一点
所以解决方法是
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(stockView == null){
LayoutInflater li = LayoutInflater.from(getContext());
stockView = li.inflate(R.layout.list_item, parent, false);
}
StockList current = getItem(position);
if(current != null){
TextView stockName = (TextView)stockView.findViewById(R.id.stock_name);
stockName.setText(current.getStockCurrentName());
TextView stockPrice = (TextView)stockView.findViewById(R.id.stock_price);
stockPrice.setText(current.getStockCurrentPrice());
ImageView stockImage = (ImageView)stockView.findViewById(R.id.imagestartinglist);
stockImage.setImageBitmap(current.getStockImage());
}
return stockView;
}