Use both List View and Recycler View - android

I have a List View which has layout:
list.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.phongponix.trackingbodybuilding.MainActivity$PlaceholderFragment">
<ListView android:id="#+id/lvExerciseList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="50dp"
></ListView>
<View
android:layout_width="match_parent"
android:layout_height="100dp"></View>
</RelativeLayout>
list_item.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="20dp">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imgExcercisePhoto"
android:layout_width="100dp"
android:layout_height="100dp" />
<TextView android:text="aaa"
android:id="#+id/tvExerciseName"
android:layout_width="100dp"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvExcerciseRecords"
android:layout_width="100dp"
android:layout_height="match_parent"
android:orientation="horizontal">
</android.support.v7.widget.RecyclerView>
</TableRow>
</TableLayout>
</LinearLayout>
I setup CustomAdapter for ListView and it works fine.
Now i want setup Adapter for RecyclerView which will be Horizontal orientation. Something like this in my list Adapter
public View getView(int i, View convertView, ViewGroup viewGroup) {
View view = convertView;
if (convertView == null) {
view = inflater.inflate(R.layout.tracking_plan_list_item, null);
viewHolder = new ViewHolder();
viewHolder.title = (TextView) view.findViewById(R.id.tvExerciseName);
view.setTag(viewHolder);
recyclerView = (RecyclerView)view.findViewById(R.id.rvExcerciseRecords);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(trackingPlanHorizontalAdapter);
} else {
viewHolder = (ViewHolder) view.getTag();
}
if (data.size() > 0) {
ExerciseModel exercise = (ExerciseModel) data.get(i);
viewHolder.title.setText(exercise.getTitle());
} else {
viewHolder.title.setText("No data");
}
return view;
}
The problem is it will cause this error:
java.lang.IllegalArgumentException: LayoutManager android.support.v7.widget.LinearLayoutManager#48264ed is already attached to a RecyclerView: android.support.v7.widget
If i commented the line recyclerView.setLayoutManager(linearLayoutManager); My recycler adapter will never execute the onCreateViewHolder or onBindViewHolder. I checked the data for this recycler already and it's not an empty array.
What should i do to have both List View with Vertical Scroll and Recycler View with Horizontal scroll in side a list item??

Change the linearLayoutManager from global to local, like this :
public View getView(int i, View convertView, ViewGroup viewGroup) {
View view = convertView;
if (convertView == null) {
view = inflater.inflate(R.layout.tracking_plan_list_item, null);
viewHolder = new ViewHolder();
viewHolder.title = (TextView) view.findViewById(R.id.tvExerciseName);
view.setTag(viewHolder);
recyclerView = (RecyclerView)view.findViewById(R.id.rvExcerciseRecords);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(trackingPlanHorizontalAdapter);
} else {
viewHolder = (ViewHolder) view.getTag();
}
if (data.size() > 0) {
ExerciseModel exercise = (ExerciseModel) data.get(i);
viewHolder.title.setText(exercise.getTitle());
} else {
viewHolder.title.setText("No data");
}
return view;
}

Related

List not displaying in Fragment

I am attempting to display a listview in a fragment. However, nothing is displaying and the logging seems correct. The screen is completely blank. I thought the cause was an error in inflating the view, but I just have no clue. I have researched SO and other websites with no luck.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.v("FeaturesFragment", "FeaturesFragment");
//View v = inflater.inflate(R.layout.carfeatures_list, container, false);
//View v = inflater.inflate(R.layout.features_list_item, container, false);
View v = inflater.inflate(R.layout.features_list_item, null);
//View v = inflater.inflate(R.layout.carfeatures_list,null);
ListView lv = (ListView) v.findViewById(android.R.id.list);
Log.v("ListView lv1-features", lv.toString());
adapter = new CustomListViewAdapter(carFeaturesList, R.layout.carfeatures_list, super.getActivity());
//adapter = new CustomListViewAdapter(carFeaturesList, R.layout.features_list_item, super.getActivity());
lv.setAdapter(adapter);
Log.v("ListView lv2-features", lv.toString());
adapter.notifyDataSetChanged();
//Listview on item click listener
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Gets values from selected ListItem
String car_features = ((TextView) view.findViewById(R.id.car_features)).getText().toString();
//Log.v("onCreate-car_features", car_features.toString());
String carfeatures_id = ((TextView) view.findViewById(R.id.carfeatures_id)).getText().toString();
//Log.v("onCreate-carfeatures_id", carfeatures_id.toString());
String model_id = ((TextView) view.findViewById(R.id.model_id)).getText().toString();
//Log.v("onCreate-model_id", model_id.toString());
String carfeatures_desc = ((TextView) view.findViewById(R.id.carfeatures_desc)).getText().toString();
//Log.v("onCreate-carfeats_desc", carfeatures_desc.toString());
Boolean b = Boolean.valueOf(TAG_CARID);
if (b == true) {
view.setBackgroundColor(Color.GREEN);
}
//Colors the selected listview item
if (previouslySelectedItem != null) {
previouslySelectedItem.setBackgroundColor(Color.TRANSPARENT);
//Log.v("previouslySelectedItem", "!=null");
//Log.v("position", Integer.toString(position));
}
view.setBackgroundColor(Color.GREEN);
//Log.v("position", Integer.toString(position));
//Log.v("outofif-view", view.toString());
previouslySelectedItem = view;
}
});
//Calls async task to get json
new GetCarFeatures().execute();
Log.v("return called-Features", "return called");
Log.v("v", v.toString());
return v;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
CarFeatures item = getItem(position);
//Log.v("CarFeaturesitem", Integer.toString(position));
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater mInflater = LayoutInflater.from(getContext());
convertView = mInflater.inflate(R.layout.carfeatures_list, null);
//convertView = mInflater.inflate(R.layout.features_list_item, null);
Log.v("getView", "getView");
Log.v("View convertView", convertView.toString());
Log.v("ViewGroup parent", parent.toString());
holder.carfeatures_id = (TextView) convertView.findViewById(R.id.carfeatures_id);
//Log.v("if-holder.carfeat_id", holder.carfeatures_id.toString());
holder.model_id = (TextView) convertView.findViewById(R.id.model_id);
//Log.v("if-holder.model_id", holder.model_id.toString());
holder.carfeatures_desc = (TextView) convertView.findViewById(R.id.carfeatures_desc);
//Log.v("if-holder.carfeat_id", holder.carfeatures_desc.toString());
convertView.setTag(holder);
//Log.v("if-convertView", convertView.toString());
//Log.v("if-holder", holder.toString());
} else {
holder = (ViewHolder) convertView.getTag();
//Log.v("else-convertView", convertView.toString());
//Log.v("else-holder", holder.toString());
}
holder.carfeatures_id.setText(item.carfeatures);
//Log.v("holder.carfeatures_id", holder.carfeatures_id.toString());
holder.model_id.setText(item.modelid);
//Log.v("holder.model_id", holder.model_id.toString());
holder.carfeatures_desc.setText(item.carfeaturesdesc);
//Log.v("holder.carfeatures_desc", holder.carfeatures_desc.toString());
adapter.getItemId(position);
//Log.v("getitemid1", Long.toString(adapter.getItemId(position)));
Log.v("convertView", convertView.toString());
return convertView;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#android:id/list"
android:layout_height="match_parent"
android:layout_width="fill_parent"
android:choiceMode="singleChoice"
android:layout_gravity="center_horizontal|top"
android:layout_alignParentStart="true" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/car_features"
android:layout_below="#+id/model_id"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/carfeatures_id"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/car_features" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/model_id"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/carfeatures_desc"
android:layout_above="#+id/car_features"
android:layout_alignParentStart="true" />
</RelativeLayout>
I might be wrong, but it seems that in your
public View getView(int position, View convertView, ViewGroup parent)
you are trying to inflate this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/car_features"
android:layout_below="#+id/model_id"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/carfeatures_id"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/car_features" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/model_id"
android:layout_below="#+id/header"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/carfeatures_desc"
android:layout_above="#+id/car_features"
android:layout_alignParentStart="true" />
<ListView
android:id="#android:id/list"
android:layout_height="match_parent"
android:layout_width="fill_parent"
android:choiceMode="singleChoice"
android:layout_gravity="center_horizontal|top"
android:layout_alignParentStart="true" />
</RelativeLayout>
which will not work, because you need to inflate an item of the list (not the layout containig the list), an item, which you will fill with your data from array. I would suggest you to create a layout for that item, name it list_item.xml and inflate it.
Oh, and in case you have this item, please show it to us

Show textview on top of GridView for every n Imageviews like Gallery timeline

I am trying to build a layout as shown in the image where the Date(TextView) is shown above the images in GridView and the TextView appears multiple times in view .I am confused on how to implement it. I tried putting TextView in list item but in that case TextView is repeating for each row. Can anyone suggest how to go with this.Reference image attached.
main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/lvTimeline"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rlText">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="28 Feb"
android:layout_marginLeft="16dp"
android:layout_alignParentLeft="true"
android:id="#+id/tvDate"/>
</RelativeLayout>
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/gvTest"
android:numColumns="3"
android:verticalSpacing="3dp">
</GridView>
gridview_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="116dp"
android:layout_height="116dp"
android:scaleType="fitXY"
android:id="#+id/ivTimeline"
android:src="#android:drawable/sym_def_app_icon"/>
List adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.tvDate = (TextView) view.findViewById(R.id.tvDate);
holder.gvTimeline = (GridView) convertView
.findViewById(R.id.gvTest);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.gvTimeline.setAdapter(new GridAdapter(imagesList));
return view;
}
public static class ViewHolder {
TextView tvDate;
GridView gvTimeline;
}
GridView Adapter :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.gridview_item, parent, false);
holder = new ViewHolder();
holder.ivImage = (ImageView) view.findViewById(R.id.ivTimeline);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.ivImage.setImageResource(getItem(position));
return view;
}
public static class ViewHolder {
ImageView ivImage;
}
Image which I am getting now :

Can't click on element in ListView Android

I am trying to create a custom list. My list is contained in a Fragment that correctly implements onScrollListener and populates the list using an adapter. The problem is that I cannot click on each item and I cannot figure out why. Here there is the code of my layout fragment
<?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="match_parent"
android:background="#ffffff"
android:clickable="true">
<ListView
android:id="#+id/listNotification"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:paddingStart="15dp"
android:paddingEnd="15dp"
android:clickable="true"
/>
</LinearLayout>
and here there is the code of my custom list
<?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="match_parent"
android:background="#ffffff"
android:clickable="true">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/imageNotification"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp"
android:focusable="false"/>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#33CC33"
android:focusable="false"/>
<TextView
android:id="#+id/idQuestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:focusable="false"
/>
<TextView
android:id="#+id/typeNotification"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:visibility="gone"
android:focusable="false"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
here there is the code that creates my list using the adapter and setting the onclicklistener
adapter = new NotificationListAdapter(getActivity(), this.rows);
list = (ListView) firstAccessView.findViewById(R.id.listNotification);
list.setAdapter(adapter);
list.setOnScrollListener(this);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(adapter.getItem(position).getTypeNotification()==0) {
mNotificationInteface.readQuestion(adapter.getItem(position).getQuestionId());
}
if(adapter.getItem(position).getTypeNotification()==1){
mNotificationInteface.readAnswer(adapter.getItem(position).getQuestionId());
}
}
});
and here there is the code of my adapter
public class NotificationListAdapter extends ArrayAdapter<NotificationItem> {
private View view;
private final Activity context;
private List<NotificationItem> rows;
private int count = 1;
public NotificationListAdapter(Activity context, List<NotificationItem> firstRows ) {
super(context, R.layout.list_notifications, firstRows);
this.context = context;
this.rows = firstRows;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.list_notifications, null);
view.setPadding(0,10,0,10);
holder = new ViewHolder();
holder.textNotification = (TextView) view.findViewById(R.id.textNotification);
holder.idQuestion = (TextView) view.findViewById(R.id.idQuestion);
holder.typeNotification = (TextView) view.findViewById(R.id.typeNotification);
holder.imageNotification = (ImageView) view.findViewById(R.id.imageNotification);
view.setTag(holder);
} else {
view=convertView;
holder = (ViewHolder) convertView.getTag();
}
int typeNotification = this.rows.get(position).getTypeNotification();
holder.textNotification.setTextColor(Color.BLACK);
holder.idQuestion.setText(String.valueOf(this.rows.get(position).getQuestionId()));
holder.typeNotification.setText(String.valueOf(this.rows.get(position).getTypeNotification()));
if(typeNotification==0){
holder.textNotification.setText(R.string.askQuestion);
holder.imageNotification.setImageResource(R.mipmap.iconuseranonymous);
}
if(typeNotification==1){
//nome da recuperare da con id notifica, quindi id utente quindi dome
holder.textNotification.setText(R.string.answerQuestion);
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").transform(new CircleTransform()).fit().centerCrop().into(holder.imageNotification);
}
if(typeNotification==2){
//nome e immagine da recuperare
holder.textNotification.setText(R.string.newFriend);
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").transform(new CircleTransform()).fit().centerCrop().into(holder.imageNotification);
}
return view;
}
#Override
public NotificationItem getItem(int position){
return this.rows.get(position);
}
#Override
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
static class ViewHolder {
ImageView imageNotification;
TextView textNotification;
TextView idQuestion;
TextView typeNotification;
int position;
}
Remove android:clickable="true" from the ListView and it's parent in your XML layout, and also from the root of your list item layout.

Onclick event for horizontal list item which is present in another listview in android

I have an listView with TextView and another horizontal listView corresponding to that listView. now i need to write onclick event for list item in the horizontal listview.i can't get the parent list item position(vertical listview position).please help me. Thanks in advance.
// First Listview for Vertical
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#null" />
</RelativeLayout>
//Adapter Getview for first list
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_item, null);
TextView title = (TextView) convertView.findViewById(R.id.title);
title.setText(mArrayList.get(position).getSite());
typeId = mArrayList.get(position).getTypeId();
hori_view = (TwoWayView) convertView.findViewById(R.id.hor_list);
mChildList = mArrayList.get(position).getChildArrList();
HorizontalAdapter horListAdapter = new HorizontalAdapter(mContext, R.layout.hori_list,mChildList);
hori_view.setAdapter(horListAdapter);
horListAdapter.notifyDataSetChanged();
hori_view.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
mChildList = mArrayList.get(position).getChildArrList(); // Here i need parent list position
String text = mChildList.get(position).getChannel().toString();
int cId = mChildList.get(position).getChannelId();
int pen = mChildList.get(position).getPending();
int typeId = mChildList.get(position).getSiteid();
Intent intent = new Intent(mContext,ChannelDetails.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Bundle bundle = new Bundle();
bundle.putString("channel", text);
bundle.putInt("typeId", typeId);
intent.putExtras(bundle);
mContext.startActivity(intent);
}
});
return convertView;
}
// Second list for horizontal view
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:clickable="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Title"
android:paddingTop="10dp"
android:paddingLeft="15dp"
android:textColor="#000000"
android:textStyle="bold"/>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="10dp">
<org.lucasr.twowayview.TwoWayView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/hor_list"
style="#style/TwoWayView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
tools:context=".MainActivity" />
</LinearLayout>
</LinearLayout>
// second adapter for second list(Horizontal list)
public View getView(int position, View convertView, ViewGroup parent)
{
View retval = null;
if(retval == null)
{
LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
retval = mInflater.inflate(this.resource, null);
holder = new Holder();
}
holder.image = (ImageView) retval.findViewById(R.id.image);
holder.pending = (TextView) retval.findViewById(R.id.textOne);
holder.channel = (TextView) retval.findViewById(R.id.text1);
holder.chan_id = (TextView) retval.findViewById(R.id.text2);
holder.text3 = (TextView) retval.findViewById(R.id.text3);
mChannel = mChildList.get(position).getChannel();
holder.channel.setText(mChannel);
holder.pending.setText(String.valueOf(mChildList.get(position).getPending()));
holder.chan_id.setText(String.valueOf(mChildList.get(position).getChannelId()));
if(mChildList.get(position).getSiteid() == 100)
{
holder.image.setBackgroundResource(R.drawable.fb_icon1);
holder.text3.setText("facebook");
}
else if(mChildList.get(position).getSiteid() == 200)
{
holder.image.setBackgroundResource(R.drawable.tr_icon1);
holder.text3.setText("twitter");
}
else if(mChildList.get(position).getSiteid() == 300)
{
holder.image.setBackgroundResource(R.drawable.in_icon1);
holder.text3.setText("linkedin");
}
retval.setTag(holder);
return retval;
}
}

Add custom view (ad space) programmatically in ArrayAdapter

I'm trying to add a custom view programmatically into a listview of images in the custom adapter. Currently I'm trying to add it as the third "image". Not that steady on my customer adapter, so not sure how to get it to work. Populating all the images works perfect, but cant get that ad space in between there. Any suggestions?
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.image_row, null);
ViewHolder viewHolder = new ViewHolder();
if(position==3) {
adSpace = new AdSpace(context, "c026dd7e-60a5-46c9-948f-38eb18a", true);
adSpace.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
Log.i("AdSpace","Adding adspace");
} else {
viewHolder.image = (ImageView) rowView.findViewById(R.id.offer_row);
}
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
imageLoader.displayImage(imageURLArray.get(position), holder.image, options);
return rowView;
}
static class ViewHolder {
public ImageView image;
public AdSpace adspace;
}
list.xml:
<?xml version="1.0" encoding="utf-8"?>
<"packagename".ZoomView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/offer_list" />
image_row.xml:
<?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/"packagename""
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/offer_row"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:contentDescription="Image" />
You are creating adSpace but never using it.
if(position==3) {
adSpace = new AdSpace(context, "c026dd7e-60a5-46c9-948f-38eb18a", true);
adSpace.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
Log.i("AdSpace","Adding adspace");
return adSpace;
}
You can remove adSpace from the viewholder.

Categories

Resources