I ran into a problem with a RecyclerView and I thought you guys can help me.
So the problem is:
Inflating the item view xml takes too much time.
The layout is grid layout with 3 items in a row and each item has a complex UI look.
So first I've used android studio's profiler to analyse it and I saw that the CPU has a significant bump at the point of creation. Even if I remove the entire bind code, It still takes some time when I move into this screen/ scrolling inside the recycler view.
Going into separating the items to smaller items will be crazy because it's already a matrix (3 items in a row as I said).
So I thought about 2 things:
maybe I can create more view holders in advance, any idea how to do that? I saw methods like RecyclerView.setItemViewCacheSize and LayoutManager.setInitialPrefetchItemCount but no luck making it happens (it is still calling on create a lot of times)
What about AsyncLayoutInflater? does someone uses it in a RecyclerView?
Any new idea will be great!
This is how the view xml looks like:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="3dp"
card_view:cardElevation="2.5dp"
card_view:cardUseCompatPadding="true">
<CustomView1>
</CustomView1>
<CustomView2>
</CustomView2>
<CustomView3>
</CustomView3>
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_gravity="end"
android:layout_marginEnd="2dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:adjustViewBounds="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<include layout="#layout/someLayoutToInclude" />
</android.support.v7.widget.CardView>
</FrameLayout>
OnCreateViewHolder looks like:
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(viewType, parent, false);
return new ItemViewHolder(view);
Grid Layout initialization:
final GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3);
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int position) {
return 1;
}
});
Thanks
Here is the solution I did at the end:
As I mentioned, my issue was that onCreateViewHolder, creates a complex view with a huge hierarchy that cannot be changed (product-wise), and I had 3 items in a row(GridLayoutManager)
Because of that, I had 2 things that worked bad:
Scrolling fast/ flinging created new items while scrolling and the UI freezed for few milliseconds.
The animation between the previous screen and the screen with the RecyclerView
looked bad and leggy.
So what I did is:
As I mentioned above , there's an AsyncLayoutInflater.
In order to use it, I needed to inflate a dummy ViewGroup, and after that to add the async inflated view to this dummy view.
Of course some extra logic should be done for supporting onBindViewHolder.
public ItemViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.async_holder_item, parent, false);
AsyncLayoutInflater asyncLayoutInflater = new AsyncLayoutInflater(parent.getContext());
asyncLayoutInflater.inflate(viewType, (ViewGroup) view, new AsyncLayoutInflater.OnInflateFinishedListener() {
#Override
public void onInflateFinished(#NonNull View view, int resid, #Nullable ViewGroup parent) {
parent.addView(view);
}
});
return new ItemViewHolder(view, viewType);
}
You can also use a shimmer or loading state at the dummy view (keep the layout simple!) and add android:animateLayoutChanges="true" to the top view group to make it smoother
Related
FrameLayout for some reason doesn't work perfectly in listview. I got this incorrect displaying in emulator. In Android Studio items looks nicely. Please help me :)
In Android Studio
In emulator
item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:padding="10dp">
<TextView
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:layout_gravity="center"
android:textSize="27sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/txtGenre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Genre"
android:layout_gravity="bottom|right"
android:textSize="18sp"
android:textStyle="italic"/>
<TextView
android:id="#+id/txtId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID"
android:textSize="18sp"
android:textStyle="italic"
android:layout_gravity="bottom|left"/>
</FrameLayout>
Adapter code
public class MyFilmListAdapter extends ArrayAdapter<Film> {
Context context;
ArrayList<Film> films;
public MyFilmListAdapter(#NonNull Context context, ArrayList<Film> films) {
super(context, R.layout.item_film, films);
this.context = context;
this.films = films;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
Film f = films.get(position);
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.item_film, null, false);
TextView name = v.findViewById(R.id.txtName);
TextView genre = v.findViewById(R.id.txtGenre);
TextView id = v.findViewById(R.id.txtId);
name.setText(f.getName() + "");
genre.setText(f.getGenre() + "");
id.setText(f.getId() + "");
return v;
}
}
I tried to use LinerLayout and it has correct displaying, however I need FrameLayout.
According to the official documentation
FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organise child views in a way that's scalable to different screen sizes without the children overlapping each other.
You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.
So you can use LinearLayout inside it.
and nowadays better is you should use Recyclerview with ConstraintLayout
My issue:
In my xml file, I define android:visibility="gone" in the linear layout labelled as assess_layout_list. Then, in the onClick() of course_adapter_layout, the whole view, I set the visibility back to View.VISIBLE, which does not work, even though the Log call just before it works, the LinearLayout object called assess_list_layout is not null, and it does work when I define the visibility="invisible" in the xml file. I want it to be gone at first though, and visible after clicking as this fits the design of the app.
Here is my course_adapter_view.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/course_adapter_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="left"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginTop="20dp"
android:padding="15dp"
android:elevation="2dp"
android:background="#drawable/course_header_background">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/course_color_circle"/>
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"/>
<TextView
android:id="#+id/course_adapter_course_code"
android:text="TextView1"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_weight="0.5"/>
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"/>
<TextView
android:id="#+id/course_adapter_course_title"
android:text="TextView2"
android:layout_width="wrap_content"
android:layout_height="20dp"/>
</LinearLayout>
<LinearLayout
android:id="#+id/assess_list_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginRight="40dp"
android:layout_marginLeft="40dp"
android:background="#drawable/course_body_background"
android:padding="20dp"
android:visibility="gone"
>
<ListView
android:id="#+id/course_adapter_assess_list"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginBottom="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="More" />
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="New"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Here is my CourseListAdapter.java file that I use to create each view for each course in the list of courses, minus the usual stuff:
package com.example.schoolplanner2.adapters;
public class CourseListAdapter extends ArrayAdapter<Course> {
private static final String TAG = "CourseListAdapter";
private Context context;
int mResource;
public CourseListAdapter(#NonNull Context context, int resource, #NonNull ArrayList<Course> objects) {
super(context, resource, objects);
this.context = context;
mResource = resource;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
// get info
String course_code = getItem(position).getCourseCode();
Double course_grade = getItem(position).getCurrentGrade();
// make inflater and inflate the layout
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(mResource, parent, false);
TextView tv_course_code = v.findViewById(R.id.course_adapter_course_code);
TextView tv_course_title = v.findViewById(R.id.course_adapter_course_title);
tv_course_code.setText(course_code);
tv_course_title.setText(String.valueOf(course_grade));
// add on click to each list view element
LinearLayout layout = v.findViewById(R.id.course_adapter_layout);
layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.i(TAG, "List view element has been clicked " + course_code);
// expand the view to include a new fragment
LinearLayout assess_list_layout = view.findViewById(R.id.assess_list_layout);
assess_list_layout.setVisibility(View.VISIBLE);
// get the list view and add each course to the course view
ListView assessment_list_view = (ListView) view.findViewById(R.id.course_adapter_assess_list);
AssessmentListAdapter assessAdapter = new AssessmentListAdapter(getContext(), R.layout.assessment_adapter_view, getItem(position).getAssessmentList(), getItem(position));
assessment_list_view.setAdapter(assessAdapter);
}
});
return v;
}
}
Please let me know if there is any more information you need. Will also take suggestions on other ways of accomplishing the same thing. Thanks for your help.
~Seth.
Edit: when the assess_list_layout.setVisibility(View.VISIBLE) is outside of the onClick it does work.
Further Edit: Things I have tried so far to no avail:
moving the location of where I define the LinearLayout componenent
calling invalidate() on parent view
using runOnUiThread()
changing view to v in the line where I attempt to findViewById for assess_list_layout, they are the same thing so it does not help.
calling requestLayout() on assess_list_layout
Update: I have now managed to get the assess_list_layout section to appear when the course_adapter_layout is clicked on. The only problem now is that the view does not take up anymore space on the screen, it just turns into a scrollable view that can be scrolled up and down on to see the whole view.
Also, when I scroll to fast, it resets the view back to the way it was on bootup.
1.View Visibility not working
The Visibility is not working because the view is not rendered initially. Remove the visibility gone in the xml and handle the visibility fully in the adapter class. In the 'assess_list_layout' linerlayout height can be hardcode because inside this layout the listview height is already hardcoded. You can hardcode to 300 and check. This way will help the view to get the initial rendering.
2. Scroll issue
While scrolling the already visible 'assess_list_layout' view might be not visible. This is because we need to handle the visibility, this handling is similar to checkbox selection handling in listview. Hope the Course class is model class, in that add a another property named isSelected as boolean and set the default value to false. Please refer below the course class,
Course Class
public class Course {
private boolean isSelected = false;
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
}
please refer the below code changes in the adapter class.
public class CourseListAdapter extends ArrayAdapter<Course> {
private static final String TAG = "CourseListAdapter";
private Context context;
int mResource;
public CourseListAdapter(#NonNull Context context, int resource, #NonNull ArrayList<Course> objects) {
super(context, resource, objects);
this.context = context;
mResource = resource;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
// get info
String course_code = getItem(position).getCourseCode();
Double course_grade = getItem(position).getCurrentGrade();
// make inflater and inflate the layout
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(mResource, parent, false);
TextView tv_course_code = v.findViewById(R.id.course_adapter_course_code);
TextView tv_course_title = v.findViewById(R.id.course_adapter_course_title);
//My Change
// expand the view to include a new fragment
LinearLayout assess_list_layout = view.findViewById(R.id.assess_list_layout);
// get the list view and add each course to the course view
ListView assessment_list_view = (ListView) view.findViewById(R.id.course_adapter_assess_list);
assess_list_layout.setVisibility(View.GONE);
if (getItem().get(position).isSelected()) {
assess_list_layout.setVisibility(View.Visible);
AssessmentListAdapter assessAdapter = new AssessmentListAdapter(getContext(), R.layout.assessment_adapter_view, getItem(position).getAssessmentList(), getItem(position));
assessment_list_view.setAdapter(assessAdapter);
}
//My Change
tv_course_code.setText(course_code);
tv_course_title.setText(String.valueOf(course_grade));
// add on click to each list view element
LinearLayout layout = v.findViewById(R.id.course_adapter_layout);
layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.i(TAG, "List view element has been clicked " + course_code);
//My Change
getItem().get(position).setSelected(true);
//My Change
assess_list_layout.setVisibility(View.VISIBLE);
AssessmentListAdapter assessAdapter = new AssessmentListAdapter(getContext(), R.layout.assessment_adapter_view, getItem(position).getAssessmentList(), getItem(position));
assessment_list_view.setAdapter(assessAdapter);
}
});
return v;
}
}
I have commented as My Change to find the difference in the code.
issue 1
// make inflater and inflate the layout
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(mResource, parent, false);
you don't reuse view,which leads to inflate a new view every time when called getView();As inflate() method is IO sensitive,it slow down the smoothness of scrolling ,trigger jank。
try this
// make inflater and inflate the layout
View v = null;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
v= inflater.inflate(mResource, parent, false);
} else {
v = convertView;
}
issue 2
when scorll listview ,you need reset itemview state,add a "expand" property to your Course bean ,when click item set expand = true;and then add flowing code above layout.setOnClickListener
v.findViewById(R.id.assess_list_layout).setVisibility( item.expand ? View.VISIBLE:View.GONE);
ListView assessment_list_view = (ListView) v.findViewById(R.id.course_adapter_assess_list);
if (item.expand) {
AssessmentListAdapter assessAdapter = new AssessmentListAdapter(getContext(), R.layout.assessment_adapter_view,
item.getAssessmentList(), item);
assessment_list_view.setAdapter(assessAdapter);
}
issue 3
set setOnClickListener in getView() method , will create a new Clicker instance every time getView() called. use listView.setOnItemClickListener() instead
tips:
after all,you should use RecyclerView instead of ListView,which is a powerful UI widget
I'm trying to have these two colors for the ListGroup items in my list: #E1E1E1 and #C2C2C2. The first for the even items and the other for the odd ones. It's 'partially' working. When the activity that opens the list is opened, I get the correct result but ONLY FOR THE VISIBLE PART of the list. If I scroll down, the backgrounds are all of the default color. If I scroll down and flip my phone into landscape mode, the activity gets created again and, again, I get the correct result only for the visible part of the list. I hope someone can help me a bit :)
EDIT:
what you see as white in the image is gray15p. The darker is gray30p which is the default in the XML. android:background="#color/gray30p". If I use, if-else it does work, but I can't understand why the gray15p (looks like white) is not being applied using the IF alone.
Here's how it looks:
In my expandable List Adapter:
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
...
...
//set background color in odd items
if(groupPosition%2==0){
LinearLayout groupLayout=(LinearLayout)convertView.findViewById(R.id.groupLinearLayout);
groupLayout.setBackgroundResource(R.color.gray15p);
}
return convertView;
and the layout file list_group.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/groupLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray30p"
android:orientation="horizontal" >
<!-- android:padding="8dp"
android:background="#000000"> -->
<ImageView
android:id="#+id/lblListHeaderImage"
android:layout_width="90dp"
android:layout_height="90dp"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:paddingTop="1dp"
android:paddingBottom="1dp"
android:src="#drawable/defaultimg"
android:scaleType="fitCenter"
android:contentDescription="#string/app_name"/>
<!--level 1 of expandable list -->
<TextView
android:id="#+id/lblListHeader"
android:layout_width="wrap_content"
android:layout_height="90dp"
android:paddingLeft="10dp"
android:textSize="22sp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:gravity="center"
android:textColor="#000000" />
</LinearLayout>
I think whats missing is to set the color for the even items. The Problem is that you adapter loads the layout with the white background and changes it for the odd items to grey, but when you scroll down, the views gets reused. What happen is that your views background is already set to grey.
just adjust your code to something like that
//set background color in odd items
LinearLayout groupLayout=(LinearLayout)convertView.findViewById(R.id.groupLinearLayout)
if(groupPosition%2==0){
groupLayout.setBackgroundResource(R.color.gray15p);
} else {
groupLayout.setBackgroundResource(R.color.gray30p); // <-- important part.
}
When developing an android application in java, I have a mainActivity which sets up a list adapter in the following way:
private void refreshDisplay()
{
adapter = new ItemAdapter(this, R.layout.listview_item_row, myList);
setListAdapter(adapter);
}
This method refreshDisplay is called when data making up the list is changed via user input and the mainactivity is returned to after completing the edit activity.
The list itself is composed of a ListView inside the MainActivity.xml file, which loads a list_item_row_layout thus:
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
and inside the list_item_row.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/TitleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"/>
<TextView
android:id="#+id/DetailText"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:clickable="true" />
</RelativeLayout>
Inside the list adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
myHolder holder;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new itemHolder();
holder.titleText = (TextView)row.findViewById(R.id.TitleText);
holder.textDetail = (TextView)row.findViewById(R.id.DetailText);
row.setTag(holder);
}
else
{
holder = (myHolder)row.getTag();
}
myItem = data.get(position);
holder.titleText.setText(myItem.getText());
holder.textDetail.setText(myItem.getDetail());
if (holder.textDetail.getVisibility()==0) {
holder.titleText.setVisibility(8);
holder.textDetail.setVisibility(0);
}
else if (holder.titleText.getVisibility()>0) {
holder.textDetail.setVisibility(8);
holder.titleText.setVisibility(0);
}
else {
holder.titleText.setVisibility(0);
holder.textDetail.setVisibility(8);
}
return row;
}
When the list is generated it displays without problem, and even runs correctly as far as accepting the first click which is supposed to expand the TitleText into the more verbose DetailText. So the DetailText starts with a Visibility of GONE and the TitleText is set to VISIBLE. If the user clicks on the listitem then it does successfully switch to the DetailText field. But the problem arises when the same item is clicked on again - the TitleText field does not replace the detailed version. I did initially think the DetailText was not clickable - but what puzzles me now is that if one of the other list items are clicked on then the original expanded one reverts back to the title and the clicked on one expands correctly! But how can I make it so that the expanded list item can be contracted into the title text without expanding another list item? Is there some problem that means a list item that is set to VISIBLE in code cannot be then set back to GONE or INVISIBLE even?
Had to use a bit of a hack in the end since was nearing the end of my usable hair to tear out haha.
Since reverting back to all todos with titles displayed was also just the way things appeared when the activity was first created - I put the following code in where the second click happened.
There is a bit of a pause though before this diplays on my HTC One, it remains to be seen when I test it on slower phones if its an acceptable solution.
Intent intentstart = new Intent(MainActivity.this, MainActivity.class);
intentstart.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentstart);
I am populating a Listview (list.xml) with a textview (list_row.xml).
I need to add an additional textview to list_row.xml, which means I need to wrap them in a layout. However, I get a Textview ID error when I do this.
list_row.xml I need:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/nameTV"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#FFFF00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Main.xml
public View GetView(int inPosition, View inConvertView, ViewGroup inParent)
{
View _row = inConvertView;
if (_row == null)
{
// Inflate Row
Log.d(TAG, "Starting XML Row Inflation ... ");
LayoutInflater inflater = (LayoutInflater)
cContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
_row = inflater.inflate(R.layout.list_row, inParent, false);
Log.d(TAG, "Successfully completed XML Row Inflation!");
}
// Get item
PropertiesAd _adProperty = GetItem(inPosition);
// Get reference to TextView - country_name
cAdName = (TextView) _row.findViewById(R.id.nameTV);
//Set name
cAdName.setText(_adProperty.toString());
return _row;
}
For the sake of testing, I haven't added the second textview yet. I'm just trying to get this to work with the layout wrapper.
Everything works fine without the LinearLayout. But again, I will need it to add a second textview. Suggestions?
Thanks
ArrayAdapter:
public class ArrayAdapterAd extends ArrayAdapter<PropertiesAd>
{
private static final String TAG = "AdArrayAdapter";
private Context cContext;
private TextView cAdName;
private List<PropertiesAd> cAdList = new ArrayList<PropertiesAd>();
public ArrayAdapterAd(Context inContext, int inTextViewResourceId, List<PropertiesAd> inObjects)
{
super(inContext, inTextViewResourceId, inObjects);
this.cContext = inContext;
this.cAdList = inObjects;
}
public int GetCount()
{
return this.cAdList.size();
}
public PropertiesAd GetItem(int inIndex)
{
return this.cAdList.get(inIndex);
}
public View GetView(int inPosition, View inConvertView, ViewGroup inParent)
{
View _row = inConvertView;
if (_row == null)
{
// Inflate Row
Log.d(TAG, "Starting XML Row Inflation ... ");
LayoutInflater inflater = (LayoutInflater)
cContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
_row = inflater.inflate(R.layout.ad_list, inParent, false);
Log.d(TAG, "Successfully completed XML Row Inflation!");
}
// Get item
PropertiesAd _adProperty = GetItem(inPosition);
// Get reference to TextView - country_name
cAdName = (TextView) _row.findViewById(R.id.ad_listTextView);
//Set country name
cAdName.setText(_adProperty.toString());
return _row;
}
}
Now I see your problem... you cannot do that extending the ArrayAdapter class. Instead, extend BaseAdapter.
Here is xml for a listview "list_row" with 2 textviews
<?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">
<TextView android:id="#+id/text1" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="#+id/text2" android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
I'm assuming the getView() is from an ArrayAdapter subclass? If so, what does your constructor look like? Perhaps you're passing in an id reference in stead of a layout reference? This will probably resolve fine if you're whole layout is nothing more than a single TextView, but could explain why an error occurs when wrapping it in a layout.
Also, don't forget to implement the rowwrapper/viewholder pattern to improve performance and recycle views. For an example, have a look here.
By the way, you may even suffice with one of the Android built-in layouts. There a two line list item layout defined in android.R.layout.simple_list_item_2. It may not suit your needs though, as the styling on the two elements is different. A custom layout is always the most flexible in the end. :)