create adapter for two elements in a same row - android

I'm trying to create an adapter for a listview, which contains two elements in a same row.
The LayoutFile got two linear LinearLayouts Layout Image with two images and name
The adapter only fill the first LinearLayout data, and don't show the second LinearLayout.
Someone could help me? Thanks in advance !
XML File:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageViewItem"
android:layout_width="match_parent"
android:layout_height="175dp"
android:src="#drawable/icon" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="left">
<TextView
android:id="#+id/textViewTitleItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Title"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right">
<TextView
android:id="#+id/textViewPriceItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Price"
android:gravity="center" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
It is only a View, I want to duplicate it in the same row.
Below the adapter:
public ItemAdapter(Activity activity, int layoutResourceId)
{
this.activity = activity;
this.layoutResourceId = layoutResourceId;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var row = convertView;
var currentItem = this[position];
TextView txtViewTitle;
TextView txtViewPrice;
ImageView imgViewItem;
if (row == null)
{
var inflater = activity.LayoutInflater;
row = inflater.Inflate(layoutResourceId, parent, false);
txtViewTitle = row.FindViewById<TextView>(Resource.Id.textViewTitleItem);
txtViewPrice = row.FindViewById<TextView>(Resource.Id.textViewPriceItem);
imgViewItem = row.FindViewById<ImageView>(Resource.Id.imageViewItem);
} else
txtViewTitle = row.FindViewById<TextView>(Resource.Id.textViewTitleItem);
txtViewPrice = row.FindViewById<TextView>(Resource.Id.textViewPriceItem);
imgViewItem = row.FindViewById<ImageView>(Resource.Id.imageViewItem);
txtViewTitle.Text = currentItem.Title;
txtViewPrice.Text = Convert.ToString(currentItem.Price);
return row;
}

It seems that you didn't provide orientation to your parent LinearLayout. By default it is horizontal.
That's why its showing only top layout content.
You need to set it as vertical to show the entire content in your list.
android:orientation="vertical"

Related

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

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

List view how to manipulate objects of list adapter item View

adapterListView = new SpecialAdapter(getBaseContext(),list,R.layout.listview_layout,from,to);
headerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.jpos_ror_header, null, false);
footerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.jpos_ror_footer, null, false);
TextView tin=(TextView)headerView.findViewById(R.id.textViewTinValue);
TextView textViewNameOfPayor=(TextView)headerView.findViewById(R.id.textViewNameOfPayor);
TextView textViewLocation=(TextView)headerView.findViewById(R.id.textViewLocation);
Here is my code in getting object id's from my header and footer. how can i get the view of my list view data adapter without onClick event. i want to do some layout manipulation in viewing of my layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<!-- here is my data adapter to be display per item in list view -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/relativeModeOfPaymentCheck"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000" >
<LinearLayout
android:id="#+id/linearForCheck"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="vertical"
android:paddingTop="5dp" >
<TextView
android:id="#+id/textViewModeOfPaymentCheckValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mode of Payment Value check"
android:layout_marginLeft="15dp"
android:textStyle="bold"
android:textColor="#ffffff" />
</LinearLayout>
<!-- asdasda -->
<LinearLayout
android:id="#+id/linearForAmountModeOfPayment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearCheckDate"
android:orientation="horizontal"
android:weightSum="2" >
<RelativeLayout
android:id="#+id/relativeBlock100"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:background="#000000" >
<TextView
android:id="#+id/textViewModAmountCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AMOUNT"
android:textColor="#ffffff" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeBlock101"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#000000" >
<TextView
android:id="#+id/textViewModeOfPayCheckValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#ffffff" />
</RelativeLayout>
</LinearLayout>
<!-- name of check issued -->
<LinearLayout
android:id="#+id/linearForNameCheckIssued"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearForCheck"
android:orientation="horizontal"
android:visibility="gone"
android:weightSum="2" >
<RelativeLayout
android:id="#+id/relativeBlock101"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:background="#000000" >
<TextView
android:id="#+id/textViewBankNameCheckIssued"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RIZAL COMMERCIAL BANKING CORP."
android:textColor="#ffffff" />
</RelativeLayout>
</LinearLayout>
<!-- CHECK NUMBER -->
<LinearLayout
android:id="#+id/linearCheckNumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearForNameCheckIssued"
android:visibility="gone"
android:orientation="horizontal" >
<TextView
android:id="#+id/textViewCheckNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="CHECK # : "
android:textColor="#ffffff" />
<TextView
android:id="#+id/textViewCheckNumValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff" />
</LinearLayout>
<!-- CHECK DATE -->
<LinearLayout
android:id="#+id/linearCheckDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
<!-- android:visibility="gone" -->
android:layout_below="#+id/linearCheckNumber"
android:orientation="horizontal" >
<TextView
android:id="#+id/textViewCheckDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="CHECK DATE : "
android:textColor="#ffffff" />
<TextView
android:id="#+id/textViewCheckDateValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLineForCheck"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearForAmountModeOfPayment"
android:orientation="horizontal" >
<View
android:id="#+id/View201"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
in this adapter i want to manipulate the view of linearlayouts by having LinearLAyout.setVisibility(LinearLayout.GONE or VISIBLE);
Here i think i have some lead from my problem from the answer of himanshu. here is my listview special adapter im having problem how can get the view and viewGroup implementation to my special adapter here is my SpecialAdapter..
public class SpecialAdapter extends SimpleAdapter {
public static TextView tv;
private int[] colors = new int[] { 0xffcccccc , 0xffffffff };
private int[] colors2 = new int[] { 0xffffffff , 0xff000000 };
// Context context;
public SpecialAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
super(context, items, resource, from, to);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
//TextView textView = (TextView)view.findViewById(R.id.textViewModAmountCheck);
//textView.setText("AlvinTest");
return view;
}
Could anyoune tell me i am doing right when im implementing mg getView2.
public View getView2(Context context, int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(context);//get your layout inflator;
//LayoutInflater inf = LayoutInflater.from(getContext());
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.listview_layout, null);
}
LinearLayout yyy = (LinearLayout) convertView.findViewById(R.id.linearCheckDate);
yyy.setVisibility(View.GONE);
return convertView;
}
public boolean setViewValue(View view, Cursor cursor, int columnIndex){
int getIndex = cursor.getColumnIndex("Name");
String empname = cursor.getString(getIndex);
tv = (TextView) view;
tv.setTextColor(Color.WHITE);
tv.setText(empname);
if(empname.equals("Any String"))
{
tv.setTextColor(Color.rgb(58, 58, 224));
return true;
}
return false;
}
}
Here's my code when im calling the getView2 from my listAdapter
LayoutInflater inf = LayoutInflater.from(this);
View v = inf.inflate(R.layout.listview_layout, null);
//add magic here bu i don't know how can i get the view of these View and ViewGroup
View itemConvertView = inf.inflate(R.layout.listview_layout, null);
ViewGroup listParent =null; //this.getParent();//(ViewGroup)SelectorView.this.getParent();// null;
adapterListView.getView2(this,0, itemConvertView, listParent);
try this:
Hide your Layout:
YourLayout.setVisibility(View.GONE);
Visible your Layout :
YourLayout.setVisibility(View.VISIBLE);
If i understood your problem correctly, you want to get the first element of the ListView pro grammatically and change its state (Visibility).
You can get the element in ListView by calling getChildAt(int index) method
e.g
LinearLayout yourFirstView = (LinearLayout) listview.getChildAt(0);
Accessing child of the element that reside in first row of the listview.
ViewType view = (ViewType)yourFirstView.findViewById(R.id.elementId);
setting the visiblity
yourFirstView.setVisiblity(View.Gone); // or View.VISIBLE
What I understand from your question is that there is some view in your ListView item which you want to show only if needed. You can do this in the getView method of your adapter where you inflate the custom layout of list item. Something like below
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = get your layout inflator;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.XXXX, null);
}
LinearLayout yyy = (LinearLayout) convertView.findViewById(R.id.member_name_text_view);
yyy.setVisibility(View.Gone)
return convertView;
}

Scrolling a textView inside a Gridview

thank you for your time.
I have a list of Strings that a display inside a gridview using an ArrayAdapter. Some of those Strings are long and take too much room. I want to limit the height of each grid cell to 1 line and be able to read the full string when i'm clicking on this cell.
Here's my gridview.xml :
<GridView
android:id="#+id/songSelectionGrid_grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/songSelectionGrid_button_back"
android:columnWidth="90sp"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:verticalSpacing="10dp" />
and here is my textview inside my gridview :
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:scrollHorizontally="true"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:maxLines = "1"
android:scrollbars = "horizontal"/>
How can i make the Strings scroll horizontally ?
I've tried a onItemClickListener on the gridview followed by a "setMovementMethod" on the view, but it need to be a TextView and casting it doesn't seem to work
Thanks !
Ok so I've managed to make it work as intended : gridview displaying multiple albums (cover picture + horizontally scrollable text). To do so, I've created my own GridAdapter with the following getView() :
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView = null;
convertView = null;
if(convertView == null)
{
gridView = new View(context);
gridView = inflater.inflate(R.layout.gridtextview, null);
TextView textView = (TextView) gridView.findViewById(R.id.gridView_text);
textView.setText(textArray[position]);
Log.i("GridAdapter","textView.getText :
}
return gridView;
}
The gridView I am populating is composed of the following :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridView_topLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/gridView_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:contentDescription="#string/gridTextView_Album"
android:src="#drawable/ic_launcher" />
<HorizontalScrollView
android:id="#+id/gridView_horizontalScroll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:clipChildren="true"
android:fillViewport="true"
android:scrollbars="none" >
<RelativeLayout
android:id="#+id/gridView_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/gridView_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:isScrollContainer="true"
android:paddingBottom="10sp"
android:singleLine="true" />
</RelativeLayout>
</HorizontalScrollView>
I am not sure this is the best way of doing it but it does work.
But still, keep in mind I'm still a beginner in android dev :)

Center TextViews vertically in RelativeLayout when one or more has visibility set to GONE

I am trying to create a list item layout with 4 items. It is very similar to the example here with some key differences:
the RelativeLayout layout_height may be larger than listPreferredItemHeight
the image is a CheckBox
there are three TextViews
The top and middle TextViews could have visibility set to GONE
What I would like is: If _label is gone, then _message and _definition should be evenly weighted in the layout. If _label and _message are gone, _definition should be centered. I have tried several variations of height, width, gravity, layout_gravity and I even tried to solve this with a LinearLayout (both as the whole layout and just to contain the vertical text). The only way I can get this to work is if I set layout_height to listPreferredItemHeight, but then only two of the text views fit.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight">
<CheckBox android:id="#+id/alarm_list_item_able"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="True"
android:layout_alignParentLeft="True"
android:layout_alignParentBottom="True"
android:layout_centerVertical="True"
android:focusable="false"
android:layout_marginRight="6dp"
android:onClick="onClick"
/>
<TextView android:id="#+id/alarm_list_item_label"
android:text="Label place holder"
android:singleLine="True"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/alarm_list_item_able"
android:layout_alignParentTop="True"
android:gravity="center_vertical"
/>
<TextView android:id="#+id/alarm_list_item_message"
android:text="Message place holder"
android:singleLine="True"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/alarm_list_item_label"
android:layout_toRightOf="#id/alarm_list_item_able"
android:layout_alignWithParentIfMissing="true"
android:gravity="center_vertical"
/>
<TextView android:id="#+id/alarm_list_item_location"
android:singleLine="True"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/alarm_list_item_message"
android:layout_toRightOf="#id/alarm_list_item_able"
android:layout_marginBottom="5dp"
android:layout_alignWithParentIfMissing="true"
android:layout_alignParentRight="true"
android:paddingBottom="5dp"
android:text="(0.000, 0.000) 0m"
android:gravity="center_vertical"
/>
</RelativeLayout>
Adapter getView Code using the xml from blessenm's answer
#Override
public View getView(int position, View convertView, ViewGroup parent){
Log.d(Global.TAG,"Position "+position);
View newView;
if (convertView != null )
newView = convertView;
else {
LayoutInflater layout = LayoutInflater.from(MyActivity.this);
newView = layout.inflate(R.layout.alarm_list_item, null);
}
String tmp1 = "Place Holder Label"
TextView label = (TextView)newView.findViewById(R.id.alarm_list_item_label);
LayoutParams l = label.getLayoutParams();
if (tmp1.matches("^\\s*$")){
label.setLayoutParams(new LinearLayout.LayoutParams(l.width,l.height,0f));
} else {
label.setLayoutParams(new LinearLayout.LayoutParams(l.width,l.height,1f));
}
label.setText(tmp1)
String tmp2 = "Place Holder Message"
TextView message = (TextView)newView.findViewById(R.id.alarm_list_item_message);
l = message.getLayoutParams();
if (tmp2.matches("^\\s*$")){
message.setLayoutParams(new LinearLayout.LayoutParams(l.width,l.height,0f));
} else {
message.setLayoutParams(new LinearLayout.LayoutParams(l.width,l.height,1f));
}
message.setText(tmp2);
TextView location = (TextView)newView.findViewById(R.id.alarm_list_item_location);
location.setText("location");
return newView;
}
Layout_gravity will only work inside a linear layout. When you want all your textviews to take even height, put them in a linearlayout with vertical orientation and give weight as 1 for all the textviews. So now all of them will take equal heights.
When you need to hide the textviews, just set their layout_weight to 0, now the remaining textview will take the full available space. And if you give the textview gravity="center", the text will be centered both vertically and horizontally.
Edit: The xml and getView method
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<CheckBox android:id="#+id/alarm_list_item_able"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:layout_marginRight="6dp"
android:onClick="onClick"
android:layout_centerVertical="true"/>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/alarm_list_item_able"
android:orientation="vertical"
android:layout_centerVertical="true">
<TextView android:id="#+id/alarm_list_item_label"
android:text="Label place holder"
android:singleLine="True"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/alarm_list_item_message"
android:text="Message place holder"
android:singleLine="True"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/alarm_list_item_able"
android:layout_below="#id/alarm_list_item_label"/>
<TextView android:id="#+id/alarm_list_item_location"
android:singleLine="True"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="(0.000, 0.000) 0m"
android:layout_toRightOf="#id/alarm_list_item_able"
android:layout_below="#id/alarm_list_item_message"/>
</LinearLayout>
</RelativeLayout>
The getview method
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
convertView = inflater.inflate(R.layout.list, null);
holder = new ViewHolder();
holder.label = (TextView)convertView.findViewById(
R.id.alarm_list_item_label);
holder.msg = (TextView)convertView.findViewById(
R.id.alarm_list_item_message);
holder.loc = (TextView)convertView.findViewById(
R.id.alarm_list_item_location);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(holder != null){
if(data.get(position).mLabel.equals("")){
LinearLayout.LayoutParams labelLp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,0);
if(!data.get(position).mMsg.equals("")) {
holder.label.setLayoutParams(labelLp);
holder.msg.setText(data.get(position).mMsg);
holder.loc.setText(data.get(position).mLoc);
} else {
holder.label.setLayoutParams(labelLp);
holder.msg.setLayoutParams(labelLp);
holder.loc.setText(data.get(position).mLoc);
}
} else {
holder.label.setText(data.get(position).mLabel);
holder.msg.setText(data.get(position).mMsg);
holder.loc.setText(data.get(position).mLoc);
}
}
return convertView;
}

problem with custom ListView showing 3 vertical TextViews

im trying to display 3 TextViews as a custom row design in a ListView. Unfortunatly, only 2 TextViews ( row_title and row_postcode_city) are visible...
Maybe somebody has an idea?
Regards,
float
Code of my ArrayAdapter:
private class PartnerAdapter extends ArrayAdapter<Partner> {
private ArrayList<Partner> items;
public PartnerAdapter(Context context, int textViewResourceId, ArrayList<Partner> items) {
super(context, textViewResourceId, items);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.partner_suche_ergebnis_row, parent, false);
}
Partner o = items.get(position);
if (o != null) {
TextView title = (TextView) v.findViewById(R.id.row_title);
TextView subtitle = (TextView) v.findViewById(R.id.row_subtitle);
TextView postcode_city = (TextView) v.findViewById(R.id.row_postcode_city);
if (title != null) {
title.setText(o.getTitle());
}
if(subtitle != null){
subtitle.setText(o.getSubtitle());
}
if(postcode_city != null){
subtitle.setText(o.getPostcode() + " " + o.getCity());
}
}
return v;
}
}
XML of the custom row design:
<?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="?android:attr/listPreferredItemHeight"
android:padding="0dip"><!--
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="#drawable/icon" />
--><LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="#+id/row_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="bold"/>
<TextView
android:id="#+id/row_subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/row_postcode_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
Try to use the following xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="0dip">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="#+id/row_title"
android:text="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="bold"/>
<TextView
android:id="#+id/row_subtitle"
android:text="2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:text="3"
android:id="#+id/row_postcode_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
Check it now.
Change your LinearLayout to "wrap_content" on layout_height
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content">
Also, I don't know why you have a LinearLayout inside a LinearLayout. Since this xml is only for individual rows. Each row will be a horizontal LinearLayout, the second one being unnecessary as ListView automatically lists rows vertically.
Finally, the root LinearLayout should use minHeight for the default Item Height, not layout_height.
android:minHeight="?android:attr/listPreferredItemHeight"
This should be your xml file (unless you really need the second LinearLayout for some odd reason)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="0dip">
<TextView
android:id="#+id/row_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="bold"/>
<TextView
android:id="#+id/row_subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/row_postcode_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Sorry for offtopic, but I here some useful tips from me
You have a LOT of needless (and flat out wrong) stuff going on here:
Partner o = items.get(position);
if (o != null) {
You should get something for every position, if item retuns null that means you are doing something wrong. In your case, if you get null you just return reused old, or brand new row, which will confuse the user by displaying the old data, or no data at all.
if (title != null) {
You have 2 ways of getting View object of your row, by inflating, or getting it from recycler (convertView). And recycler will ALLWAYS return the right type of View, so those check are useless since your components will always be found.

Categories

Resources