I know, that here are lots of question about this topic and google have many tutorials about it, but I still have a problem, so please tell my where I'm wrong ;/. There are my classes:
layout class (activity_finish.xml):
<?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="fill_parent"
android:orientation="vertical">
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="3dp"
android:listSelector="#drawable/list_selector" />
</LinearLayout>
row layout (list_row.xml):
<?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:background="#drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip" >
<ImageView
android:id="#+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:contentDescription="#string/desc"
android:src="#drawable/icon" />
</LinearLayout>
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dip"
android:layout_alignBottom="#+id/destinationName"
android:gravity="right"
android:text="#string/time"
android:textColor="#10bcc9"
android:textSize="23dip" />
<TextView
android:id="#+id/destination"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="8dip"
android:layout_toRightOf="#+id/thumbnail"
android:text="#string/destination"
android:textColor="#343434"
android:textSize="13dip" />
<TextView
android:id="#+id/destinationName"
android:layout_width="153dip"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/destination"
android:layout_below="#+id/destination"
android:text="#string/destinationName"
android:textColor="#040404"
android:textSize="20dip"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/stopName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/thumbnail"
android:layout_below="#+id/thumbnail"
android:text="#string/stopName"
android:layout_marginLeft="3dip" />
<TextView
android:id="#+id/stopStreet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/thumbnail"
android:layout_below="#+id/stopName"
android:layout_marginTop="3dip"
android:text="#string/stopStreet"
android:layout_marginLeft="3dip"/>
<TextView
android:id="#+id/stopOneWay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/time"
android:layout_below="#+id/stopName"
android:layout_marginTop="3dip"
android:text="#string/stopOneWay"
android:layout_marginLeft="3dip"/>
<TextView
android:id="#+id/lineNumber"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignLeft="#+id/thumbnail"
android:layout_alignTop="#+id/thumbnail"
android:gravity="center"
android:text="#string/lineNumber"
android:layout_marginLeft="3dip"
android:textStyle="bold"
android:textSize="28dip"
android:textColor="#DCF0F5"/>
</RelativeLayout>
StopAdapter:
public class StopAdapter extends ArrayAdapter<DisplayStop> {
private List<DisplayStop> stops;
private LayoutInflater inflater;
private Context context;
int layoutResourceId;
public StopAdapter(Context context, int layoutResourceId, List<DisplayStop> stops) {
super(context, layoutResourceId, stops);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.stops = stops;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
ViewHolder holder = new ViewHolder();
if(row == null)
{
inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder.image = (ImageView) row.findViewById(R.id.list_image);
holder.time = (TextView) row.findViewById(R.id.time);
holder.destination = (TextView) row.findViewById(R.id.destination);
holder.destination = (TextView) row.findViewById(R.id.destinationName);
holder.stopName = (TextView) row.findViewById(R.id.stopName);
holder.stopStreet = (TextView) row.findViewById(R.id.stopStreet);
holder.stopOneWay = (TextView) row.findViewById(R.id.stopOneWay);
holder.lineNumber = (TextView) row.findViewById(R.id.lineNumber);
row.setTag(holder);
}
else{
holder = (ViewHolder)row.getTag();
}
holder.time.setText(stops.get(position).getTime());
holder.destination.setText("Kierunek:");
holder.destinationName.setText(stops.get(position).getDestination());
holder.stopName.setText(stops.get(position).getName());
holder.stopStreet.setText(stops.get(position).getAddress());
holder.lineNumber.setText(stops.get(position).getLine());
if(stops.get(position).getOneWay()) holder.stopOneWay.setText("P. jednokierunkowy");
else holder.stopOneWay.setText("");
int line = Integer.parseInt(stops.get(position).getLine());
if(line >= 50){
holder.image.setImageResource(R.drawable.icon);
}
else{
holder.image.setImageResource(R.drawable.icon2);
}
return row;
}
}
FinishActivity:
public class FinishActivity extends Activity {
private ListView lv;
private List<DisplayStop> stops;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_finish);
lv = (ListView) findViewById(R.id.list);
stops = getStops();//create several objects in list
if(stops.size() > 0){
StopAdapter stopAdapter = new StopAdapter(this, R.layout.list_row, stops);
lv.setAdapter(stopAdapter);
}
else
{
Log.d("klop", "0 elementów");
}
}
Please help, bacause I have no idea what can be wrong (I made several versions from severals tutorials and still nothing ;/)
Try doing it without extending the ArrayAdapter Class, and declare it directly as such:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, arrayListData);
lv.setAdapter(adapter);
If all you're doing is putting items from an Array/ArrayList, then you don't need to go further than just using the ArrayAdapter
In the definition of the ListView in activity_finish.xml, set the height to fill_parent
android:layout_height="fill_parent"
The list views content has no height when it is inflated, only once items are added.
Also there is no need to hold a pointer to the items in the adapter
remove this:
private List<DisplayStop> stops;
Then when you want to access the items in the ListAdapter, use this instead:
DisplayStop item = this.getItem(position);
layout class (activity_finish.xml):
<?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="fill_parent"
android:orientation="vertical">
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="3dp"
android:listSelector="#drawable/list_selector" />
</LinearLayout>
row layout (list_row.xml):
<?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:background="#drawable/list_selector"
android:padding="5dip" >
<ImageView
android:id="#+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:contentDescription="#string/desc"
android:src="#drawable/icon" />
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dip"
android:layout_alignBottom="#+id/destinationName"
android:gravity="right"
android:text="#string/time"
android:textColor="#10bcc9"
android:textSize="23dip" />
<TextView
android:id="#+id/destination"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="8dip"
android:layout_toRightOf="#+id/thumbnail"
android:text="#string/destination"
android:textColor="#343434"
android:textSize="13dip" />
<TextView
android:id="#+id/destinationName"
android:layout_width="153dip"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/destination"
android:layout_below="#+id/destination"
android:text="#string/destinationName"
android:textColor="#040404"
android:textSize="20dip"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/stopName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/thumbnail"
android:layout_below="#+id/thumbnail"
android:text="#string/stopName"
android:layout_marginLeft="3dip" />
<TextView
android:id="#+id/stopStreet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/thumbnail"
android:layout_below="#+id/stopName"
android:layout_marginTop="3dip"
android:text="#string/stopStreet"
android:layout_marginLeft="3dip"/>
<TextView
android:id="#+id/stopOneWay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/time"
android:layout_below="#+id/stopName"
android:layout_marginTop="3dip"
android:text="#string/stopOneWay"
android:layout_marginLeft="3dip"/>
<TextView
android:id="#+id/lineNumber"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignLeft="#+id/thumbnail"
android:layout_alignTop="#+id/thumbnail"
android:gravity="center"
android:text="#string/lineNumber"
android:layout_marginLeft="3dip"
android:textStyle="bold"
android:textSize="28dip"
android:textColor="#DCF0F5"/>
</RelativeLayout>
StopAdapter:
public class StopAdapter extends ArrayAdapter<DisplayStop> {
private List<DisplayStop> stops;
public StopAdapter(Context context, int layoutResourceId, List<DisplayStop> stops) {
super(context, layoutResourceId, stops);
this.stops = stops;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
ViewHolder holder = new ViewHolder();
if(row == null)
{
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.list_row, parent, false);
holder.image = (ImageView) row.findViewById(R.id.list_image);
holder.time = (TextView) row.findViewById(R.id.time);
holder.destination = (TextView) row.findViewById(R.id.destination);
holder.destination = (TextView) row.findViewById(R.id.destinationName);
holder.stopName = (TextView) row.findViewById(R.id.stopName);
holder.stopStreet = (TextView) row.findViewById(R.id.stopStreet);
holder.stopOneWay = (TextView) row.findViewById(R.id.stopOneWay);
holder.lineNumber = (TextView) row.findViewById(R.id.lineNumber);
row.setTag(holder);
}
else{
holder = (ViewHolder)row.getTag();
}
holder.time.setText(stops.get(position).getTime());
holder.destination.setText("Kierunek:");
holder.destinationName.setText(stops.get(position).getDestination());
holder.stopName.setText(stops.get(position).getName());
holder.stopStreet.setText(stops.get(position).getAddress());
holder.lineNumber.setText(stops.get(position).getLine());
if(stops.get(position).getOneWay())
holder.stopOneWay.setText("P.jednokierunkowy");
else
holder.stopOneWay.setText("");
int line = Integer.parseInt(stops.get(position).getLine());
if(line >= 50){
holder.image.setImageResource(R.drawable.icon);
}
else{
holder.image.setImageResource(R.drawable.icon2);
}
return row;
}
}
FinishActivity:
public class FinishActivity extends Activity {
private ListView lv;
private List<DisplayStop> stops;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_finish);
lv = (ListView) findViewById(R.id.list);
stops = getStops();//create several objects in list
StopAdapter stopAdapter = new StopAdapter(this, R.layout.list_row, stops);
lv.setAdapter(stopAdapter);
}
else
{
Log.d("klop", "0 elementów");
}
}
Ok, I found the problem - it was really STUPID - in StopAdapter I made Mistake in lines:
holder.destination = (TextView) row.findViewById(R.id.destination);
holder.destination = (TextView) row.findViewById(R.id.destinationName);
so text in destination was null. It should be:
holder.destination = (TextView) row.findViewById(R.id.destination)
holder.destinationName = (TextView) row.findViewById(R.id.destinationName);
And everything is good. Thanks for help - I know the simplest errors are the hardest to find :)
Related
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.
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]);
}
I'm using a gridview to present various article items consisting of two textfield. Because of different amount of text the height of each item is different and a row should be sized with the height of the biggest item of a row. The gridview uses colnum = autofit.
As the following screenshot before and after scrolling show, the behaviour is as expected since i scroll down and up again: Rows mess up and cut content of big items.
What am I missing?
Gridview before scrolling:
GridView after scrolling down and up:
Further code:
item xml code
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="14dp"
android:textColor="#040404"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textColor="#343434"
android:textSize="14sp"
android:typeface="serif" />
</LinearLayout>
My ListAdapter (recycles views if possible):
public class SearchResultListViewAdapter extends ArrayAdapter
{
Context context;
Newspaper newspaper;
List<SearchResultListItem> searchResults;
int viewStyle;
public SearchResultListViewAdapter(Context context, int resourceId, List<SearchResultListItem> searchResults, int viewStyle) {
super(context, resourceId, searchResults);
this.context = context;
this.searchResults = searchResults;
this.viewStyle = viewStyle;
}
/* private view holder class */
private class ViewHolder
{
TextView txtTitle;
TextView txtText;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View gridItem;
ViewHolder holder = null;
if (convertView == null)
{
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (this.viewStyle == 0)
{
gridItem = mInflater.inflate(R.layout.searchresult_list_item, null);
} else
{
gridItem = mInflater.inflate(R.layout.searchresult_grid_item, null);
}
holder = new ViewHolder();
} else {
gridItem = convertView;
holder = (ViewHolder) gridItem.getTag();
}
holder.txtText = (TextView) gridItem.findViewById(R.id.text);
holder.txtTitle = (TextView) gridItem.findViewById(R.id.title);
gridItem.setTag(holder);
SearchResultListItem rowItem = getItem(position);
holder.txtText.setText(Html.fromHtml(rowItem.getText()), TextView.BufferType.SPANNABLE);
holder.txtTitle.setText(Html.fromHtml(rowItem.getTitle()), TextView.BufferType.SPANNABLE);
return gridItem;
}
}
And finally my activity layout containing the gridview:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<EditText
android:id="#+id/searchPatternInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/loadSearchResults"
android:imeOptions="actionSearch"
android:inputType="text" />
<ImageButton
android:id="#+id/showExtendedSearch"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_action_filter" />
<ImageButton
android:id="#+id/loadSearchResults"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/showExtendedSearch"
android:src="#drawable/ic_action_search" />
</RelativeLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffffff" >
<ImageSwitcher
android:id="#+id/nothingLoaded"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/ic_nothing_loaded" >
</ImageSwitcher>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" >
<GridView
android:id="#+id/searchResultThumbnailsGridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="160dp"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:gravity="center"
android:horizontalSpacing="4dp"
android:listSelector="#drawable/list_selector"
android:numColumns="auto_fit"
android:padding="10dp"
android:layout_weight="1"
android:stretchMode="columnWidth"
android:verticalSpacing="4dp" />
</LinearLayout>
</RelativeLayout>
</TableRow>
</TableLayout>
You seem to have confusion using View.setTag() and View.getTag() methods. Try this:
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if (convertView == null)
{
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (this.viewStyle == 0)
{
convertView = mInflater.inflate(R.layout.searchresult_list_item, null);
} else
{
convertView = mInflater.inflate(R.layout.searchresult_grid_item, null);
}
holder = new ViewHolder();
//Always use View.findViewById() and View.setTag() here
holder.txtText = (TextView) convertView.findViewById(R.id.text);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
convertView.setTag(holder);
} else {
//Always use View.getTag() here
holder = (ViewHolder) convertView.getTag();
}
//Always set your data here
SearchResultListItem rowItem = getItem(position);
holder.txtText.setText(Html.fromHtml(rowItem.getText()), TextView.BufferType.SPANNABLE);
holder.txtTitle.setText(Html.fromHtml(rowItem.getTitle()), TextView.BufferType.SPANNABLE);
return convertView;
}
I moved on to use a third party component. The StaggeredGridView workes fine for me, more information can be found here.
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.
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...