首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓项目中的ArrayAdapter

安卓项目中的ArrayAdapter
EN

Stack Overflow用户
提问于 2012-12-20 09:53:02
回答 1查看 87关注 0票数 0

可能重复: Adapter for a custom layout of listview

代码语言:javascript
复制
ArrayAdapter<String> adapter = null;
JSONArray productsList = null;
try {
    productsList = obj5.getJSONArray("Content");
} catch (JSONException e2) {
    e2.printStackTrace();
}

if(productsList != null){
    ListView lv = (ListView) findViewById(android.R.id.list);

    for (int i=0; i<productsList.length(); i++) {

    final String[] items = new String[productsList.length()]; 

    String product = null;

    try {       
        product = productsList.getJSONObject(i).getString("Quantity")+" X "+
            productsList.getJSONObject(i).getString("Name") +"         "+
            Currency.britishPound+productsList.getJSONObject(i).getString("MinCost");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    if(product!=null){                  
        items[i]=product;                       
    }

    adapter = new ArrayAdapter<String>(APIQuickCheckout.this, R.layout.product_item, R.id.label, items);

    lv.setAdapter(adapter);
}

以下是我的列表视图:

代码语言:javascript
复制
<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_margin="5dip"
    android:layout_weight="30">
</ListView>

下面是我如何分别传递每种产品的方法。

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<!--  Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:textSize="16sp"
    android:textStyle="bold" >
 </TextView>

现在,根据前面提到的代码,我只传递给每一行一个简单的字符串。然而,我想知道我如何通过三个不同的字符串。我想传递三个不同的字符串的原因是我希望它们的格式如下:

代码语言:javascript
复制
Quantity X 
Number                          Price(this should be at the end of the line)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-20 10:04:31

您必须从适配器派生,并提供适当填充的视图。就像这里:

https://github.com/ko5tik/camerawatch/blob/master/src/de/pribluda/android/camerawatch/CameraAdapter.java

为了取得更好的业绩,请考虑:

  • 重用提供给getView() method#的视图
  • 利用viewHolder模式保存getViewById()解析
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13969387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档