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); }可能重复: 适用于listview的自定义布局的适配器
这是我的列表视图:
<ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="0dp" android:layout_margin="5dip" android:layout_weight="30"> </ListView>这是我如何单独传递每个产品。
<?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>现在,根据前面提到的代码,我传递给每一行只是一个简单的字符串。 但是,我想知道如何通过三个不同的字符串。 我想传递三个不同字符串的原因是我希望它们采用以下格式:
Quantity X Number Price(this should be at the end of the line)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); }Possible Duplicate: Adapter for a custom layout of listview
Here is my list view:
<ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="0dp" android:layout_margin="5dip" android:layout_weight="30"> </ListView>And here is how I pass each product seperately.
<?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>Now and according to the the aforementioned code I'm passing to each line just a simple string. However, I was wondering how could I pass three different Strings. The reason that I want to pass three different Strings is that I want them to be in the following format:
Quantity X Number Price(this should be at the end of the line)最满意答案
您必须从适配器派生,并提供正确填充的视图。 像这儿:
https://github.com/ko5tik/camerawatch/blob/master/src/de/pribluda/android/camerawatch/CameraAdapter.java
为了更好的性能考虑:
重用提供给getView()方法的视图# 利用viewHolder patern来保存getViewById()分辨率You have to derive from adapter, and provide properly filled views. Like here:
https://github.com/ko5tik/camerawatch/blob/master/src/de/pribluda/android/camerawatch/CameraAdapter.java
For better performance consider:
reusing views supplied to getView() method# utilise viewHolder patern to save getViewById() resolution更多推荐
发布评论