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

Android SDK location should not contain whitespace, as this cause problems with NDK tools
JSON url Redirection fails
ng click not calling the function --very new to angular and ionic
Failed to build Android project with Google Play services
Error implementing tutorial code
applidium/HeaderListView not working correct
Facebook Login Button crashing app without error message
When I come back from other screens to ViewPager, then ViewPager is totally Blank, What could be the cause?
API to extract text from a image for Android Mobile
How to Go to New Screen/Page in Android Studio?
Can I pass Surface between Activities?
Fail to Move a View to a TableRow
Save data to server data base in Android give unexpected error and close
webview fragment press back to return to
ImageView bitmap is stretched after activity transition
Paste text on Android using ADB?

Categories

HOME
go
kentico
google-cloud-platform
scikit-learn
delphi-7
fftw
google-apps
interpolation
vsm
webix
bing-maps
accessibility
g++
distribution
github-enterprise
alamofire
redis-sentinel
dynatrace
mongodb-csharp
git-extensions
cfml
oledb
asp.net-core-webapi
drive
qliksense
appirater
sqlpackage
realm-mobile-platform
event-flow
paragraph
autolisp
docfx
connection-pool
mcustomscrollbar
dbnull
knime
xmldocument
signal-strength
vaadin-charts
puredata
jquery-ui-draggable
remedy
arules
dt
cmp
restore
microstation
vulcanize
spring-cloud-feign
android-wake-lock
thread-sleep
inf
boomerang
sciruby
wiredep
jdi
ons-api
pax-exam
toolkit
slash
iced-coffeescript
mirror
alloy-ui
typeconverter
bufferedinputstream
osc
zend-currency
j-security-check
bcache
document-repository
jbake
grails-plugin-rabbitmq
android-framework
terminate
conditionaltagsupport
solace-mq
kendo-dataviz
time.h
node-blade
diem
composite-component
intersect
extreme-programming
subproject
kqueue
fitbounds
android-holo-everywhere
silent
drupal-fivestar
blender-2.49
feasibility
unmanagedresources
wcf-proxy

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