android


want to use my own layout at ArrayAdapter


I use the following code to use the ArrayAdapter for my ListView:
ArrayList<String> mylist = new ArrayList<String>();
String xml = ParseXMLMethods.getXML();
Document doc = ParseXMLMethods.XMLfromString(xml);
NodeList children = doc.getElementsByTagName(KEY_ITEM);
for (int i = 0; i < children.getLength(); i++) {
Element e = (Element)children.item(i);
mylist.add(ParseXMLMethods.getValue(e, KEY_TITLE));
mylist.add(ParseXMLMethods.getValue(e, KEY_CITY));
mylist.add(ParseXMLMethods.getValue(e, KEY_COUNT));
}
adapter1=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
mylist);
list2.setAdapter(adapter1);
Here I am able to see only Title, but I want to see all three in my listView Row.For this I create another layout R.layout.list_layout2 as follows:
My layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:paddingLeft="6dp"
>
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:padding="1dp"
android:textSize="16dp"
android:textStyle="bold"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="1dp"
android:textSize="12dp"
android:textColor="#000000" />
<TextView
android:id="#+id/subtitle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textSize="12dp"
android:textColor="#000000"
android:layout_toRightOf="#+id/subtitle" />
</RelativeLayout>
</LinearLayout>
How can I use this layout?
You're going to want to look at a Custom ListView Adapter. Note that your original code uses a custom ListView but not the Adapter. You're going to want to create your own Adapter that extends ArrayAdapter since your ListView row is more complex than just one TextView.
See Tutorial and enter link description here.
Ex:
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow>
<ImageView
android:id="#+id/img"
android:layout_width="50dp"
android:layout_height="50dp"/>
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="50dp" />
</TableRow>
</TableLayout>
CustomList.java
public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final String[] web;
private final Integer[] imageId;
public CustomList(Activity context, String[] web, Integer[] imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_row, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web[position]);
imageView.setImageResource(imageId[position]);
return rowView;
}
}
In the example above, you're going to want to switch out the ImageView and replace it with your TextViews.

Related Links

Cocos Application did go into background callback not working
Android set Normal status bar programmatically
Cordova navigator.connection only unknown or wifi
Android GCM message not received on device
Clear push notification automatically after few minutes
TextInputLayout com.android.layoutlib.bridge.MockView cannot be cast to android.view.ViewGroup
android BitmapFactory.decodeFile returns null when jpeg is saved with Microsoft Paint
How to make JSONarray of object using MultipartEntity?
android: cannot resolve the symbol “urls”
Why is Volley returning null value for response object
Back Pressed from Fragment to Activity
Searching for Addresses using Skobbler multistep search
Can we use VectorDrawable or VectorXML as icons for push notifications in android?
Navigation View's selectors behave incorrectly
Fragment not being replaced
Android support v23.1.0 update breaks NavigationView get/find header

Categories

HOME
service-worker
phantom-dsl
linkedin
string
forms
jsf-2.2
aws-cli
vxml
3d-reconstruction
babeljs
python-multithreading
crystal-reports-2013
trac
elasticsearch-5
rcloud
reactive-programming
fhir
plist
redis-sentinel
filechannel
zerobrane
heat
mongodb-csharp
microsoft-ocr
systemtime
parcelable
draft-js-plugins
opentk
licensing
appirater
progress-bar
comparable
eviews
angularjs-material
glib
playstation-portable
linkedhashmap
card.io
indy10
arduino-ide
mnist
playframework-2.1
reactivekit
projects-and-solutions
mongoose-schema
spring-mvc-test
numberpicker
smoothstate.js
openrasta
geotiff
tinymce-3
arules
meteor-packages
hadoop-2.7.2
flex4.6
omniauth-facebook
nss
realsense
qbxml
blitline
state-restoration
themoviedb-api
morton-number
broadband
sonarqube-5.0
deform
iso-prolog
personalization
mintty
paste
notorm
jammer
script#
facebook-timeline
epiceditor
cardreader
mysql-error-2013
virtual-pc
objectquery
kqueue
fsevents
load-time
ie-compatibility-mode
updatesourcetrigger
build-environment
feasibility
flvplayback
pnrp

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App