RecyclerView OnClicklistener is unresponsive - android

Firstly I know there are hundreds of these questions but the answers have not solved my problem. I suspect it's something small I'm missing, basically I've added a RecyclerView and need to start a new activity when a card is clicked.
I've been following this tutorial and it's been smooth sailing up until this point.
https://guides.codepath.com/android/using-the-recyclerview
This is the Recycler
<FrameLayout 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"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/bgImg"
android:scaleType="centerCrop"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/RV"
/>
</FrameLayout>
This is the card
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="4dp"
android:layout_margin="5dp"
android:clickable="true"
android:background="?android:attr/selectableItemBackground">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/vendor_image2"
android:layout_width="fill_parent"
android:layout_height="240dp"
android:padding="5dp" />
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/vendor_image2"
>
<TextView
android:id="#+id/vendor_name2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:paddingBottom="5dp"/>
<TextView
android:id="#+id/vendor_content2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="7dp"
android:layout_toEndOf="#+id/vendor_name2"
android:textColor="#color/text_col"
android:paddingBottom="5dp" />
<TextView
android:id="#+id/vendor_suburb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="italic"
android:textColor="#color/text_col"
android:layout_below="#+id/vendor_name2"
android:layout_marginLeft="10dp" />
<RatingBar
android:id="#+id/MyRating2"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/vendor_suburb2"
android:layout_marginLeft="10dp"
android:isIndicator="true"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:numStars="5"
android:textColor="#android:color/black"
android:rating="3.5"
android:stepSize="0.1" />
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
And finally the Adapter and Handler
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import com.iconiccode.android.guestloc8tor.SQL.DatabaseHandler;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
public class VendorRecycleAdapter extends RecyclerView.Adapter<VendorRecycleAdapter.VendorHolder>
{
public List<Vendor> vendorList;
private DatabaseHandler dbHandler = new DatabaseHandler(MyApp.getContext());
private Vendor currentVendor;
int cate;
Activity thiscontext;
public VendorRecycleAdapter(Activity context, int Cate)
{
this.cate=Cate;
this.thiscontext=context;
}
#Override
public VendorHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
LayoutInflater inflater = LayoutInflater.from(thiscontext);
//View view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_2, parent, false);
View view = inflater.inflate(R.layout.vendor_list_cards,parent,false);
return new VendorHolder(view);
}
#Override
public void onBindViewHolder(VendorHolder holder, int position)
{
vendorList = dbHandler.getVendorData(cate);
currentVendor = vendorList.get(position);
// Get the image string from the Vendor object
String vendorImage = currentVendor.getImage();
Log.e("Vendor Adapter Image", vendorImage);
// Get the name string from the Vendor object
String vendorName = currentVendor.getCompanyName();
Log.e("Vendor Adapter Name", vendorName);
// Get the content string from the Vendor object
String vendorContent = currentVendor.getContent();
// Get the location string from the Vendor object
String vendorSuburb = currentVendor.getSuburb();
Log.e("Vendor Adapter Suburb", vendorSuburb);
// Find the TextView with view ID vendor_name
// Display the name of the current vendor in that TextView
holder.Vvendor_name.setText(vendorName);
holder.Vvendor_image.setImageResource(R.drawable.loading);
ImageLoader imageLoader;
DisplayImageOptions options;
imageLoader = ImageLoader.getInstance();
String url = vendorImage;
imageLoader.init(ImageLoaderConfiguration.createDefault(MyApp.getContext()));
options = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.loading)
.showImageOnFail(R.drawable.b4f29385)
.resetViewBeforeLoading(true).cacheOnDisk(true)
.imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565).considerExifParams(true)
.displayer(new FadeInBitmapDisplayer(300)).build();
imageLoader.displayImage(url, holder.Vvendor_image);
// Find the TextView with view ID vendor_content
holder.Vvendor_content.setText(vendorContent);
holder.Vvendor_suburb.setText(vendorSuburb);
}
#Override
public int getItemCount()
{
vendorList = dbHandler.getVendorData(cate);
return vendorList.size();
}
public class VendorHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
public ImageView Vvendor_image;
public TextView Vvendor_name,Vvendor_content,Vvendor_suburb;
public RatingBar Vrating;
public VendorHolder(View itemView)
{
//These pull as null
super(itemView);
Vvendor_image = (ImageView)itemView.findViewById(R.id.vendor_image2);
Vvendor_name = (TextView)itemView.findViewById(R.id.vendor_name2);
Vvendor_content =(TextView)itemView.findViewById(R.id.vendor_content2);
Vvendor_suburb = (TextView)itemView.findViewById(R.id.vendor_suburb2);
Vrating = (RatingBar) itemView.findViewById(R.id.MyRating2);
}
#Override
public void onClick(View v)
{
// int position = getAdapterPosition();
// currentVendor = vendorList.get(position);
Intent intent = new Intent(thiscontext,VendorDetailsActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
String Category = Integer.toString(cate);
JSONObject message = currentVendor.getAllData();
try {
intent.putExtra("IMAGE", message.getString("IMAGE"));
intent.putExtra("URL", message.getString("URL"));
intent.putExtra("CONTENT", message.getString("CONTENT"));
intent.putExtra("COMPANY_NAME", message.getString("COMPANY_NAME"));
intent.putExtra("TEL", message.getString("TEL"));
intent.putExtra("BOOKING_URL", message.getString("BOOKING_URL"));
intent.putExtra("LAT", message.getString("LAT"));
intent.putExtra("LON", message.getString("LON"));
intent.putExtra("STREET", message.getString("STREET"));
intent.putExtra("SUBURB", message.getString("SUBURB"));
intent.putExtra("PROVINCE", message.getString("PROVINCE"));
intent.putExtra("CITY", message.getString("CITY"));
intent.putExtra("CAT",Category);
} catch (JSONException json)
{
Log.e("ERROR", json.getMessage());
}
thiscontext.startActivity(intent);
}
}
}
Any help would be greatly appreciated.
Thanks

I think you just forgot to set the onClickListener in your ViewHolder constructor:
public VendorHolder(View itemView)
{
//These pull as null
super(itemView);
Vvendor_image = (ImageView)itemView.findViewById(R.id.vendor_image2);
Vvendor_name = (TextView)itemView.findViewById(R.id.vendor_name2);
Vvendor_content =(TextView)itemView.findViewById(R.id.vendor_content2);
Vvendor_suburb = (TextView)itemView.findViewById(R.id.vendor_suburb2);
Vrating = (RatingBar) itemView.findViewById(R.id.MyRating2);
// If it should be triggered only when clicking on a specific view, replace itemView with the view you want.
itemView.setOnClickListener(this);
}

Related

Having issue with designing comment section

I am trying to make Popup Dialog Comment Section.
In this Comment section i done almost all things but facing new problem.
Here i am using Dialog for Show and add new comment but when user click on Enter a Comment (Edittext). It gets hide under Keyboard. So it will be problematic for user to post new comment.
Here My Xml File :
Popup.xml:
<?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="wrap_content"
android:background="#drawable/dialog_bg"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/popup_rcv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom">
<EditText
android:id="#+id/new_comment_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Please enter Comment" />
<Button
android:id="#+id/comment_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Thank you in Advance.
Try this
dialog_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="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/popup_rcv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal">
<EditText
android:id="#+id/new_comment_et"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Please enter Comment" />
<Button
android:id="#+id/comment_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
</LinearLayout>
Activity
import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
public class HomeActivity extends AppCompatActivity {
RecyclerView additionalDataList;
ArrayList<String> arrayList = new ArrayList<>();
DataAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_layout);
Window window = dialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
dialog.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
EditText edtComment = dialog.findViewById(R.id.new_comment_et);
Button btnComment = dialog.findViewById(R.id.comment_btn);
additionalDataList = dialog.findViewById(R.id.popup_rcv);
additionalDataList.setLayoutManager(new LinearLayoutManager(this));
additionalDataList.setHasFixedSize(true);
addDataToList();
btnComment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!TextUtils.isEmpty(edtComment.getText().toString().trim())) {
arrayList.add(0, edtComment.getText().toString().trim());
edtComment.setText("");
adapter.notifyDataSetChanged();
}
}
});
adapter = new DataAdapter(this, arrayList);
additionalDataList.setAdapter(adapter);
dialog.show();
}
private void addDataToList() {
for (int i = 0; i < 5; i++) {
arrayList.add("Comment :" + i);
}
}
}
DataAdapter
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
Context context;
ArrayList<String> arrayList = new ArrayList<>();
public DataAdapter(Context context, ArrayList<String> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
#Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.test, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.title.setText(arrayList.get(position));
}
#Override
public int getItemCount() {
return arrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView title;
public ViewHolder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.additionalInfoTitle);
}
}
}
OUTPUT

How to fix OnItemClickListener on ListView

This is java class MainActivity.java
package com.example.mhn.intercoapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import com.example.mhn.intercoapp.Adapters.EmployeeDirAdapter;
import com.example.mhn.intercoapp.static_class.EmployeeDir;
import java.util.ArrayList;
public class EmployeeDirectoryActivity extends AppCompatActivity {
ListView listView;
ImageView back_button;
EmployeeDir obj;
ArrayList <EmployeeDir> empDir ;
EmployeeDirAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_employee_directory);
listView = (ListView) findViewById(R.id.listView_emp_directory_xml);
back_button = (ImageView) findViewById(R.id.back_button_header_emp_directory_activity_xml);
obj = new EmployeeDir();
empDir = new ArrayList<>();
adapter = new EmployeeDirAdapter(getApplicationContext(),empDir);
back_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
finish();
}
});
obj.setEmp_name("Hafiz Sadique Umar");
obj.setEmp_email("hsu#gmail.com");
obj.setEmp_contact_num("+923045607057");
empDir.add(obj);
empDir.add(obj);
empDir.add(obj);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
String value = (String)adapter.getItemAtPosition(position);
Intent intent= new Intent(getApplicationContext(),DoneActivity.class);
startActivity(intent);
// assuming string and if you want to get the value on click of list item
// do what you intend to do on click of listview row
}
});
}
}
This is activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".EmployeeDirectoryActivity">
<RelativeLayout
android:id="#+id/relatice_layout_header_emp_directory_activity_xml"
android:background="#color/colorAccent"
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:layout_height="56dp">
<ImageView
android:id="#+id/back_button_header_emp_directory_activity_xml"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:src="#drawable/back_arrow_header"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Employee Directory"
android:textColor="#fff"
android:textSize="18dp"
android:textStyle="bold"
android:layout_centerInParent="true"
/>
</RelativeLayout>
<ListView
android:clickable="true"
android:id="#+id/listView_emp_directory_xml"
android:layout_below="#id/relatice_layout_header_emp_directory_activity_xml"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
This is Adapter
package com.example.mhn.intercoapp.Adapters;
import android.content.Context;
import android.content.Intent;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.mhn.intercoapp.MainActivity;
import com.example.mhn.intercoapp.R;
import com.example.mhn.intercoapp.static_class.EmployeeDir;
import java.util.ArrayList;
public class EmployeeDirAdapter extends BaseAdapter {
Context context;
private LayoutInflater inflater;
private ArrayList<EmployeeDir> menuData=new ArrayList<>();
public EmployeeDirAdapter(Context context, ArrayList<EmployeeDir>EmpDir)
{
this.context = context;
this.menuData=EmpDir;
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return menuData.size();
}
#Override
public Object getItem(int i) {
return menuData.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderEmpDir holder;
if (convertView == null) {
holder = new ViewHolderEmpDir();
convertView = inflater.inflate(R.layout.emp_directory_list_row_layout, parent, false);
holder.name = (TextView) convertView.findViewById(R.id.name_textView_emp_directory_list_row_layout);
holder.email = (TextView) convertView.findViewById(R.id.email_textView_emp_directory_list_row_layout);
holder.phone = (TextView) convertView.findViewById(R.id.phone);
holder.pic = (ImageView) convertView.findViewById(R.id.emp_pic_emp_directory_list_row_layout);
holder.viewButton = (ImageView) convertView.findViewById(R.id.view_button_emp_dir_list_row_layout);
convertView.setTag(holder);
}
else
{
holder = (ViewHolderEmpDir) convertView.getTag();
}
holder.name.setText(menuData.get(position).getEmp_name());
holder.email.setText(menuData.get(position).getEmp_email());
holder.phone.setText(menuData.get(position).getEmp_contact_num());
//Linkify.addLinks(holder.email,Linkify.EMAIL_ADDRESSES);
//Linkify.addLinks(holder.phone,Linkify.PHONE_NUMBERS);
holder.pic.setImageResource(R.drawable.user_sign_up);
holder.viewButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context,MainActivity.class);
}
});
//downloadImage(position,holder);
return convertView;
}
// private void downloadImage(int position,ViewHolderShowAllBooking holder)
// {
// Picasso.with(context)
// .load("http://www.efefoundation.net/inklink/uploads/artist/"+menuData.get(position).getArtistId()+".jpg")
// .fit() // will explain later
// .into(holder.profile);
// }
static class ViewHolderEmpDir
{
TextView name;
TextView email;
TextView phone;
ImageView pic;
ImageView viewButton;
}
}
This is list_row_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:weightSum="4"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_marginTop="5dp"
android:layout_centerHorizontal="true"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="70dp"
android:background="#color/colorAccent">
<ImageView
android:id="#+id/emp_pic_emp_directory_list_row_layout"
android:layout_marginTop="5dp"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_marginBottom="5dp"
android:src="#drawable/user_sign_up"/>
</RelativeLayout>
<RelativeLayout
android:background="#color/colorAccent"
android:layout_marginTop="5dp"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="3">
<TextView
android:id="#+id/name_textView_emp_directory_list_row_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textColor="#fff"
android:textSize="18dp"
android:layout_alignParentLeft="true"
android:text="Husnain"/>
<TextView
android:id="#+id/phone"
android:layout_toRightOf="#id/name_textView_emp_directory_list_row_layout"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:autoLink="phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="***********"
android:textColor="#fff"/>
<TextView
android:id="#+id/email_textView_emp_directory_list_row_layout"
android:layout_alignParentLeft="true"
android:layout_below="#id/name_textView_emp_directory_list_row_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mhn786#gmail.com"
android:autoLink="email"
android:layout_marginLeft="10dp"
android:textSize="18dp"
android:textColor="#fff"
/>
<ImageView
android:id="#+id/view_button_emp_dir_list_row_layout"
android:src="#drawable/view"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>
When I apply OnItemClickListener it does not do any action like intent inside the func not called. I tried toast too but nothing shown on clicking the listView Items.
I also tried OnItemSlectedListener , it also not worked for me.
What is the problem here with this code. ?
Thanks Every One. Issue was with autolink on email and phone, I make their focusable=false but nothing happened.Then I remove it and guess what? Clicklistener also starts working.
The attribute android:clickable="true" in your ListView could be overriding all of click events of its child views, you should probably remove it:
<ListView
android:clickable="true" <!-- Remove this attribute -->
android:id="#+id/listView_emp_directory_xml"
android:layout_below="#id/relatice_layout_header_emp_directory_activity_xml"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

Facebook Native in feed ads are overlapping to each other in RecyclerView

I have an app which has Recyclerview and I want to put some facebook Native ads in between list items, like every 5 list items 1 native ad will be shown. everything is working perfectly but main problem is when I scroll down adChoice icon is being doubled and on scrolling up also adChoice icons are getting doubled. It seems that ads are overlapping to the previous one.
SCREENSHOT
Any suggestion will help me a lot. Here is the all source code and the official facebook audience network native ad code sample.
dependency
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.volley:volley:1.1.0'
implementation 'com.facebook.android:audience-network-sdk:4.+'
activity_main.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_id"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
content_layout.xml
<?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="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="#+id/textViewHead"
android:text="Heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"/>
<TextView
android:id="#+id/textViewDesc"
android:text="Description"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
ads_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:orientation="vertical">
<LinearLayout
android:id="#+id/adChoicesContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:orientation="vertical"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_gravity="center_vertical"
android:layout_marginBottom="5dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<com.facebook.ads.AdIconView
android:id="#+id/adIconView"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
tools:src="#mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tvAdTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:maxLines="1"
android:textColor="#android:color/white"
tools:text="Ad Title"/>
<TextView
android:id="#+id/tvAdBody"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="center_vertical"
android:lines="3"
android:textColor="#android:color/darker_gray"
tools:text="This is an ad description."/>
</LinearLayout>
</LinearLayout>
<com.facebook.ads.MediaView
android:id="#+id/mediaView"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<TextView
android:id="#+id/sponsored_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:textColor="#android:color/darker_gray"
android:textSize="10sp"/>
<Button
android:id="#+id/btnCTA"
style="?android:attr/borderlessButtonStyle"
android:layout_width="130dp"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_gravity="right"
android:layout_marginTop="20dp"
android:background="#android:color/darker_gray"
android:gravity="center"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:text="Install Now"
android:textColor="#android:color/white"
android:textSize="14sp"/>
</RelativeLayout>
</LinearLayout>
ContentModel.java
package com.example.my_demo_app.fb_in-feed_ad;
public class ContentModel {
String head, des;
public ContentModel(String head, String des) {
this.head = head;
this.des = des;
}
public String getHead() {
return head;
}
public String getDes() {
return des;
}
}
MyAdapter.java
package com.example.my_demo_app.fb_in-feed_ad;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.facebook.ads.Ad;
import com.facebook.ads.AdChoicesView;
import com.facebook.ads.AdIconView;
import com.facebook.ads.MediaView;
import com.facebook.ads.NativeAd;
import java.util.ArrayList;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int MENU_ITEM_VIEW_TYPE = 0;
public static final int AD_ITEM_VIEW_TYPE = 1;
private final List<Object> mRecyclerViewItems;
private final Context context;
public MyAdapter(List<Object> recyclerViewItems, Context context) {
this.mRecyclerViewItems = recyclerViewItems;
this.context = context;
}
//--------------------getItemViewType
#Override
public int getItemViewType(int position) {
Object recyclerViewItem = mRecyclerViewItems.get(position);
if (recyclerViewItem instanceof ContentModel) {
return MENU_ITEM_VIEW_TYPE;
} else if (recyclerViewItem instanceof Ad) {
return AD_ITEM_VIEW_TYPE;
} else {
return -1;
}
}
//--------------------getItemCount
#Override
public int getItemCount() {
return mRecyclerViewItems.size();
}
//--------------------onCreateViewHolder
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
if (viewType==MENU_ITEM_VIEW_TYPE){
View menuItemView = inflater.inflate(R.layout.content_layout, parent, false);
return new MenuItemHolder(menuItemView);
}else if (viewType==AD_ITEM_VIEW_TYPE){
View adItemView = inflater.inflate(R.layout.ads_layout, parent, false);
return new AdHolder(adItemView);
}
return null;
}
//--------------------onBindViewHolder
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
int itemType = getItemViewType(position);
if (itemType==MENU_ITEM_VIEW_TYPE){
MenuItemHolder menuItemHolder = (MenuItemHolder) holder;
ContentModel contentModel = (ContentModel) mRecyclerViewItems.get(position);
menuItemHolder.txtH.setText(contentModel.getHead());
menuItemHolder.txtD.setText(contentModel.getDes());
}else if (itemType==AD_ITEM_VIEW_TYPE){
AdHolder nativeAdViewHolder = (AdHolder) holder;
NativeAd nativeAd = (NativeAd) mRecyclerViewItems.get(position);
AdIconView adIconView = nativeAdViewHolder.adIconView;
TextView tvAdTitle = nativeAdViewHolder.tvAdTitle;
TextView tvAdBody = nativeAdViewHolder.tvAdBody;
Button btnCTA = nativeAdViewHolder.btnCTA;
LinearLayout adChoicesContainer = nativeAdViewHolder.adChoicesContainer;
MediaView mediaView = nativeAdViewHolder.mediaView;
TextView sponsorLabel = nativeAdViewHolder.sponsorLabel;
tvAdTitle.setText(nativeAd.getAdvertiserName());
tvAdBody.setText(nativeAd.getAdBodyText());
btnCTA.setText(nativeAd.getAdCallToAction());
sponsorLabel.setText(nativeAd.getSponsoredTranslation());
AdChoicesView adChoicesView = new AdChoicesView(context, nativeAd, true);
adChoicesContainer.addView(adChoicesView);
List<View> clickableViews = new ArrayList<>();
clickableViews.add(btnCTA);
clickableViews.add(mediaView);
nativeAd.registerViewForInteraction(nativeAdViewHolder.container, mediaView, adIconView, clickableViews);
}
}
//--------------------MenuItemHolder
public class MenuItemHolder extends RecyclerView.ViewHolder{
public TextView txtH, txtD;
public MenuItemHolder(View itemView) {
super(itemView);
txtH = (TextView) itemView.findViewById(R.id.textViewHead);
txtD = (TextView) itemView.findViewById(R.id.textViewDesc);
}
}
//--------------------AdHolder
public class AdHolder extends RecyclerView.ViewHolder{
AdIconView adIconView;
TextView tvAdTitle;
TextView tvAdBody;
Button btnCTA;
View container;
TextView sponsorLabel;
LinearLayout adChoicesContainer;
MediaView mediaView;
AdHolder(View itemView) {
super(itemView);
this.container = itemView;
adIconView = (AdIconView) itemView.findViewById(R.id.adIconView);
tvAdTitle = (TextView) itemView.findViewById(R.id.tvAdTitle);
tvAdBody = (TextView) itemView.findViewById(R.id.tvAdBody);
btnCTA = (Button) itemView.findViewById(R.id.btnCTA);
adChoicesContainer = (LinearLayout) itemView.findViewById(R.id.adChoicesContainer);
mediaView = (MediaView) itemView.findViewById(R.id.mediaView);
sponsorLabel = (TextView) itemView.findViewById(R.id.sponsored_label);
}
}
}
MainActivity.java
package com.example.my_demo_app.fb_in-feed_ad;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.facebook.ads.Ad;
import com.facebook.ads.AdError;
import com.facebook.ads.NativeAd;
import com.facebook.ads.NativeAdListener;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final String URL_DATA = "https://res.cloudinary.com/ravi40/raw/upload/v1532239134/my_json/heroes_list.json";
private RecyclerView recyclerView;
private List<Object> mRecyclerViewItems;
MyAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view_id);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerViewItems = new ArrayList<>();
//--------------------Native Ad
NativeAd nativeAd = new NativeAd(getApplicationContext(), "IMG_16_9_LINK#YOUR_FACEBOOK_NATIVE_AD_PLACEMENT_ID_WILL_GOES_HERE"); // IMG_16_9_LINK# denote only testing purpose
nativeAd.setAdListener(new NativeAdListener() {
#Override
public void onMediaDownloaded(Ad ad) {
}
#Override
public void onError(Ad ad, AdError adError) {
}
#Override
public void onAdLoaded(Ad ad) {
for (int i=4; i<mRecyclerViewItems.size()+4; i+=5)
mRecyclerViewItems.add(i, ad);
adapter.notifyDataSetChanged();
}
#Override
public void onAdClicked(Ad ad) {
}
#Override
public void onLoggingImpression(Ad ad) {
}
});
nativeAd.loadAd();
loadRecyclerViewData();
}
private void loadRecyclerViewData(){
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading Data...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET,
URL_DATA,
new Response.Listener<String>() {
#Override
public void onResponse(String s) {
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(s);
JSONArray array = jsonObject.getJSONArray("heroes");
for (int i = 0; i<array.length(); i++){
JSONObject o = array.getJSONObject(i);
ContentModel item = new ContentModel(
o.getString("name"),
o.getString("about")
);
mRecyclerViewItems.add(item);
}
adapter = new MyAdapter(mRecyclerViewItems, getApplicationContext());
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
Ad are getting overlapped when adding in the container. The solution is to clear the container before adding the ads again.
For example:
AdChoicesView adChoicesView = new AdChoicesView(context, nativeAd, true);
// Clear the container.
adChoicesContainer.removeAllViews()
adChoicesContainer.addView(adChoicesView);

I want to create an Android Page which looks like this

I am beginner Android Developer. I want to create an Android Page which will dynamically display Three things 1. Question No. 2. Your Answer 3. Right Answer. How can I do this? It should look like this and can grow dynamically
What about something like this (Uses a listview):
Mainactivity:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ArrayList<Question> questionArrayList;
private void fillList(){
questionArrayList = new ArrayList<>();
for (int i = 0; i < 12; i++) {
questionArrayList.add(new Question(i,"myans","yourans"));
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fillList();
ListView mlIstView = (ListView)findViewById(R.id.mListview);
QuestionAdapter adapter = new QuestionAdapter(this,R.layout.listviewlayout,questionArrayList);
mlIstView.setAdapter(adapter);
}
}
Question.java
public class Question {
private int numberOfQuestion;
private String yourAnswer;
private String correctAnswer;
public Question(int numberOfQuestion, String yourAnswer, String correctAnswer) {
this.numberOfQuestion = numberOfQuestion;
this.yourAnswer = yourAnswer;
this.correctAnswer = correctAnswer;
}
public int getNumberOfQuestion() {
return numberOfQuestion;
}
public String getYourAnswer() {
return yourAnswer;
}
public String getCorrectAnswer() {
return correctAnswer;
}
}
QuestionAdapter
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by waro on 6/4/17.
*/
public class QuestionAdapter extends ArrayAdapter<Question> {
private ArrayList<Question> arrayList;
public QuestionAdapter(#NonNull Context context, #LayoutRes int resource, #NonNull ArrayList<Question> objects) {
super(context, resource, objects);
arrayList = objects;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
if(convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.listviewlayout,parent,false);
}
Question question = arrayList.get(position);
TextView questiontv = (TextView)convertView.findViewById(R.id.question);
TextView yourans = (TextView)convertView.findViewById(R.id.yourans);
TextView correctans = (TextView)convertView.findViewById(R.id.correctans);
questiontv.setText(Integer.toString(question.getNumberOfQuestion()));
yourans.setText(question.getYourAnswer());
correctans.setText(question.getCorrectAnswer());
return convertView;
}
}
activity_mail.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.suprentive.waro.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:id="#+id/mLinearLayout">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Question"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Your answer"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Right answer"
android:layout_weight="1"/>
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mListview"
android:layout_below="#id/mLinearLayout">
</ListView>
</RelativeLayout>
listviewlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/question"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/yourans"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/correctans"
android:layout_weight="1"/>
</LinearLayout>
Result
Hopefully this helps you out.

listview item is not added on button click

I have an issue with adapter adding data to listview on button click.
I have a activity called createteam that has name and email edittext and a createplayer button. On click createplayer button, the name and email should be added to list view.
I want to use an adapter for doing that(so that I can learn).
Update:
Onclicking the button the the first entry gets added but when I fill in the
edittexts again, the listview does get updated. I am notifying the adapter when I update
the data but only first entry gets and remains added to list view
create_team.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/teamname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/blue"
android:hint="Team Name"
android:gravity="center"
android:textStyle="bold">
</EditText>
<Button
android:id="#+id/addplayer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/blue"
android:text="Add Player"
android:layout_gravity="center"
android:textStyle="bold">
</Button>
<LinearLayout
android:id="#+id/view_addplayer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="#+id/createplayer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/red"
android:text="Create Player"
android:layout_gravity="center"
android:textStyle="bold">
</Button>
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/blue"
android:hint="Name"
android:gravity="center"
android:textStyle="bold">
</EditText>
<EditText
android:id="#+id/email"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/blue"
android:hint="Email "
android:textStyle="bold"
android:gravity="center">
</EditText>
</LinearLayout>
<ListView
android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</ScrollView>
add_player_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:orientation="horizontal"
android:weightSum="2">
<TextView android:id="#+id/name"
android:textSize="20sp"
android:textStyle="bold"
android:color="#color/blue"
android:textColor="#FFFF00"
android:layout_width="wrap_content"
android:layout_height="50dp"/>
<TextView android:id="#+id/email"
android:textSize="15sp"
android:textStyle="bold"
android:color="#color/mustard"
android:layout_width="wrap_content"
android:layout_height="50dp"/>
</LinearLayout>
CreateTeamActivity.java
package com.recscores.android;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.Toast;
import com.recscores.android.PlayerInfo;
public class CreateTeamActivity extends ListActivity {
Button createPlayer;
EditText Email;
EditText Name;
private ArrayList<PlayerInfo> newList;
private PlayerAddAdapter newAdpt;
private int i = 0;
private ListView lv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_team);
newList = new ArrayList<PlayerInfo>();
// CREATE TEAM
getWindow().setBackgroundDrawableResource(R.drawable.app_background2);
createPlayer = (Button)findViewById(R.id.createplayer);
Email = (EditText)findViewById(R.id.email);
Name = (EditText)findViewById(R.id.name);
lv = (ListView)findViewById(android.R.id.list);
newAdpt = new PlayerAddAdapter(CreateTeamActivity.this,android.R.id.list,newList);
lv.setAdapter(newAdpt);
createPlayer.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// newList = new ArrayList<PlayerInfo>();
PlayerInfo info = new PlayerInfo();
info.SetName(Name.getText().toString());
info.SetEmail(Email.getText().toString());
newList.add(info);
newAdpt.notifyDataSetChanged();
//Thread.sleep(2000);
Name.setText("");
Email.setText("");
}
});
}
}
PlayerAddAdapter.java
package com.recscores.android;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class PlayerAddAdapter extends ArrayAdapter<PlayerInfo> {
private Activity activity;
//private final Context mContext;
private ArrayList<PlayerInfo> items;
public PlayerAddAdapter(Activity a, int textViewResourceId, ArrayList<PlayerInfo> items){
super(a,textViewResourceId,items);
this.items=items;
this.activity = a;
}
public static class ViewHolder{
public TextView name;
public TextView email;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi.inflate(R.layout.add_player_listview, null);
holder = new ViewHolder();
holder.name = (TextView)v.findViewById(R.id.name);
holder.email = (TextView)v.findViewById(R.id.email);
v.setTag(holder);
}
else
holder=(ViewHolder)v.getTag();
final PlayerInfo custom = items.get(position);
if (custom != null) {
**holder.name.setText(custom.GetName());
holder.email.setText(custom.GetEmail());
}
return v;
}
}
PlayerInfo.java
package com.recscores.android;
public class PlayerInfo {
private String name="";
private String email="";
public void SetName(String name){
this.name = name;
}
public String GetName(){
return this.name;
}
public void SetEmail(String email){
this.email = email;
}
public String GetEmail(){
return this.email;
}
}
LayoutInflater vi =(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.add_player_listview, null);
Change your code like this. You are not setting values to v variable.
onClick of your button call list.notifyDataSetChanged(). It will refresh your list.
Incase if doesn't work just check getCount() of your adapter what value it is returning. If it is returning 0 your list will not show any data even you call list.notifyDataSetChanged().
Make an arrayList in your adapter and add new items into that. and from getCount() return size of arrayList.

Categories

Resources