Android项目中的ArrayAdapter [重复](ArrayAdapter in Android Project [duplicate])

编程入门 行业动态 更新时间:2024-10-13 08:25:31
Android项目中的ArrayAdapter [重复](ArrayAdapter in Android Project [duplicate])

可能重复: 适用于listview的自定义布局的适配器

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 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)

Possible Duplicate: Adapter for a custom layout of listview

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); }

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

更多推荐

本文发布于:2023-04-29 03:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334472.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:项目   Android   ArrayAdapter   duplicate   Project

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!