Android ListView is slow to create and bind - android

I (as many other) have problems getting smooth performance in my listview. I believe I have done everything by the book, but it still lags significantly (~300-500 ms)every time a new item is being drawn on the screen.
I'm using a viewHolder and reusing as many UI elements as possible (I think). There are two clear bottle necks.
The first is the line inflating the XML code in the newView() method. That is for some reason really slow (~300 ms) and I don't know if this is expected or not.
Secondly the bindView takes a long time to execute. This is only because of the TextView.setText() method calls which takes up all the time. Other people have had the same problem and I do understand that it can be a slow thing if Android needs to remeasure the entire UI for every listitem. However, after changing the XML file TextViews to a fixed size I still get really slow performance from .setText(). I know I could put the .setText in a Handler but that seems like a hack in this case. I would prevent the list from being laggy, but it would not add the text any faster. I believe I should be able to set the text faster than ~400 ms and if it takes this long something is wrong with my UI/logic.
But without further ado I present you with the source code: Hope to get some clever insight into what I'm doing wrong.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (ApplicationConfiguration.DEBUGGING)
Debug.startMethodTracing("getview");
View listViewItem;
EpisodeViewHolder holder;
Cursor itemCursor = (Cursor) getItem(position);
if (!itemCursor.moveToPosition(position)) {
throw new IllegalStateException("couldn't move cursor to position "
+ position);
}
mCurrentFeedItem = MyListItem.getByCursor(itemCursor);
int type = getItemViewType(position);
if (convertView == null) {
switch (type) {
case TYPE_EXPAND:
listViewItem = newView(mContext, itemCursor, parent);
break;
default:
listViewItem = newView(mContext, itemCursor, parent);
break;
}
} else {
listViewItem = convertView;
}
bindView(listViewItem, mContext, itemCursor);
switch (type) {
case TYPE_EXPAND:
getPlayerViewHolder(position, listViewItem, itemCursor,
convertView, parent);
}
if (ApplicationConfiguration.DEBUGGING)
Debug.stopMethodTracing();
return listViewItem;
}
This is my newView
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = mInflater.inflate(R.layout.episode_list, null); // here is the bottleneck
EpisodeViewHolder holder = new EpisodeViewHolder();
holder.icon = (ImageView) view.findViewById(R.id.list_image);
holder.mainTitle = (TextView) view.findViewById(R.id.title);
holder.subTitle = (TextView) view.findViewById(R.id.podcast);
holder.timeDuration = (TextView) view.findViewById(R.id.duration);
holder.currentPosition = (TextView) view.getTag(R.id.current_position);
holder.slash = (TextView) view.findViewById(R.id.time_slash);
holder.fileSize = (TextView) view.findViewById(R.id.filesize);
holder.stub = (ViewStub) view.findViewById(R.id.stub);
holder.playerView = (View) view.findViewById(R.id.stub_player);
view.setTag(holder);
return view;
}
and my bindView method is really long but I'll shorten it down a bit
#Override
public void bindView(View view, Context context, Cursor cursor) {
.....
final EpisodeViewHolder holder = (EpisodeViewHolder) view.getTag();
.....
// not a bottleneck
// Loads the image Async. The synchronous part of the call takes around 10%
// of the entire method
PicassoWrapper.load(mContext, i, holder.icon);
// This is the bottleneck
holder.mainTitle.setText(title);
}
XML UI
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_marginRight="7dip"
android:padding="0dip" >
<ImageView
android:id="#+id/list_image"
style="#style/PodcastImage" />
</LinearLayout>
<TextView
android:id="#+id/title"
style="#style/EpisodeHeaderFont"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:text="Podcast Title" />
<TextView
android:id="#+id/podcast"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_above="#+id/current_position"
android:layout_below="#id/title"
android:layout_marginTop="1dip"
android:layout_toRightOf="#+id/thumbnail"
android:ellipsize="end"
android:text="Podcast Name"
android:textSize="10dip" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/current_position"
style="#style/EpisodeMinorFont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail" />
<TextView
android:id="#+id/time_slash"
style="#style/EpisodeMinorFont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/current_position"
android:layout_alignBottom="#+id/current_position"
android:layout_toRightOf="#+id/current_position"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:visibility="gone" />
<TextView
android:id="#+id/duration"
style="#style/EpisodeMinorFont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/time_slash"
android:layout_alignBottom="#+id/time_slash"
android:layout_toRightOf="#+id/time_slash" />
<TextView
android:id="#+id/filesize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/duration"
android:layout_toRightOf="#+id/duration"
android:gravity="bottom|right"
android:text="0"
android:textSize="10dip"
android:textStyle="bold" />
</RelativeLayout>
<FrameLayout
android:id="#id/drag_handle"
android:layout_width="25dp"
android:layout_height="match_parent"
android:paddingBottom="30dp"
android:paddingTop="30dp" >
<LinearLayout
android:layout_width="7dp"
android:layout_height="fill_parent"
android:background="#drawable/drag_handle"
android:orientation="vertical"
android:tileMode="repeat" >
</LinearLayout>
</FrameLayout>
</LinearLayout>
<ViewStub
android:id="#+id/stub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inflatedId="#+id/stub_player"
android:layout="#layout/list_item_expanded" />

Related

Android ImageView won't load on CustomList with ViewHolder

I have a custom list view which I recently upgraded to use View Holder. Right now the project works fine but the images in the two Image Views don't show on the screen.
This two black icons appear "transparent" but work if I click them.
This is the layout:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/imgInfo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="12dp"
android:layout_weight="0.04"
android:visibility="visible"
app:srcCompat="#drawable/details64" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/imgInfo"
android:layout_toLeftOf="#+id/imgEdit"
android:layout_toRightOf="#+id/imgInfo"
android:layout_weight="0.24"
android:orientation="vertical">
<TextView
android:id="#+id/txtChamado"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAlignment="center"
android:textColor="#android:color/background_dark"
android:textSize="25sp" />
<TextView
android:id="#+id/txtSolicitante"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="#android:color/background_dark"
android:textSize="15sp" />
<TextView
android:id="#+id/txtItemDeCatalogo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="#android:color/background_dark"
android:textSize="15sp" />
</LinearLayout>
<ImageView
android:id="#+id/imgEdit"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="12dp"
android:layout_weight="0.03"
android:contentDescription=""
app:srcCompat="#drawable/pencil64" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
This is the code snippet:
#Override
public View getView(final int position, View view, final ViewGroup parent) {
View convertView;
ViewHolder holder;
if (view == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.customized_list_view, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}else{
convertView = view;
holder = (ViewHolder) convertView.getTag();
}
// Object declaration
final Sette setteData = getItem(position);
//region Put the Tasks values to the textviews
// Get me two strings, one with the older value and one with the newest
String oldChamado = holder.txtChamado.getText().toString();
String newChamado = setteData.getChamado();
// Do not repeat the string in the textview
if (!(oldChamado.equals(newChamado))) {
holder.txtChamado.setText(newChamado);
}
//region Click Listener for the button that leads to Approvals Activity
if (!aBoolean) {
holder.imgEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chamadoClicked = setteData.getChamado();
solicitanteClicked = setteData.getSolicitante();
itemDeCatalogoClicked = setteData.getItemDeCatalogo();
id = setteData.getId();
Intent intent = new Intent(context.getApplicationContext(), ApprovalsActivity.class);
intent.putExtra("Id", id);
intent.putExtra("chamadoClicked", chamadoClicked);
intent.putExtra("solicitanteClicked", solicitanteClicked);
intent.putExtra("itemDeCatalogoClicked", itemDeCatalogoClicked);
context.startActivity(intent);
}
});
}
//endregion
return convertView;
}// END METHOD
ViewHolder (which I got from an internet example):
public class ViewHolder {
TextView txtChamado;
TextView txtSolicitante;
TextView txtItemDeCatalogo;
ImageView imgInfo, imgEdit;
Typeface bebasKai;
Typeface london;
public ViewHolder(View view){
bebasKai = Typeface.createFromAsset(context.getAssets(), "fonts/bebaskai.otf");
london = Typeface.createFromAsset(context.getAssets(), "fonts/london.ttf");
txtChamado = (TextView) view.findViewById(R.id.txtChamado);
txtChamado.setTypeface(bebasKai);
txtSolicitante = (TextView) view.findViewById(R.id.txtSolicitante);
txtSolicitante.setTypeface(london);
txtItemDeCatalogo = (TextView) view.findViewById(R.id.txtItemDeCatalogo);
txtItemDeCatalogo.setTypeface(london);
imgEdit = (ImageView) view.findViewById(R.id.imgEdit);
imgInfo = (ImageView) view.findViewById(R.id.imgInfo);
}
}
I tried clean, rebuild, download the project from BitBucket again, reinstall the app from scratch on my phone, invalidade cache/restart and check the code.
Am I missing something obvious here?
**EDIT:**Thank you SaNtoRiaN, I knew it was something simple that I was missing.
Change app:srcCompat attribute in your ImageView to android:src to view your images properly. To know more about the difference, check this answer.

Multiple Clickable Areas in ListItem

I have a list that is organized as follows:
CheckBox,
ImageButton,
LinearLayout{ TextView title, TextView Subtitle}
What I am trying to do is to have three separate clickable areas of the ListItem: the checkbox should be checkable, ImageButton, and the actual list item itself. The checkbox is hidden by default, but can be made visible when a certain button on the list's header is pressed (ie SELECT). I just want a way to select multiple items from the list, and I am not irreversibly set on using hidden checkboxes- if there is a way to do this that would be easier, I would be happy to hear the alternative.
Here is the relevant snippet of my adapter:
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder;
final T t = list.get(position);
if(convertView == null){
convertView = vi.inflate(R.layout.folder_list_item,parent,false);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.text_view_folder_item);
holder.subtitle = (TextView) convertView.findViewById(R.id.text_view2);
holder.eye = (ImageButton) convertView.findViewById(R.id.folder_vis_button);
holder.check = (CheckBox) convertView.findViewById(R.id.phone);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(t.getName());
holder.subtitle.setText(t.getDescription());// = (TextView) convertView.findViewById(R.id.text_view2);
return convertView;
}
And the XML is as follows. As you can see, I have made the individual list items clickable but unfocassable, as recommended in posts I have read about this very problem.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:clickable="true"
android:focusable="true">
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:visibility="invisible"
android:focusable="false"/>
<ImageButton
android:id="#+id/folder_vis_button"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:scaleType="fitXY"
android:layout_toRightOf="#+id/checkbox"
android:contentDescription="#string/contentDescription"
android:focusable="false"
android:src="#drawable/open_eyeball" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_toRightOf="#id/folder_vis_button"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical"
android:focusable="false"
android:clickable="true">
<TextView
android:id="#+id/text_view_folder_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/text_view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
Clicking on the list item is handled, if I convert the listitem to actually use on of the built in listitems such as android.R.layout.simple_list_item_1.
However, when I use my own layout, I cannot get click events on the list item itself, just on the image button.
I have tried inflating the view in a different way (int resource, ViewGroup root) ... I have tried working just with the imagebutton, or just with the checkbox, using an imageview instead of an imagebutton, with little in terms of success. Thank you for taking the time to look at this.

Android GridView onItemClickListener doesn't fire

This is my GridView ArrayAdapter:
<?xml version="1.0" encoding="utf-8"?>
<!-- gridview adapter layout -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.coapps.pico"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#drawable/gridview_background_selector" >
<!-- profile picture layout -->
<RelativeLayout
android:id="#+id/fragment_events_adapter_relativelayout_profile_picture"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp" >
<!-- profile picture image view -->
<ImageView
android:id="#+id/fragment_events_adapter_imageview_profile_picture"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:contentDescription="#string/app_name"
android:scaleType="fitXY"
android:src="#drawable/facebook_blank_profile" />
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/background_black_transparent" >
<!-- user name text view -->
<com.coapps.pico.NillanTextView
android:id="#+id/fragment_events_adapter_textview_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:src="#drawable/facebook_blank_profile"
android:textColor="#android:color/white"
app:isBold="true" />
</HorizontalScrollView>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/fragment_events_adapter_relativelayout_profile_picture"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:background="#color/orange" >
<HorizontalScrollView
android:id="#+id/fragment_events_adapter_horizontalscrollview_event_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" >
<!-- event name -->
<com.coapps.pico.NillanTextView
android:id="#+id/fragment_events_adapter_textview_event_name"
style="#style/text_shadow_black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:src="#drawable/facebook_blank_profile"
android:textAppearance="#android:style/TextAppearance.Small"
android:textColor="#android:color/white"
app:isBold="true" />
</HorizontalScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/fragment_events_adapter_horizontalscrollview_event_name"
android:layout_centerHorizontal="true"
android:gravity="center"
android:orientation="horizontal" >
<!-- event icon -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="15dp"
android:layout_gravity="right"
android:layout_margin="2dp"
android:contentDescription="#string/app_name"
android:src="#drawable/ic_action_event" />
<!-- event start time -->
<com.coapps.pico.NillanTextView
android:id="#+id/fragment_events_adapter_textview_start_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:src="#drawable/facebook_blank_profile"
android:textColor="#android:color/black"
android:textSize="12sp"
app:isBold="true" />
</LinearLayout>
</RelativeLayout>
<!-- attending event -->
<ImageView
android:id="#+id/fragment_events_adapter_imageview_attending_mask"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:background="#color/background_black_transparent"
android:contentDescription="#string/app_name"
android:padding="30dp"
android:src="#drawable/v"
android:visibility="gone" />
</RelativeLayout>
and i'm setting an onItemClickedListener:
private void setGridView()
{
//create a new event list
picoEventsList = new ArrayList<PicoEvent>();
//create new gridview arrayadapter
arrayAdapter = new EventDetailsArrayAdapter(getActivity(), picoEventsList);
//set grid's view empty view
View emptyView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_events_empty_view, null);
gridView.setEmptyView(emptyView);
gridView.setOnItemClickListener(this);
gridView.setAdapter(arrayAdapter);
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//get clicked event
PicoEvent event = arrayAdapter.getItem(arg2);
//create new event details fragment
EventDetailsFragment eventDetailsFragment = new EventDetailsFragment();
//add the event to fragment's arguments
eventDetailsFragment.setArguments(PicoEventCreator.createBundle(event));
//show the event details fragment
getMainActivity().showFragment(null, eventDetailsFragment, true, true);
}
but the onItemClick() method doesn't get fired..
I've checked and non of my views in my layout has clickable="true" setted...
Any ideas ?
Hey guyz finally got a solution...
what we were doing is directly accessing the Layout inside the GridView, so the onItemClickListener finds it confusing to access the item.
So the solution is to apply the onClickListener inside the Adapter (i.e. normally ArrayAdapter)
so what i m trying to say is:
public View getView(int position, View convertView, ViewGroup parent) {
//Here row is a view and we can set OnClickListener on this
final View row;
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
//Here we inflate the layout to view (linear in my case)
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.imageTitle = (TextView) row.findViewById(R.id.text);
holder.image = (ImageView) row.findViewById(R.id.image);
row.setTag(holder);
} else {
row = convertView;
holder = (ViewHolder) row.getTag();
}
ImageItem item = data.get(position);
holder.imageTitle.setText(item.getTitle());
holder.image.setImageBitmap(item.getImage());
//Now get the id or whatever needed
row.setId(position);
// Now set the onClickListener
row.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "Clicked" + row.getId() + "!!",
Toast.LENGTH_SHORT).show();
}
});
return row;
}
Ok i found what the problem was
Apparently GridView and HorzonitalScrollView are not such good friends...
probably because the HorzonitalScrollView has onClick funcionality..
The solution i found is to put:
android:descendantFocusability="blocksDescendants"
in my ArrayAdapter root view.

Create custom ListView with shadow

How could I create custom ListView like shown in this picture. I know I can make this happen with help of ScrollView and Layouts, but I need to do as a ListView.
Each listview item go over each other.
This may help you
List view with custom view items with partially overlaps (Android)
I have this code like your Requirement. List may overlap by reducing the dividerheight to minus value
<?xml version="1.0" encoding="utf-8"?>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="-5dp"
android:listSelector="#drawable/list_selector" />
Then add background color to adapter according to the position.
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
if(position % 2 ==0){
vi.setBackgroundResource(R.drawable.listselector_1);
}else if(position % 3 ==0){
vi.setBackgroundResource(R.drawable.listselector_2);
}else{
vi.setBackgroundResource(R.drawable.listselector_3);
}
song = data.get(position);
// Setting all values in listview
title.setText(song.get(CustomizedListView.KEY_TITLE));
artist.setText(song.get(CustomizedListView.KEY_ARTIST));
duration.setText(song.get(CustomizedListView.KEY_DURATION));
imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
return vi;
}
Otherwise you can use listselector images also as your requirement
Thank you guys for trying help me to solve this issue. I look every answer here and finally get i want. I think it will be better I put my code here so maybe someone it will be helpful. The only difference is that my created listview has also shadow in it.
Here is the code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:my_font="http://schemas.android.com/apk/res/com.Helix.android.SmartBonus"
android:orientation="vertical"
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="5dp"
>
<ImageView
android:id="#+id/imageview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/shadow_bg"
android:visibility="visible"/>
<RelativeLayout
android:id="#+id/secondLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#drawable/drop_shadow"
android:layout_below="#id/imageview">
<ImageView
android:id="#+id/companyImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/secret_logo_small"
android:layout_marginRight="10dp"/>
<TextView
android:id="#+id/companyDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/companyImageView"
android:textColor="#android:color/white"
android:layout_marginTop="7dp"
my_font:ttf_name="300"
android:text="Салон кросаты"/>
<TextView
android:id="#+id/companyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/companyImageView"
android:layout_below="#id/companyDescription"
android:textColor="#android:color/white"
my_font:ttf_name="300"
android:text="Secret"/>
<TextView
android:id="#+id/companyPercentText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:layout_marginTop="7dp"
android:textSize="19sp"
android:layout_marginRight="1dp"
android:layout_toLeftOf="#+id/companyPercent"
my_font:ttf_name="700"
android:text="-20"/>
<TextView
android:id="#id/companyPercent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textColor="#android:color/white"
android:layout_marginTop="7dp"
android:textSize="12sp"
my_font:ttf_name="300"
android:text="%"/>
<ImageView
android:id="#+id/companyPercentImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/companyPercent"
android:layout_marginTop="7dp"
android:textColor="#android:color/white"
android:src="#drawable/percentage_devider"/>
<TextView
android:id="#+id/companyDistance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/companyPercentImage"
android:textSize="16dp"
android:textColor="#A57F1C"
my_font:ttf_name="300"
android:text="7m"/>
<LinearLayout
android:id="#+id/checkingButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/companyDistance"
android:layout_marginTop="10dp"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:focusable="false"
android:background="#drawable/green_button_bg"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="16dp"
android:textColor="#android:color/white"
my_font:ttf_name="300"
android:text="Check-in"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="16dp"
android:layout_marginLeft="10dp"
android:textColor="#color/checkin_point_color"
android:layout_weight="1"
my_font:ttf_name="300"
android:text="#string/ten_point"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="#drawable/slak"
/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
adapter.
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
final Discount discount = getItem(i);
Discount prev_discount = null;
if (i > 0){
prev_discount = getItem(i-1);
}
if (view == null){
final LayoutInflater li = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = li.inflate(R.layout.discount_data_item, viewGroup, false);
viewHolder = new ViewHolder();
viewHolder.logo = (ImageView)view.findViewById(R.id.companyImageView);
viewHolder.name = (SmartBonus_TextView)view.findViewById(R.id.companyName);
viewHolder.description = (SmartBonus_TextView)view.findViewById(R.id.companyDescription);
viewHolder.percent = (SmartBonus_TextView)view.findViewById(R.id.companyPercentText);
viewHolder.distance = (SmartBonus_TextView)view.findViewById(R.id.companyDistance);
viewHolder.mainLayout = (RelativeLayout)view.findViewById(R.id.mainLayout);
viewHolder.secondLayaout = (RelativeLayout)view.findViewById(R.id.secondLayout);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
if (i == 0){
viewHolder.mainLayout.setBackgroundColor(android.R.color.transparent);
setRoundedBackground(viewHolder.secondLayaout, Color.parseColor(discount.getBackgroundColor()));
} else {
viewHolder.mainLayout.setBackgroundColor(Color.parseColor(prev_discount.getBackgroundColor()));
setRoundedBackground(viewHolder.secondLayaout, Color.parseColor(discount.getBackgroundColor()));
}
return view;
}
private void setRoundedBackground(View view,int color){
final GradientDrawable gradientDrawable = (GradientDrawable) view.getBackground().mutate();
gradientDrawable.setColor(color);
gradientDrawable.invalidateSelf();
}
Here is the result
You should override getView method.
Sample code:
View getView(int position, View convertView, ViewGroup parent){
Object data = getItem(positon);
//usually data object should have type property
if(data.type == TYPE1){
return getLayoutInflater().inflate(R.layout.custom_xml1, null);
}else (data.type == TYPE2){
return getLayoutInflater().inflate(R.layout.custom_xml2, null);
}else{
return getLayoutInflater().inflate(R.layout.custom_xml, null);
}
};
Note:You should reuse convertView object for performance.
Ok, As I have sort of time I manage to write few steps to achieve your task.
1. Make Custom drawable using xml in drawable folder with top corners have radius..
2. Now in your Listview attributes just define negative divider height
Like, android:dividerHeight="-20dp"
3. Set the Custom Drawable as a background of List row in getView().
Also its nice if you able to set color to drawable at dynamically for list row. (In getView() of Custom Adapter).
This what I have achieved from above steps:

Can't change Image source in my listview

I'm working on an e-commerce application, i need to display a list of products -a product = one ImageView and some textViews- from the database, but when i extract the data from the database, everting works fine except the imageView, it shows the same image that is in the source Layout.
this is the getView() Method in my adapter.
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView==null)
{
holder=new ViewHolder();
convertView = inflater.inflate(R.layout.lesproduits, null);
holder.nomduProduit = (TextView)convertView.findViewById(R.id.nomProduit);
holder.prixDuProduit = (TextView)convertView.findViewById(R.id.prixProduit);
holder.imageDuProduit = (ImageView)convertView.findViewById(R.id.imageProduit);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
Bitmap bitmapImage = BitmapFactory.decodeFile(path+File.separator+lesProduits.get(position).getImage());
Drawable drawableImage = new BitmapDrawable(bitmapImage);
System.out.println(path+File.separator+lesProduits.get(position).getImage());
holder.imageDuProduit.setBackgroundDrawable(drawableImage);
holder.nomduProduit.setText(lesProduits.get(position).getNomDuProduit());
holder.prixDuProduit.setText(lesProduits.get(position).getPrixDuProduit());
return convertView;
}
and below is the source Layout:
<?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="match_parent">
<ImageView
android:id="#+id/imageProduit"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="30dp"
android:layout_marginTop="5dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/nomProduit"
android:layout_marginTop="5dp"
android:layout_width="170dp"
android:layout_height="30dp"
android:layout_toRightOf="#+id/imageProduit"
android:text="Smart phone"
android:textSize="25sp" />
<TextView
android:id="#+id/prixProduit"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_alignLeft="#+id/nomProduit"
android:layout_below="#+id/nomProduit"
android:layout_marginTop="5dp"
android:text="Large Text"
android:textSize="15sp" />
</RelativeLayout>
You are changing the background, not the foreground.
ImageView has both foregrond and background images.
instead of this
holder.imageDuProduit.setBackgroundDrawable(drawableImage);
use this
holder.imageDuProduit.setImageDrawable(drawableImage);

Categories

Resources