this.getContext() might be null, in a fragment using custom adapter - java

i have implemented a custom adapter on a fragment but it produces the error in the getView method of the fragment on the below line ,
CustomAdapter adapter = new CustomAdapter(this.getContext(), sub);
listView.setAdapter(adapter);
Here is the fragment_third.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"
tools:context=".ThirdFragment">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listviewthird"/></RelativeLayout>
Here is the sec.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image_below"
android:layout_width="match_parent"
android:layout_height="150dp"
android:src="#drawable/background_1" />
<TextView
android:id="#+id/dept_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="#string/app_name"
android:textSize="#dimen/text_size"
android:textStyle="bold" /></RelativeLayout>
Here is the ThirdFragment.class
public class ThirdFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
String[] sub = {"random1", "random2", "random3", };
//code has been vomited.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_third, container, true);
ListView listView = view.findViewById(R.id.listviewthird);
CustomAdapter adapter = new CustomAdapter(this.getContext(), sub);
listView.setAdapter(adapter);
return view;
}
Here is the CustomAdapter class
public class CustomAdapter extends ArrayAdapter<String> {
String[] example;
Context mContext;
public CustomAdapter(#NonNull Context context, String[] example) {
super(context, R.layout.sec);
this.example = example;
this.mContext = context;
}
#Override
public int getCount() {
return example.length; //returns the size of the list
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
ViewHolder mViewHolder = new ViewHolder();
if(convertView == null) {
LayoutInflater mInflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflator.inflate(R.layout.sec, parent, false);
mViewHolder.mExample = (TextView) convertView.findViewById(R.id.dept_name);
mViewHolder.mExample.setText(subjects[position]);
convertView.setTag(mViewHolder);
}else {
mViewHolder = (ViewHolder) convertView.getTag();
}
return convertView;
}
static class ViewHolder {
TextView mExample;
}}
Am i doing wrong while implementing adapter on fragment? Someone explain me.

Use view.getContext(). It works for me

Related

Application keeps crashing with "E/RecyclerView: No adapter attached; skipping layout"

My application keeps crashing whenever I try to open up the layout with a RecyclerView.
Apparently, I am getting this error:
E/RecyclerView: No adapter attached; skipping layout
The code was mostly copied from the documentation.
MyAdapter.java
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private String[] mDataset;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class MyViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView textView;
public MyViewHolder(TextView v) {
super(v);
textView = v;
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(String[] myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
TextView v = (TextView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.textView.setText(mDataset[position]);
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return mDataset.length;
}
}
ChatActivity.java
public class ChatActivity extends AppCompatActivity{
private RecyclerView recyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager layoutManager;
String[] myDataset = {"Hello", "Who r u", "Wait what", "Nice to meet you", "Yeet"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
recyclerView.setHasFixedSize(true);
// use a linear layout manager
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
// specify an adapter (see also next example)
mAdapter = new MyAdapter(myDataset);
recyclerView.setAdapter(mAdapter);
}
}
activity_chat.xml
<?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">
<Button
android:id="#+id/btnFinish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/back_button"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
my_text_view.xml
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="#+id/textView" />
</LinearLayout>
Your my_text_view.xml should contain the TextView as root element according your implementation.
Approach-1:
So, just change this like below solve your problem.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="#+id/textView" />
Approach-2:
Or change your onCreateViewHolder implementation:
onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.my_text_view, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
And change ViewHolder also:
MyViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView textView;
public MyViewHolder(View v) {
super(v); textView = v;
textView = v.findViewById(R.id.textView);
}
}

What's wrong with my implementation of RecyclerView?

I am following the Android Developer's explanatin on how to set RecyclerView, but I get some errors, for example android.widget.LinearLayout cannot be cast to android.widget.TextView.
The code taken from here : RecyclerView
This is my code, including all necessary files:
MainActivity.java:
package com.example.lastlocation.recyclerviewer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
String[] myDataset = new String[5];
myDataset[0] = "Data0";
myDataset[1] = "Data1";
myDataset[2] = "Data2";
myDataset[3] = "Data3";
myDataset[4] = "Data4";
// specify an adapter (see also next example)
mAdapter = new MyAdapter(myDataset);
mRecyclerView.setAdapter(mAdapter);
}
}
This is MyAdapter.java:
package com.example.lastlocation.recyclerviewer;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private String[] mDataset;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class MyViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public MyViewHolder(TextView v) {
super(v);
mTextView = v;
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(String[] myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
TextView v = (TextView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.mTextView.setText(mDataset[position]);
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return mDataset.length;
}
}
this is activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">
<View
android:id="#+id/view"
android:layout_width="386dp"
android:layout_height="135dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageView"
android:layout_width="66dp"
android:layout_height="64dp"
android:layout_marginTop="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/view"
app:srcCompat="#android:color/holo_orange_dark" />
<TextView
android:id="#+id/textView2"
android:layout_width="68dp"
android:layout_height="30dp"
android:layout_marginTop="8dp"
android:text="John Doe"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView" />
<!-- A RecyclerView with some commonly used attributes -->
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="375dp"
android:layout_height="367dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.428"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view"
app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>
And this is the xml for a single text view called my_text_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout for a single list item -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="#+id/my_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
As you can see I nearly copied the code from the Android Developer's website, but I still don't know where is the problem.
public static class MyViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public MyViewHolder(TextView v) {
super(v);
mTextView = v;
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(String[] myDataset) {
mDataset = myDataset;
}
The problem is in your ViewHolder constructor it should be like that
public static class MyViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public MyViewHolder(View v) {
super(v);
mTextView = v.findViewById(R.id.your_text_view_id);
}
}
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = (TextView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
You are trying to assign LinearLayout which is the View in your constructor to your TextView and it is crashing. This will fix the problem.
The view you receive as a constructor param in your ViewHolder is the root view of your ViewHolder, in your case a LinearLayout, from this root view you can find the other views in it.
You are inflating row view wrongly and thus not assigning views in viewholder in proper manner change your code as below
#Override
public UserListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
ViewHolder vh = new ViewHolder((inflater.inflate(R.layout.your_row_layout, parent, false)));
return vh;
}
and change your viewholder as below
public static class MyViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public MyViewHolder(View v) {
super(v);
mTextView = v.findViewById(R.id.my_text_view);
}
}
Just Simply you need to change your onCreateViewHolder method by taking as
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);
return view;

Not displaying horizontal and vertical list in fragment

Creating a tabbed fragment activity, in my home_fragment i am trying to display a horizontal and a vertical list view, but its not displaying the list, below is my code of home_fragment for onCreateView() method --
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v=inflater.inflate(R.layout.fragment_home, container, false);
horizontalList = (RecyclerView)v.findViewById(R.id.horizontal_recycler);
verticalList = (RecyclerView)v.findViewById(R.id.recyle_view);
horizontalList.setHasFixedSize(true);
verticalList.setHasFixedSize(true);
//set horizontal LinearLayout as layout manager to creating horizontal list view
LinearLayoutManager horizontalManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
horizontalList.setLayoutManager(horizontalManager);
horizontalAdapter = new HorizontalListAdapter(getActivity());
horizontalList.setAdapter(horizontalAdapter);
//set vertical LinearLayout as layout manager for vertial listview
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
verticalList.setLayoutManager(layoutManager);
verticalAdapter = new VerticalListAdapter(getActivity());
verticalList.setAdapter(verticalAdapter);
return v;
}
Is there any changes to be made here.
HorizontalListAdapter.java --
public class HorizontalListAdapter extends RecyclerView.Adapter<HorizontalListAdapter.ViewHolder>{
private Activity activity;
public HorizontalListAdapter(Activity activity) {
this.activity = activity;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
LayoutInflater inflater = activity.getLayoutInflater();
View view = inflater.inflate(R.layout.item_horizontal_list, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(HorizontalListAdapter.ViewHolder viewHolder, final int position) {
viewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(activity, "Position clicked: " + position, Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return 10;
}
/**
* View holder to display each RecylerView item
*/
protected class ViewHolder extends RecyclerView.ViewHolder {
private LinearLayout linearLayout;
public ViewHolder(View view) {
super(view);
linearLayout = (LinearLayout) view.findViewById(R.id.layout);
}
}
}
item_horizontal_list.xml--
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp">
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/mountain" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="#string/lorem_ipsum" />
</LinearLayout>
</android.support.v7.widget.CardView>
Not displaying horizontal and vertical list in fragment
in your onBindViewHolder you are no displaying any data check out your adapter class
You need to set any data inside your onBindViewHolder like this
Sample code
public class HorizontalListAdapter extends RecyclerView.Adapter<HorizontalListAdapter.ViewHolder> {
private Activity activity;
public HorizontalListAdapter(Activity activity) {
this.activity = activity;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
LayoutInflater inflater = activity.getLayoutInflater();
View view = inflater.inflate(R.layout.item_horizontal_list, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(HorizontalListAdapter.ViewHolder viewHolder, final int position) {
viewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(activity, "Position clicked: " + position, Toast.LENGTH_SHORT).show();
}
});
viewHolder.edtName.setText("NILU");
viewHolder.imageView.setImageResource(R.mipmap.ic_launcher);
}
#Override
public int getItemCount() {
return 10;
}
/**
* View holder to display each RecylerView item
*/
protected class ViewHolder extends RecyclerView.ViewHolder {
private LinearLayout linearLayout;
TextView edtName, edtPhone, edtPass, edtAdrress;
ImageView imageView;
public ViewHolder(View view) {
super(view);
linearLayout = (LinearLayout) view.findViewById(R.id.layout);
edtName = view.findViewById(R.id.edtUname);
imageView = view.findViewById(R.id.imageView);
}
}
}
Changes in your layout you need to set id to your imageview and textview
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp">
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/mountain" />
<TextView
android:id="#+id/edtUname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="#string/lorem_ipsum" />
</LinearLayout>
</android.support.v7.widget.CardView>
here is the good tutorial of Android working with Card View and Recycler View

Android: Error when i try to add view while "setContentView()" not specified

the idea is showing list view with icon (each row with different icon). it works perfectly but when i try to add adMob Banner to my view but i get errors "java.lang.NullPointerException".
When i try to do it with setContentView() it says you must have R.id.list, even if i add a list, same error remains.
Code:
MainActivity.class
public class MainActivity extends ListActivity {
static final String[] ITEMS = new String[] { "Item1", "Item2",
"Item3", "Item4"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new HowtoArrayAdapter(this, ITEMS));
showBanner();
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String selectedItem = (String) getListAdapter().getItem(position);
//Toast.makeText(this, selectedItem, Toast.LENGTH_SHORT).show();
}
public void showBanner(){
RelativeLayout adContainer = (RelativeLayout) findViewById(R.id.activity_main);
AdView adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(getString(R.string.banner_ad_unit_id));
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
adContainer.addView(adView, params);
}}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/logo"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/tower" />
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/logo"
android:paddingLeft="10dp"
android:paddingTop="21dp"
android:textSize="26sp" />
</RelativeLayout>
The Adapter Class:
public class HowtoArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public HowtoArrayAdapter(Context context, String[] values) {
super(context, R.layout.activity_main, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.activity_main, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
textView.setText(values[position]);
//........
return rowView;
}
}
Thank you in advance!

Cannot resolve method setListAdapter

I try to show listView in fragment. But method setListAdapter - is not resolved.
I think, that i must to get id of listView (android.R.id.list);
and then : lv.setAdapter(mAdapter);but its dont work too.
public class MyEmployeeFragment extends Fragment {
private CustomAdapter sAdapter;
private List<User> userList;
ProgressDialog mProgressDialog;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
userList = new ArrayList<User>();
sAdapter = new CustomAdapter(getActivity(),userList);
setListAdapter(sAdapter);
new CustomAsync().execute();
}
private class CustomAdapter extends ArrayAdapter<User> {
private LayoutInflater inflater;
public CustomAdapter(Context context, List<User> objects) {
super(context, 0, objects);
inflater = LayoutInflater.from(context);
}
class ViewHolder {
TextView id;
TextView name;
TextView lastName;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vH;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_employee, null);
vH = new ViewHolder();
vH.id = (TextView) convertView.findViewById(R.id.tv_employee_id);
vH.name = (TextView) convertView.findViewById(R.id.tv_employee_name);
vH.lastName = (TextView) convertView.findViewById(R.id.tv_employee_last_name);
convertView.setTag(vH);
} else
vH = (ViewHolder) convertView.getTag();
final User user = getItem(position);
vH.id.setText(user.getId());
vH.name.setText(user.getName());
vH.lastName.setText(user.getLastName());
return convertView;
}
}
private class CustomAsync extends AsyncTask<Void,Void,List<User>>
{}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:divider="#B7B7B7"
/>
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar2"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="#id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/empty"
android:layout_above="#+id/progressBar2"
android:layout_alignRight="#+id/progressBar2"
android:layout_alignEnd="#+id/progressBar2"
android:layout_marginBottom="83dp">
</TextView>
</RelativeLayout>
setListAdapter is a method of ListFragment while your fragment extends Fragment.
http://developer.android.com/reference/android/app/ListFragment.html#setListAdapter(android.widget.ListAdapter)
So you either extend ListFragment
or
Extend Fragment inflate a layout with ListView, initialize ListView in onCreateView of Fragment and then use listView.setAdapter(adapter).
In case you want to extend Fragment
<ListView
android:id="#+id/list"
Then
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.yourxml, container, false);
ListView lv = (ListView)rootView.findViewById(R.id.list);
lv.setAdapter(youradapter);
return rootView;
}
setListAdapter() is a method in ListFragment..For this you need to extend ListFragmnet instead of Fragment.
Change this line
public class MyEmployeeFragment extends Fragment
into
public class MyEmployeeFragment extends ListFragment
You need define the ListView first:
ListView list = (ListView) findById(android.R.id.list);
And later you need put the adapter:
list.setAdapter(sAdapter);
Or change
extends Fragment
to
extends ListFragment
extends ListFragment should be working fine.
Below is a sample that works for me..
public class Chicks extends ListFragment implements OnItemClickListener {
}

Categories

Resources