使用ArrayList作为元素填充ListView

时间:2015-08-18 11:20:32

标签: java android listview arraylist android-listview

dataAdapter = new ArrayAdapter(getApplicationContext(), R.layout.lst_layout, R.id.lstLayout, exList);
                lstView = (ListView) findViewById(R.id.listView);
                lstView.setAdapter(dataAdapter);

exList是String数组的ArayList。如何使用它填充ListView?

exList = new ArrayList<String[]>();

编辑从这里开始。这是在asdctask中创建的asdad asd asd asdasada sdada daasd asd ad asda dasd asd asda da dasdasd asdasd ad:

@Override
        protected String doInBackground(String... urls) {
            String[] temp = new String[4];
            try {
                InputStream stream = downloadUrl("http://www.tcmb.gov.tr/kurlar/today.xml");
                try {
                    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                    factory.setNamespaceAware(true);
                    XmlPullParser xpp = factory.newPullParser();
                    xpp.setInput(new InputStreamReader(stream));

                    int eventType = xpp.getEventType();
                    int kontrol = 0;
                    while (eventType != XmlPullParser.END_DOCUMENT) {

                        if (eventType == XmlPullParser.START_TAG) {


                            if (xpp.getName().equals("Currency")) {
                                kontrol++;
                                //ExchangeRate exr = new ExchangeRate();
                                //exr.setCurrencyCode(xpp.getAttributeValue(null, "CurrencyCode"));
                                temp[0] = xpp.getAttributeValue(null, "CurrencyCode");
                                Log.d("deneme: ", xpp.getAttributeValue(null, "CurrencyCode"));

                            } else if (xpp.getName().equals("Isim") && xpp.next() == XmlPullParser.TEXT) {
                                kontrol++;
                                //ExchangeRate exr = exList.get(exList.size() - 1);
                                //exr.setCurrencyName(xpp.getText());
                                temp[1] = xpp.getText();
                                Log.d("deneme", xpp.getText());

                            } else if (xpp.getName().equals("ForexBuying") && xpp.next() == XmlPullParser.TEXT) {
                                kontrol++;
                                //ExchangeRate exr = exList.get(exList.size() - 1);
                               //exr.setForexBuying(Double.valueOf(xpp.getText()));
                                temp[2] = xpp.getText();
                                Log.d("deneme", xpp.getText());
                            }else if (xpp.getName().equals("ForexSelling") && xpp.next() == XmlPullParser.TEXT) {
                                kontrol++;
                                //ExchangeRate exr = exList.get(exList.size() - 1);
                                //exr.setForexBuying(Double.valueOf(xpp.getText()));
                                temp[3] = xpp.getText();
                                Log.d("deneme", xpp.getText());
                            }
                            if (kontrol == 4) {
                                exList.add(temp);
                                temp = new String[4];
                                kontrol=0;
                            }
                        }
                        eventType = xpp.next();
                    }

                } catch (XmlPullParserException e) {
                    e.printStackTrace();
                }

                return "";

            } catch (IOException e) {
                return e.getMessage();
            }
        }

3 个答案:

答案 0 :(得分:2)

要使用基本ArrayAdapter,只需初始化适配器并将适配器连接到ListView。首先,我们初始化适配器:

ArrayList<String> exList = new ArrayList<>();
exList.add("data1");
exList.add("data2");
ArrayAdapter<String> itemsAdapter = 
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, exList);

ArrayAdapter需要声明要转换为View的项目类型(在本例中为String),然后接受三个参数:context(活动实例),XML项目布局和数据数组。请注意,我们选择了simple_list_item_1.xml这是一个简单的TextView作为每个项目的布局。

现在,我们只需将此适配器连接到要填充的ListView:

ListView lstView = (ListView) findViewById(R.id.listView);
lstView.setAdapter(dataAdapter);

我希望它有所帮助!

答案 1 :(得分:2)

要将 ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) { @Override public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder viewHolder1) { return false; } @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) { //Remove swiped item from list and notify the RecyclerView if (swipeDir == ItemTouchHelper.LEFT) { Snackbar.make(recyclerViewCards, getString(R.string.item_deleted), Snackbar .LENGTH_LONG) .show(); showVisitingCardsPresenterImpl.detletCardFromRealm(Realm.getInstance (ShowVisitingCardsActivity.this), mAdapter.getID(viewHolder .getAdapterPosition()), viewHolder .getAdapterPosition() ); } else { showVisitingCardsPresenterImpl.startVisitingCardUpdateActivity(mAdapter.getID(viewHolder .getAdapterPosition())); } } }; 的元素转换为String[],请使用ArrayList方法:

Arrays.asList()

您看到的String[] temp = new String[4]; //Assign value for each element in this array in doInBackground... List<String> stringList = new ArrayList<String>(Arrays.asList(temp)); [Ljava.lang.String;@xxxx的内部形式。致quote

  

如果此类对象表示一个数组类,那么内部   名称的形式由前面的元素类型的名称组成   一个或多个'['字符代表数组的深度   嵌套[调用String[]时]。元素类型名称的编码如下:

toString()

修改

Element Type        Encoding
boolean             Z
byte                B
char                C
double              D
float               F
int                 I
long                J
short               S 
class or interface  Lclassname;

答案 2 :(得分:2)

我按照建议使用自定义适配器处理它。这是适配器:

package com.xxx.xxx;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class OzelAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private ArrayList<String[]> mKisiListesi;

    public OzelAdapter(Activity activity, ArrayList<String[]> kisiler) {
        //XML'i alıp View'a çevirecek inflater'ı örnekleyelim
        mInflater = (LayoutInflater) activity.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        //gösterilecek listeyi de alalım
        mKisiListesi = kisiler;
    }

    @Override
    public int getCount() {
        return mKisiListesi.size();
    }

    @Override
    public Object getItem(int position) {
        //şöyle de olabilir: public Object getItem(int position)
        return mKisiListesi.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View satirView;

        satirView = mInflater.inflate(R.layout.lst_layout, null);
        TextView textView1 =
                (TextView) satirView.findViewById(R.id.lstLayout1);
        TextView textView2 =
                (TextView) satirView.findViewById(R.id.lstLayout2);
        TextView textView3 =
                (TextView) satirView.findViewById(R.id.lstLayout3);
        TextView textView4 =
                (TextView) satirView.findViewById(R.id.lstLayout4);

        textView1.setText(mKisiListesi.get(position)[0]);
        textView2.setText(mKisiListesi.get(position)[1]);
        textView3.setText(mKisiListesi.get(position)[2]);
        textView4.setText(mKisiListesi.get(position)[3]);

        return satirView;
    }
}

这是listview的布局行:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:gravity="left"
        android:textColor="@color/black"
        android:maxLines="8"
        android:lines="8"
        android:id="@+id/lstLayout1"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:editable="false" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:gravity="left"
        android:textColor="@color/black"
        android:maxLines="8"
        android:lines="8"
        android:id="@+id/lstLayout2"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:editable="false" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:gravity="left"
        android:textColor="@color/black"
        android:maxLines="8"
        android:lines="8"
        android:id="@+id/lstLayout3"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:editable="false" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:gravity="left"
        android:textColor="@color/black"
        android:maxLines="8"
        android:lines="8"
        android:id="@+id/lstLayout4"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:editable="false" />

</LinearLayout>

最后:

OzelAdapter dataAdapter = new OzelAdapter(MainActivity.this, exList);
                lstView = (ListView) findViewById(R.id.listView);
                lstView.setAdapter(dataAdapter);