Set different icons for each row - android

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]);
}

Related

How to declare imageview without set/inflate the layout?

is there any code to declare the imageview and set imageresource without inflate the layout ?
i am create listview adapter in other layout, so i didnt inflate the layout.
but ineed to set image to the imageview.
this is my code but its not working
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_list_request, container, false);
setHasOptionsMenu(true);
rlRequestWorkflow = (RelativeLayout)getActivity().findViewById(R.id.rlListRequestWorkflow);
listPhoto = (ImageView)getActivity().findViewById(R.id.listPhoto);
rlRequestWorkflow.addView(listPhoto);
}
this is my current layout (main class)
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/flRequest"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
tools:context="com.ITK.SMP.Native.Workflow.ListRequest">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:background="#ffffff">
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lvRequest"
android:choiceMode="singleChoice"
android:nestedScrollingEnabled="false"
/>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
this is listadapter class
public class ListRequestAdapter extends ArrayAdapter<ListRequestItem> {
ImageView listPhoto;
public ListRequestAdapter(Context context, List<ListRequestItem> items)
{
super(context, R.layout.style_fragment_list_request, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
// inflate the GridView item layout
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.style_fragment_list_request, parent, false);
// initialize the view holder
viewHolder = new ViewHolder();
viewHolder.tvTanggal = (TextView) convertView.findViewById(R.id.tvTanggalRequest);
viewHolder.tvTipe = (TextView) convertView.findViewById(R.id.tvTipeRequest);
viewHolder.tvStatus = (TextView) convertView.findViewById(R.id.tvStatus);
viewHolder.permitid = "";
convertView.setTag(viewHolder);
} else {
// recycle the already inflated view
viewHolder = (ViewHolder) convertView.getTag();
}
// update the item view
ListRequestItem item = getItem(position);
viewHolder.tvTanggal.setText(item.tanggal);
viewHolder.tvTipe.setText(item.tipe);
viewHolder.tvStatus.setText(item.status);
viewHolder.permitid = item.permitid;
return convertView;
}
private static class ViewHolder {
TextView tvTanggal;
TextView tvTipe;
TextView tvStatus;
String permitid;
}
}
this is the imageview in the listadapterlayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="reqtag"
android:id="#+id/rlListRequestWorkflow"
>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:weightSum="1"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="0.2"
android:orientation="vertical"
android:layout_height="wrap_content">
<!--ImageView here-->
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/listPhoto"
android:layout_width="65dp"
android:layout_height="63dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:src="#drawable/ic_menu_camera"
android:layout_marginTop="10dp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="0.8"
android:orientation="vertical"
android:layout_height="wrap_content">
<!--All textViews here-->
<TextView
android:layout_width="100dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Type"
android:textSize="18dp"
android:fontFamily="sans-serif"
android:textColor="#color/black"
android:id="#+id/tvTipeRequest"
android:width="130dp" />
<TextView
android:layout_width="100dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Date"
android:fontFamily="sans-serif"
android:id="#+id/tvTanggalRequest"
android:textSize="15dp"
android:width="130dp" />
<TextView
android:layout_width="100dp"
android:layout_weight="1"
android:layout_height="50dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black"
android:text="Status"
android:textSize="15dp"
android:fontFamily="sans-serif"
android:id="#+id/tvStatus"
android:layout_column="38" />
</LinearLayout>
</LinearLayout>
any help would be appreciated
If you don't want to inflate your imageview, then pass your imageview in the constructor of class, and set the image from there.
Suppose, MainActivity is the class where your image view is present
MainActivity.class
ImageView imageview;
imageview=(ImageView)findViewById(R.Id.imageview);
new ListAdapter(imageview);
ListAdapter.class
ImageView imageview;
//constructor
public ListAdapter(ImageView imageview){
this.imageview=imageview;
}
//now you can use imageview to set image
Edit this:-
rlRequestWorkflow = (RelativeLayout)view.findViewById(R.id.rlListRequestWorkflow);
listPhoto = (ImageView)view.findViewById(R.id.listPhoto);

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.

Get "No results" Message Custom Adapter ListView Android

I am having a problem to set a "No results" message when getCount() returns 0.
I have a custom layout for the listview, and I simply want to get a "No Results" textview when there is no data on the listview.
Any idea how to achieve this?
I saw something with a empty id, but that needs to be on a listview, since this is a custom layout, it doesn't have that "tag".
Custom Adapter Code:
public class ListScheduleAdapter extends BaseAdapter {
Context context;
protected List<Schedule> listSchedules;
LayoutInflater inflater;
public ListScheduleAdapter(Context context, List<Schedule> listSchedules) {
this.listSchedules = listSchedules;
this.inflater = LayoutInflater.from(context);
this.context = context;
}
public int getCount() {
return listSchedules.size();
}
public Schedule getItem(int position) {
return listSchedules.get(position);
}
public long getItemId(int position) {
return listSchedules.get(position).getCod_Horario();
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = this.inflater.inflate(R.layout.layout_list_item,
parent, false);
holder.txtTeacher = (TextView) convertView
.findViewById(R.id.txt_teacher);
holder.txtDayofWeek = (TextView) convertView
.findViewById(R.id.txt_dayofweek);
holder.txtSubject = (TextView) convertView
.findViewById(R.id.txt_subject);
holder.txtTime = (TextView) convertView
.findViewById(R.id.txt_time);
holder.txtRoom = (TextView) convertView
.findViewById(R.id.txt_room);
holder.txtClass = (TextView) convertView
.findViewById(R.id.txt_class);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Schedule sche = listSchedules.get(position);
String fullTeacher = sche.getProfessor();
String[] names = fullTeacher.split(" ");
holder.txtTeacher.setText(names[0] + " " + names[names.length-1]);
holder.txtDayofWeek.setText(sche.getDia_Semana());
holder.txtSubject.setText(sche.getDisciplina());
holder.txtTime.setText(sche.getT_Entrada() + "h-" + sche.getT_Saida()+"h");
holder.txtRoom.setText(sche.getSala());
holder.txtClass.setText(sche.getTurma());
return convertView;
}
private class ViewHolder {
TextView txtTeacher;
TextView txtDayofWeek;
TextView txtSubject;
TextView txtTime;
TextView txtRoom;
TextView txtClass;
}
}
Fragment Code (method called on a async OnPostExecute method):
private void populateListView() {
ListScheduleAdapter adapter = new ListScheduleAdapter(getActivity(), ScheduleList);
listviewHorarios.setAdapter(adapter);
}
Layout code:
<?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:orientation="horizontal"
android:padding="8dp" >
<LinearLayout
android:id="#+id/layout_row1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/txt_teacher"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="normal" />
<TextView
android:id="#+id/txt_dayofweek"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="normal" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout_row2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/layout_row1"
android:orientation="horizontal" >
<TextView
android:id="#+id/txt_subject"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="normal" />
<TextView
android:id="#+id/txt_time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="normal" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout_row3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/layout_row2"
android:orientation="horizontal" >
<TextView
android:id="#+id/txt_room"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="normal" />
<TextView
android:id="#+id/txt_class"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="normal" />
</LinearLayout>
<TextView
android:id="#+id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawablePadding="5dp"
android:gravity="center"
android:text="#string/no_data_available"
android:textColor="#android:color/black"
android:visibility="gone" />
</RelativeLayout>
add
<TextView
android:id="#+id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawablePadding="5dp"
android:gravity="center"
android:text="#string/no_data_available"
android:textColor="#android:color/black"
android:visibility="gone" />
to the same layout where you declared your ListView (wrap both in a FrameLayout)
on the ListView's instance call
listView.setEmptyView(findViewById(R.id.empty));
when the adapter is empty, you will see the TextView apper
add TextView to your listview layout and setVisibility of it to GONE and check the adapter:
if(adapter==null){
listView.setVisiblity(View.GONE);
tvEmpty.setVisiblity(View.VISIBLE);
}
hope this will help.

Background of List items is stretching more than height?

I have a listview with a custom adapter (tile) design. Everything should work fine but the thing is that the til (listview item background) stretches too much despite me giving layout_height a fixed value. Here's the code:
This is the xml for the custom tile. Notice the 85dp fixed height. The background drawable should NOT stretch beyond this hight?
<?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="85dp"
android:background="#drawable/tile_job" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="9dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="13dp"
android:text="Title" />
<TextView
android:id="#+id/pickup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="38dp"
android:layout_alignLeft="#+id/title"
android:textSize="11dp"
android:text="Pickup Address" />
<TextView
android:id="#+id/destination"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/pickup"
android:layout_below="#+id/pickup"
android:layout_marginTop="6dp"
android:textSize="11dp"
android:text="Destination Address" />
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/destination"
android:layout_alignParentRight="true"
android:layout_marginRight="40dp"
android:text="Time"
android:textSize="11dp" />
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/time"
android:layout_below="#+id/time"
android:text="Date"
android:layout_marginTop="7dp"
android:textSize="11dp" />
</RelativeLayout>
Now I'm using this in a simple listview:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/app_bg" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/strip_top" >
<TextView
android:id="#+id/strip_text"
style="#style/TopStripText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Locate" />
</RelativeLayout>
<RelativeLayout
android:layout_width="280dp"
android:layout_height="440dp"
android:layout_below="#+id/relativeLayout1"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:background="#drawable/bg_booking" >
<ListView
android:id="#+id/activebookings"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
What is causing the tile to overstretch vertically?
===========
EDIT
Here is the requested code:
Inflation of the layout:
public class JobAdapter extends BaseAdapter
{
private ArrayList<Job> bookingArrayList;
private LayoutInflater mInflater;
public JobAdapter(Context context, ArrayList<Job> results)
{
bookingArrayList = results;
mInflater = LayoutInflater.from(context);
}
#Override
public int getCount()
{
return bookingArrayList.size();
}
#Override
public Object getItem(int arg0)
{
return bookingArrayList.get(arg0);
}
#Override
public long getItemId(int position)
{
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.tile_job, null);
holder = new ViewHolder();
holder.date = (TextView) convertView.findViewById(R.id.date);
holder.time = (TextView) convertView.findViewById(R.id.time);
holder.pickup = (TextView) convertView.findViewById(R.id.pickup);
holder.destination = (TextView) convertView.findViewById(R.id.destination);
holder.title = (TextView) convertView.findViewById(R.id.title);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.date.setText(bookingArrayList.get(position).getDate());
holder.time.setText(bookingArrayList.get(position).getTime());
holder.pickup.setText(bookingArrayList.get(position).getPickup());
holder.destination.setText(bookingArrayList.get(position).getDestination());
holder.title.setText(bookingArrayList.get(position).getTitle());
return convertView;
}
static class ViewHolder
{
TextView date;
TextView time;
TextView pickup;
TextView destination;
TextView title;
}
}
Your creating you row with following:
convertView = mInflater.inflate(R.layout.tile_job, null);
Now try this:
convertView = mInflater.inflate(R.layout.tile_job, parent, false);
If you don't attach the parent layout the layout parameters from xml will be ignored. You set false as you don't attach the view to the root right away (this happens automatically when the view is returned). You can read more about this issue here.

ListView Rowlayout in a own coded Chat

What I'm trying to do
I'm trying to create a chat which has to diffrent row's. For every row I made a own layout file, but the problem is that the layoutfile of one row dosn't fit the screen.
Question
What do I need to change in the row layout that it fits like it should. You'll find the code and also a printscreen of what I'm trying.
Code
ListAdapter
public class ChatListAdapter extends BaseAdapter {
private ArrayList<String> ContentList;
private ArrayList<Integer> ChatUserList;
private static LayoutInflater inflater = null;
public ChatListAdapter(Activity activity, ArrayList<String> _ContentList,
ArrayList<Integer> _ChatUserList) {
ContentList = _ContentList;
ChatUserList = _ChatUserList;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return ContentList.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
TextView tv_text;
TextView tv_date;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
if (ChatUserList.get(position) == 1) {
// I'm RECIVER -> Message is left align
vi = inflater.inflate(R.layout.rowlayout_leftalign, null);
tv_text = (TextView) vi.findViewById(R.id.tv_chat_leftalign);
tv_date = (TextView) vi.findViewById(R.id.tv_chat_leftalign_date);
} else {
// I'm SENDER -> Message is right align
vi = inflater.inflate(R.layout.rowlayout_rightalign, null);
tv_text = (TextView) vi.findViewById(R.id.tv_chat_rightalign);
tv_date = (TextView) vi.findViewById(R.id.tv_chat_rightalign_date);
}
tv_date.setText(sdf.format(new Date()));
tv_text.setText(ContentList.get(position));
tv_text.setMaxWidth(((Chat.display.getWidth() / 4) * 3));
return vi;
}
RowLayout XML Left (Blue bubbles)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#android:color/transparent" >
<RelativeLayout
android:id="#+id/ly_rf_row_r"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginBottom="2.5dp"
android:layout_marginTop="2.5dp"
android:background="#drawable/shape_rightalign"
android:paddingBottom="2.5dp"
android:paddingTop="2.5dp" >
<TextView
android:id="#+id/tv_chat_rightalign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="right"
android:paddingBottom="2dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/tv_chat_rightalign_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="-3dp"
android:layout_marginLeft="-3dp"
android:layout_toLeftOf="#id/tv_chat_rightalign"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:textColor="#android:color/black"
android:textSize="8.5dp" />
</RelativeLayout>
</RelativeLayout>
RowLayout XML right (red bubbles)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#android:color/transparent" >
<RelativeLayout
android:id="#+id/ly_rf_row_l"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginBottom="2.5dp"
android:layout_marginTop="2.5dp"
android:background="#drawable/shape_leftalign"
android:paddingBottom="2.5dp"
android:paddingTop="2.5dp" >
<TextView
android:id="#+id/tv_chat_leftalign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="left"
android:paddingBottom="2dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/tv_chat_leftalign_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="-3dp"
android:layout_marginLeft="-3dp"
android:layout_toRightOf="#id/tv_chat_leftalign"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:textColor="#android:color/black"
android:textSize="8.5dp" />
</RelativeLayout>
</RelativeLayout>
for both XML add android:layout_width="fill_parent" and android:layout_height="wrap_content" like followings
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#android:color/transparent"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
and so on...

Categories

Resources