Reputation: 116
dataAdapter = new ArrayAdapter(getApplicationContext(), R.layout.lst_layout, R.id.lstLayout, exList);
lstView = (ListView) findViewById(R.id.listView);
lstView.setAdapter(dataAdapter);
exList is the ArayList of String arrays. How to populate ListView with it?
exList = new ArrayList<String[]>();
Edit starts here. It's in asynctask created here 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();
}
}
Upvotes: 0
Views: 5231
Reputation: 116
I handled it using custom adapter as suggested. Here is the adapter:
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;
}
}
Here is the layout row of the 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>
And finally:
OzelAdapter dataAdapter = new OzelAdapter(MainActivity.this, exList);
lstView = (ListView) findViewById(R.id.listView);
lstView.setAdapter(dataAdapter);
Upvotes: 2
Reputation: 8231
To convert the elements of a String[]
to an ArrayList
, use the Arrays.asList()
method:
String[] temp = new String[4];
//Assign value for each element in this array in doInBackground...
List<String> stringList = new ArrayList<String>(Arrays.asList(temp));
The [Ljava.lang.String;@xxxx
you see is the internal form of your String[]
. To quote:
If this class object represents a class of arrays, then the internal form of the name consists of the name of the element type preceded by one or more '[' characters representing the depth of the array nesting [when
toString()
is called]. The encoding of element type names is as follows:Element Type Encoding boolean Z byte B char C double D float F int I long J short S class or interface Lclassname;
Edit:
String[] temp;
//temp is the String array returned from/populated in your doInBackground method.
List<String> exlist = new ArrayList<>(Arrays.asList(temp));
dataAdapter = new ArrayAdapter(getApplicationContext(), R.layout.lst_layout, R.id.lstLayout, exList);
lstView = (ListView) findViewById(R.id.listView);
lstView.setAdapter(dataAdapter);
Upvotes: 2
Reputation: 12861
To use a basic ArrayAdapter, you just need to initialize the adapter and attach the adapter to the ListView. First, we initialize the adapter:
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);
The ArrayAdapter requires a declaration of the type of the item to be converted to a View (a String in this case) and then accepts three arguments: context (activity instance), XML item layout, and the array of data. Note that we've chosen simple_list_item_1.xml which is a simple TextView as the layout for each of the items.
Now, we just need to connect this adapter to a ListView to be populated:
ListView lstView = (ListView) findViewById(R.id.listView);
lstView.setAdapter(dataAdapter);
I hope it helps!
Upvotes: 2