ListView default background selector doesn't work - android

I'm trying to solve an issue with default background selector in list view.
I'm using a ListView with a default style. List item contains a few TextViews and an ImageButton. When the image button is not visible (Setting visibility to invisible using the adapter), the background selector works well (There is an on click animation). But, when the ImageButton is visible, there is no onclick animation when clicking on the item (Not the button)..
List item:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
>
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10:20:30"
android:layout_centerVertical="true"/>
<TextView
android:id="#+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/time"
android:layout_marginStart="15dp"
android:layout_toStartOf="#+id/openMapButton"
android:textStyle="bold"/>
<TextView
android:id="#+id/body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Long long bodylong bodylong bodylong bodylong bodylong bodylong bodylong body"
android:layout_below="#+id/type"
android:layout_alignStart="#id/type"
android:layout_toStartOf="#+id/openMapButton"
android:ellipsize="end"
android:maxLines="2"
/>
<ImageButton
android:id="#+id/openMapButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/open_map"
android:background="#android:color/transparent"
android:padding="4dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"/>
ListView:
<FrameLayout 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"
tools:context=".events.EventsHistoryFragment">
<ListView
android:id="#android:id/list"
tools:listitem="#layout/notification_list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
Adapter:
public class EventsAdapter extends CursorAdapter {
public EventsAdapter(Context context, Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.notification_list_item, parent, false);
EventViewHolder viewHolder = new EventViewHolder();
viewHolder.time = (TextView) view.findViewById(R.id.time);
viewHolder.type = (TextView) view.findViewById(R.id.type);
viewHolder.body = (TextView) view.findViewById(R.id.body);
viewHolder.openMapButton = (ImageButton) view.findViewById(R.id.openMapButton);
viewHolder.openMapButtonListener = new ShowMapButtonClickListener();
viewHolder.openMapButton.setOnClickListener(viewHolder.openMapButtonListener);
view.setTag(viewHolder);
return view;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
EventViewHolder viewHolder = (EventViewHolder) view.getTag();
long timestamp = cursor.getLong(EventData.TIMESTAMP_COLUMN_ID);
String type = cursor.getString(EventData.TYPE_COLUMN_ID);
String body = cursor.getString(EventData.BODY_COLUMN_ID);
double latitude = cursor.getDouble(EventData.LOCATION_LATITUDE_COLUMN_ID);
double longitude = cursor.getDouble(EventData.LOCATION_LONGITUDE_COLUMN_ID);
String humanReadableType = ServiceEvents.getReadableEventName(context, type);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Date date = new Date(timestamp);
viewHolder.time.setText(sdf.format(date));
viewHolder.type.setText(humanReadableType);
viewHolder.body.setText(body);
if(latitude == ServiceEvents.UNKNOWN_COORIDATE) {
viewHolder.openMapButton.setVisibility(View.INVISIBLE);
} else {
viewHolder.openMapButton.setVisibility(View.VISIBLE);
viewHolder.openMapButtonListener.bind(context, latitude, longitude, humanReadableType);
}
}
}

The default behavior of the android list items is determined by the selector
?android:attr/selectableItemBackground so your RelativeLayout of your ListItem should be:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:background="?android:attr/selectableItemBackground"
>
Notice the last attribute android:background="?android:attr/selectableItemBackground"

Related

Items in a Listview don't look like my item layout

I'm building an app for a news website, I have created a ListView and a layout for each item, which has quite a bit of white space, but on the list view all the white space is removed.
Here's what I mean: http://imgur.com/a/mLuuE
This is an item layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="175dp"
android:gravity="center"
android:layout_weight="0"
android:id="#+id/article_layout"
android:background="#drawable/gradientdemo">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center|bottom"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Titolo dell'articolo"
android:textSize="#dimen/abc_text_size_headline_material"
android:layout_gravity="left"
android:id="#+id/article_title" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="3/9/16 - Autore"
android:layout_gravity="left"
android:id="#+id/article_info" />
</LinearLayout>
</LinearLayout>
This is my custom adapter:
public class ArticleAdapter extends ArrayAdapter<Article> {
public ArticleAdapter(Context context, int textViewResourceId){
super(context, textViewResourceId);
}
public ArticleAdapter(Context context, int resource, List<Article> items ){
super(context, resource, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.article_item, null);
}
Article article = (Article) getItem(position);
TextView title = (TextView) v.findViewById(R.id.article_title);
TextView info = (TextView) v.findViewById(R.id.article_info);
if( title != null ) {
title.setText( article.getTitle() );
}
if( info != null ) {
info.setText( article.getAuthor() );
}
return v;
}
}
Hope it's readable, I'm kind of new to android development.
Your cell's LineaerLayout:
android:layout_height="0dp"
android:layout_weight="1"
change to:
android:layout_height="wrap_content"
you don't need layout_weight
Delete the android:layout_weight="1" and add a correct height, e.g. android:layout_height="60dp":
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center|bottom"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Titolo dell'articolo"
android:textSize="#dimen/abc_text_size_headline_material"
android:layout_gravity="left"
android:id="#+id/article_title" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="3/9/16 - Autore"
android:layout_gravity="left"
android:id="#+id/article_info" />
</LinearLayout>
in the linearLayout use wrapcontent, erase layout_weigth and add weigthsum=1
Then in textViews change height to 0dp and add to each one layout_weight=0.5
I think this will work
I fixed this by changing this line in the adapter
v = vi.inflate(R.layout.article_item, null);
to this one:
v = vi.inflate(R.layout.article_item, parent, false);

Android ListView doesn't show first item

I have gone through this & this SO questions but unable to figure out why the first item of my list view is not visible. I have checked the size of arraylist and it's correct.
if there is 2 items then it shows only 1.
if there is 1 item then it shows 0.
listview file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/videoListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#null"
android:dividerHeight="0dp" />
</RelativeLayout>
listview item xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp">
<ImageView
android:layout_width="150dp"
android:layout_height="90dp"
android:id="#+id/videoImageView"
android:layout_marginLeft="10dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="35dp"
android:id="#+id/titleTextView"
android:layout_marginRight="10dp"
android:layout_toRightOf="#+id/videoImageView"
android:layout_marginLeft="10dp"
android:textSize="12sp"
android:textColor="#000000"
android:ellipsize="end"
android:maxLines="2"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="20dp"
android:id="#+id/timeTextView"
android:layout_alignStart="#+id/titleTextView"
android:layout_alignLeft="#+id/titleTextView"
android:layout_below="#+id/titleTextView"/>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/downloadImageView"
android:layout_alignStart="#+id/timeTextView"
android:layout_alignLeft="#+id/timeTextView"
android:layout_below="#+id/timeTextView"
android:layout_alignBottom="#+id/videoImageView"
android:src="#drawable/ic_vector_download"
android:layout_marginTop="5dp"/>
<ProgressBar
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/downloadProgressBarView"
android:layout_alignStart="#+id/timeTextView"
android:layout_alignLeft="#+id/timeTextView"
android:layout_below="#+id/timeTextView"
android:layout_alignBottom="#+id/videoImageView"
android:layout_marginTop="5dp"
android:visibility="gone"/>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/shareImageView"
android:layout_below="#+id/timeTextView"
android:layout_alignBottom="#+id/videoImageView"
android:layout_toRightOf="#+id/downloadImageView"
android:layout_marginTop="5dp"
android:layout_marginLeft="7dp"
android:src="#drawable/ic_vector_share"/>
</RelativeLayout>
adapter class:
public class OfflineVideoAdapter extends ArrayAdapter<OfflineVideo> {
Context context;
OfflineVideoDBHelper db;
List<OfflineVideo> list;
public OfflineVideoAdapter(Context context, int resource, List<OfflineVideo> list) {
super(context, resource, list);
this.context = context;
this.list = list;
}
#NonNull
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final OfflineVideoHolder holder;
if (convertView == null) {
holder = new OfflineVideoHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.video_list_item, parent, false);
holder.video = (ImageView)convertView.findViewById(R.id.videoImageView);
holder.title = (TextView)convertView.findViewById(R.id.titleTextView);
holder.publishedAt = (TextView)convertView.findViewById(R.id.timeTextView);
holder.delete = (ImageView)convertView.findViewById(R.id.downloadImageView);
holder.share = (ImageView)convertView.findViewById(R.id.shareImageView);
convertView.setTag(holder);
} else {
holder = (OfflineVideoHolder)convertView.getTag();
}
final OfflineVideo offlineVideo = getItem(position);
Picasso.with(context).load(offlineVideo.imageURL).into(holder.video);
holder.title.setText(offlineVideo.title);
holder.publishedAt.setText(CommonUtils.calculateTime(offlineVideo.publishedAt));
holder.delete.setImageResource(R.drawable.ic_vector_delete);
return convertView;
}
}
Change the ListView attribute layout_width and layout_height to Fill_Parent
it worked for me.
I was having the same issue because my ListView layout_width and layout_height was "match_constraint" and the toolbar was overlapping the first item. Just adjust the height of the ListView. It works for me.

Android's simple cursor adapter and multiple row layouts

I've been dabbling with android here and there for the past few months (mostly just tutorials). I've been working on a texting app (for myself) in my free time to learn, and I've succeeded in getting a different layout for each row, but it doesn't work as I'd intended.
Each row is either going to be left or right (left by default), depending on if the number that's in the cursor's position is my phone number or not.
The problem is that my test data looks like this (where the green is my text):
Here's the code for my custom adapter:
public class ConversationMessageListAdapter extends SimpleCursorAdapter {
private LayoutInflater inflater;
private String myPhoneNumber;
public ConversationMessageListAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to, int flags) {
super(context, layout, cursor, from, to, flags);
myPhoneNumber = getMyPhoneNumber(context);
inflater = LayoutInflater.from(context);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
String number = cursor.getString(2);
System.out.println("phone#: "+number);
System.out.println("my#: "+myPhoneNumber);
if(number.equals(myPhoneNumber)){
View view = inflater.inflate(R.layout.single_message_layout_right, null);
return view;
}
return super.newView(context, cursor, parent);
}
public String getMyPhoneNumber(Context context){
TelephonyManager tMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return tMgr.getLine1Number();
}
}
You can see my attempt at debugging the issue by printing out the values of myPhoneNumber and number. It looks like they aren't always matching up correctly with the rows. It'll show the friend's number on the row where it should be mine, yet if I leave the default layout (keep them all to the left) it shows my number on that same row in the textview. Like:
555-555-FRND
Have you played Dynasty Warriors?
555-555-MINE
Not yet. Maybe soon.
But at that point, what get's printed is:
phone#: 555-555-FRND
my# 555-555-MINE
phone#: 555-555-FRND
my# 555-555-MINE
When it should look like:
phone#: 555-555-FRND
my# 555-555-MINE
phone#: 555-555-MINE
my# 555-555-MINE
I'm at a loss for what's causing this.
If it helps, here's my setViewBinder:
textsDbAdapter.setViewBinder(new ConversationMessageListAdapter.ViewBinder() {
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if(columnIndex == 2){
String number = cursor.getString(columnIndex);
String sentBy = getContactName(contentResolver, number);
if(number.equals(myPhoneNumber)){
return false;
}
TextView sender = (TextView) view;
sender.setText(sentBy);
return true;
}
//datetime
if(columnIndex == 5){
Long messageDatetime = Long.valueOf(cursor.getString(columnIndex));
String receivedOn;
if(startOfToday > messageDatetime){
receivedOn = getDateFromDatetime(messageDatetime);
} else {
receivedOn = getTimeFromDatetime(messageDatetime);
}
TextView datetime = (TextView) view;
datetime.setText(receivedOn);
return true;
}
return false;
}
});
Edit: I've also found out that when I scroll up and then back down, the rows will align randomly.
EDIT2: XML added
single_message_layout_right.xml
<RelativeLayout 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:paddingLeft="0dp"
android:paddingRight="5dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:layout_gravity="right"
tools:context=".ConversationActivity">
<!--Datetime Received-->
<TextView
android:id="#+id/datetime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:layout_below="#+id/messageContent"
android:layout_toLeftOf="#+id/displayPicContainer"
android:textSize="14sp"
android:textColor="#9110828f" />
<!--Message-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/messageContent"
android:textSize="15sp"
android:layout_marginTop="5dp"
android:layout_toLeftOf="#+id/displayPicContainer"/>
<!--Display picture layout-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="3dp"
android:layout_marginLeft="5dp"
android:id="#+id/displayPicContainer">
<!--Display picture-->
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:src="#drawable/bigboss150x150"
android:id="#+id/displayPic"/>
</LinearLayout>
</RelativeLayout>
single_message_layout_left.xml
<RelativeLayout 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:paddingLeft="0dp"
android:paddingRight="5dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
tools:context=".ConversationActivity">
<!--Display picture layout-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:layout_marginRight="5dp"
android:id="#+id/displayPicContainer">
<!--Display picture-->
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:src="#drawable/bigboss150x150"
android:id="#+id/displayPic"/>
</LinearLayout>
<!-- sender -->
<TextView
android:id="#+id/sender"
android:textSize="14sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="NAME"
android:textColor="#ff9b9aa0"
android:layout_toRightOf="#+id/displayPicContainer"/>
<!--Datetime Received-->
<TextView
android:id="#+id/datetime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:layout_below="#+id/messageContent"
android:layout_toRightOf="#+id/displayPicContainer"
android:textSize="14sp"
android:textColor="#9110828f" />
<!--Message-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/messageContent"
android:textSize="15sp"
android:layout_below="#+id/sender"
android:layout_toRightOf="#+id/displayPicContainer"/>
</RelativeLayout>
conversation_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/conversationListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="43dp"
android:stackFromBottom="true"
android:transcriptMode="normal">
</ListView>
<RelativeLayout
android:id="#+id/form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="vertical"
android:background="#ffffff">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:inputType="textCapSentences|textMultiLine"
android:ems="10"
android:lines="5"
android:minLines="1"
android:hint="Enter message"
android:id="#+id/newMessageText"
android:scrollHorizontally="true"
android:scrollbars="vertical"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/sendButton"/>
<Button
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="Send"
android:id="#+id/sendButton"
android:layout_alignBottom="#+id/newMessageText"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</RelativeLayout>
I switched from doing newView to getView and this is working for me.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
Log.d("RowID", messages.getString(messages.getColumnIndex("_id")));
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
String fromNumber = messages.getString(messages.getColumnIndex("fromNumber"));
if(fromNumber.equals(myPhoneNumber)){
row = inflater.inflate(R.layout.single_message_layout_right, parent, false);
} else {
row = inflater.inflate(R.layout.single_message_layout_left, parent, false);
TextView sender = (TextView) row.findViewById(R.id.sender);
String name = getContactName(fromNumber);
sender.setText(name);
}
TextView datetime = (TextView) row.findViewById(R.id.datetime);
Long messageDatetime = messages.getLong(messages.getColumnIndex("datetime"));
String receivedOn;
if(startOfToday > messageDatetime){
receivedOn = getDateFromDatetime(messageDatetime);
} else {
receivedOn = getTimeFromDatetime(messageDatetime);
}
datetime.setText(receivedOn);
String message = messages.getString(messages.getColumnIndex("message"));
TextView messageContent = (TextView) row.findViewById(R.id.messageContent);
messageContent.setText(message);
return row;
}

Set different icons for each row

Above is the main screen of my app with a custom adapter for listview. How do I put a different icon for each row in the listview? I tried putting an ImageView in the xml but it overwrites my whole screen only showing an icon.
MainAcitivty xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/hp_color"
tools:context=".MainActivity" >
<TextView
android:id="#+id/cityText"
style="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textSize="28sp" />
<ImageView
android:id="#+id/condIcon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_below="#id/cityText"
android:contentDescription="weather icon" />
<TextView
android:id="#+id/condDescr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/condIcon"
android:textSize="20sp" />
<ListView
android:id="#+id/mainListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<ImageButton
android:id="#+id/btnPicturePage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="picturePage"
android:src="#drawable/photo" />
Custom adapter:
public class WeatherAdapter extends ArrayAdapter<String> {
private final Activity context;
private ArrayList<String> weatherData;
static class ViewHolder {
public TextView text;
public ImageView image;
}
public WeatherAdapter(Activity context, ArrayList<String> weatherData) {
super(context, R.layout.list, weatherData);
this.context = context;
this.weatherData = weatherData;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
// reuse views
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.list, null);
// configure view holder
ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) rowView.findViewById(R.id.rowTextView);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
ArrayList<String> s = new ArrayList<String>(weatherData);
String []sa = new String [s.size()];
sa = s.toArray(sa);
holder.text.setText(sa[position]);
if (sa[position].startsWith("Air pressure")) {
rowView.setBackgroundResource(R.color.skyblue) ;
}
return rowView;
}
List xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#color/white" >
</TextView>
Your list.xml should be something similar to this (taken from one my files)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:gravity="center_vertical" android:orientation="horizontal"
android:background="#drawable/xml_pressed_bg" android:clickable="true" android:layout_width="fill_parent"
android:layout_height="54dip"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ttshow="http://schemas.android.com/apk/res-auto">
<ImageView android:id="#+id/imgThumb" android:layout_width="42.0dip" android:layout_height="42.0dip" android:scaleType="fitXY"
android:src="#drawable/sample_dj" />
<TextView android:textSize="14.0sp" android:textColor="#ff666666" android:id="#+id/txtName"
android:layout_width="180dip" android:layout_height="wrap_content"
android:singleLine="true" android:shadowColor="#99ffffff" style="#style/TextShadowLight"
android:text="Hello" android:paddingLeft="10dip"/>
</LinearLayout>
Every row item has an image (imgThumb). This needs to be changed or set to a static image, depending on your needs. This is done in the adapters' getView() method.
imgThumb.setImageResource(R.drawable.my_image);
Private TypedArray img;
img=getResource.obtainTypedArray(R.array.imgIcon)
if (sa[position].startsWith("Air pressure")) {
rowView.setBackgroundResource(R.color.skyblue) ;
holder.image.setImageResource(img.gerResourceId[0]);
}else if (sa[position].startsWith("Air Condition")) {
holder.image.setImageResource(img.gerResourceId[1]);
}

Populating a multicolumn listview without a hashmap

I have been reading about multicolumn listviews today. They seem to be what I need, but can't info info about how to populate them not using a hash map. All examples I have seen use a hash map to build the listview data adapter but this is too heavy when lot of data is implied. Is there anyway to populate a list view without a hash list?
All examples I have seen use something like this:
http://saigeethamn.blogspot.com.es/2010/04/custom-listview-android-developer.html
Yes it is possible. please Check out the code.
public class List_view_ArrayAdapter extends ArrayAdapter<String> {
private Context context;
private String[] name;
private String[] address;
private String[] rating;
private TextView nameTextView;
private TextView addressTextView;
private ImageView lisiconImageView;
private AppManagers appManagers;
public List_view_ArrayAdapter(Context context, String[] name,
String[] address, String[] rating) {
super(context, R.layout.list_layout, name);
this.context = context;
this.name = name;
this.address = address;
this.rating = rating;
appManagers = new AppManagers();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_layout, parent, false);
nameTextView = (TextView) rowView.findViewById(R.id.nameTextView);
addressTextView = (TextView) rowView.findViewById(R.id.addressTextView);
lisiconImageView = (ImageView) rowView
.findViewById(R.id.listiconImageView);
nameTextView.setText(name[position]);
addressTextView.setText(address[position]);
lisiconImageView.setImageDrawable(appManagers
.ImageOperations(rating[position]));
// imageView.setImageResource(imageid[position]);
return rowView;
}
}
Here is list_layout for this line "R.layout.list_layout"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" android:background="#d9d9d9">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" android:layout_marginBottom="5dp" android:layout_marginTop="5dp">
<ImageView
android:id="#+id/listiconImageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:layout_marginLeft="5dp" android:gravity="center_vertical" android:layout_marginBottom="5dp" android:layout_marginTop="5dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name : "
android:textColor="#2a170e" />
<TextView
android:id="#+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#2a170e" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address : "
android:textColor="#2a170e" />
<TextView
android:id="#+id/addressTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#2a170e" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Finaly you call it from your main Activity.
new List_view_ArrayAdapter arrayAdapter= new List_view_ArrayAdapter(context, appManagers.getNameArray(cafeandbarsList), name, address, rating);
listView.setAdapter(arrayAdapter);
I think it help you.
Thanks

Categories

Resources