I got a problem with my Custom Array Adapter:
I have a NullpointerException in my getView() Method.
I already found out that the ArrayList seems not to be the problem because it works fine without the Adapter.
My Adapter
public class ArrayAdapterCusPostlist extends ArrayAdapter<Post> {
Context mContext;
ArrayList<Post> postListe;
public ArrayAdapterCusPostlist(Context mContext, int layoutResourceId, ArrayList<Post>pList) {
super(mContext, layoutResourceId, pList);
postListe = pList;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(R.layout.rowpost, parent, false);
}
Post curpost=postListe.get(position);
if(curpost!=null){
TextView textViewItem = (TextView) convertView.findViewById(R.id.nickname2);
textViewItem.setText(curpost.getName());
textViewItem.setTag(curpost.getName());
}
return convertView;
}
}
And: If the ArrayList is Empty because I did not called the add() Method there is no NullPointerException. Here is my Array List:
ArrayList<Post> PostList=new ArrayList<Post>();
PostList.add(new Post("Name", "Zeit", "Post", getResources().getDrawable(R.drawable.winter), getResources().getDrawable(R.drawable.winter)));
mPostList = (ListView) findViewById(R.id.postliste);
final ArrayAdapterCusPostlist adapter2 = new ArrayAdapterCusPostlist(this, R.layout.rowpost, PostList);
mPostList.setAdapter(adapter2);
Thanks!
Context mContext; is not initialized
Context mContext;
LayoutInflater inflater;
public ArrayAdapterCusPostlist(Context mContext, int layoutResourceId, ArrayList<Post>pList) {
super(mContext, layoutResourceId, pList);
this.mContext =mContext;
inflater = LayoutInflater.from(context); // initialize here. no need for context initialization if you have this here.
}
Also it is better you use a ViewHolder Pattern
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
convertView = inflater.inflate(R.layout.rowpost, parent, false);
holder = new ViewHolder();
holder.textViewItem = (TextView) convertView.findViewById(R.id.nickname2);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
Post curpost=postListe.get(position);
holder.textViewItem.setText(curpost.getName());
holder.textViewItem.setTag(curpost.getName());
}
return convertView;
static class ViewHolder
{
TextView textViewItem;
}
}
Related
ListView Adapter Class:
public class ListViewAdapter extends ArrayAdapter {
private Context context;
private int resource;
private ArrayList<HashMap<String,String>> list2 = new ArrayList<>();
public ListViewAdapter(Context context,int resource, List<HashMap<String, String>> list) {
super(context, resource, list);
this.resource = resource;
this.context = context;
this.list2 = (ArrayList<HashMap<String, String>>) list;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
ViewHolder holder;
Object newMap = list2.get(position);
if (row == null){
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(resource, parent, false);
holder = new ViewHolder();
holder.name1 = (TextView) row.findViewById(R.id.name);
holder.add = (TextView) row.findViewById(R.id.address);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
String pname = ((HashMap<String, String>) newMap).get("place_name");
holder.name1.setText(pname);
return row;
}
public class ViewHolder {
TextView name1,add;
}
}
All data coming in ListView Adapter class. But when we try to show data into list view it's got a crash.
The Crash shown here holder.name1.setText(pname) in ListView Adapter class. And error show NullPointerException
add .setText() in if condition error occurred because you initialize Textview name1 in if condition,so check below code
#Override
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
ViewHolder holder;
Object newMap = list2.get(position);
if (row == null){
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(resource, parent, false);
holder = new ViewHolder();
holder.name1 = (TextView) row.findViewById(R.id.name);
holder.add = (TextView) row.findViewById(R.id.address);
String pname = ((HashMap<String, String>) newMap).get("place_name");
holder.name1.setText(pname);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
return row;
}
As per my understanding, there is tow possible reason to show null pointer exception one is you may be getting null value in 'place_name' and another one is your referenced R.id.name is not available.
Edit your code like following,
String pname="";
pname = ((HashMap<String, String>) newMap).get("place_name");
if(holder.name1!=null){
holder.name1.setText(pname)
}
Finally resolve this problem, silly mistake I have does not add TextView in R.layout.activity_list_view.
There is no problem in your adapter except one error that is
add below code in your ListViewAdapter class
like this
#Override
public int getCount() {
return list2.size();
}
Hope this will work for you...
I am trying to display JSON data but it's giving null pointer exception. It can't get the context of custom single item that I'm inflating
public class HotelAdapter extends ArrayAdapter{
private List<HotelModel> hotelModelList;
private LayoutInflater layoutInflater;
private int resource;
public HotelAdapter(#NonNull Context context, int resource, #NonNull
List<HotelModel> objects) {
super(context, resource, objects);
hotelModelList = objects;
this.resource=resource;
layoutInflater =
(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull
ViewGroup parent) {
ViewHolder holder = null;
if (convertView==null){
holder =new ViewHolder();
convertView = layoutInflater.inflate(R.layout.item_hotel,null);
holder.addr =
(TextView)convertView.findViewById(R.id.hotel_add_mini);
holder.categ =
(TextView)convertView.findViewById(R.id.hotel_rating);
holder.name =
(TextView)convertView.findViewById(R.id.hotel_name);
holder.price =
(TextView)convertView.findViewById(R.id.hotel_price);
holder.rating =
(RatingBar)convertView.findViewById(R.id.ratingBar2);
}
else {
holder= (ViewHolder) convertView.getTag();
}
holder.addr.setText(hotelModelList.get(position).getAddr());
holder.categ.setText(hotelModelList.get(position).getCateg());
holder.name.setText(hotelModelList.get(position).getName());
holder.price.setText(Integer.toString(hotelModelList.get(position).getPrice()));
holder.rating.setRating(hotelModelList.get(position).getRating());
return convertView;
}
class ViewHolder{
private ImageView img;
private TextView name;
private TextView addr;
private TextView categ;
private TextView price;
private RatingBar rating;
}
}
It can't get the reference of TextViews from item_hotel.xml layout files. So It's giving null pointer exception. I have checked the List<>, the list is non null so it's just the reference problem. Moreover, in this
holder.addr =
(TextView)convertView.findViewById(R.id.hotel_add_mini);
The (TextView) is showing redundant but it shouldn't have as findViewById returns a view and we have to cast it into textView.
Replace this:
private List<HotelModel> hotelModelList;
private LayoutInflater layoutInflater;
private int resource;
with:
private List<HotelModel> hotelModelList;
private LayoutInflater layoutInflater;
private int resource;
private Context context;
then update your constructor and instead of this:
public HotelAdapter(#NonNull Context context, int resource, #NonNull
List<HotelModel> objects)
{
super(context, resource, objects);
hotelModelList = objects;
this.resource=resource;
layoutInflater =
(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
}
Use following:
public HotelAdapter(#NonNull Context context, int resource, #NonNull
List<HotelModel> objects)
{
super(context, resource, objects);
this.hotelModelList = objects;
this.resource=resource;
this.context = context;
}
After that instead of following line:
convertView = layoutInflater.inflate(R.layout.item_hotel,null);
Use following two lines:
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.item_hotel, parent, false);
Remove following line also:
layoutInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
This will solve your issue you need to provide context to your layout inflater I think that might be the problem in your case.
You forgot to set the tag in your if condition and your're using tag in else condition that is the problem.
In your if condition add,
convertView.setTag(holder);
Try to make a constructor and set context also.
public class HotelAdapter extends ArrayAdapter{
Context mcontext;
private List<HotelModel> mhotelModelList;
public HotelAdapter(Context context, private List<HotelModel> hotelModelList;){
super(context, R.layout.item_hotel, hotelModelList);
this.mcontext = context;
this.mhotelModelList = hotelModelList;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater)
mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_hotel, null);
holder = new ViewHolder();
holder.addr =(TextView)convertView.findViewById(R.id.hotel_add_mini);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
holder.addr.setText(hotelModelList.get(position).getAddr());
return convertView;
}
}
Basically the problem is in this line
layoutInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
try to use context which you are getting in constructor of your HotelAdapter
layoutInflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
and set tag on convertView
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_hotel, null);
holder = new ViewHolder();
holder.addr = (TextView) convertView.findViewById(R.id.hotel_add_mini);
holder.categ = (TextView) convertView.findViewById(R.id.hotel_rating);
holder.name = (TextView) convertView.findViewById(R.id.hotel_name);
holder.price = (TextView) convertView.findViewById(R.id.hotel_price);
holder.rating = (RatingBar) convertView.findViewById(R.id.ratingBar2);
convertView.setTag(holder);
} else{
holder= (ViewHolder) convertView.getTag();
}
My layout named main_layout has a RelativeLayout with at least two elements like Textview and Imageview inside it.
public class SigninFragment extends Fragment {
private List<Test> list= null;
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Test tes= new Test ();
tes.setId(1);
tes.setDesc("descabc");
list= new ArrayList<>();
list.add(prof);
tesListAdapter =
new TesListAdapter(
rootView.getContext()
,R.layout.list_row_adapter
,list);
autocompletetextview.setThreshold(3);
autocompletetextview.setAdapter(tesListAdapter );
My Adapter Class :
public class ProfissoesListAdapter extends ArrayAdapter<Test> {
private LayoutInflater inflater;
private int resource;
private Context context;
public TesListAdapter(Context activity, int resource, List<Test> listaProf) {
super(activity, resource, listaProf);
this.resource = resource;
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.main_layout, parent, false);
holder = new ViewHolder();
holder.idProfissao = (TextView) convertView.findViewById(R.id.textViewId);
//holder.descProfissao = (TextView) convertView.findViewById(R.id.textDescProf);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Test item = getItem(position);
holder.idProfissao.setText(item.getId_prof()); **<=== BUG HERE**
return convertView;
}
Debugging adapter code I got in the line holder.idProfissao.setText(item.getId_prof()); Resource id cannot be found....
The method setText() of the TextView class it's overloaded to either use a String or an integer representing an id in the form of R.string.some_text. If item.getId_prof() doesn't return an id then you need to make it a String before setting it as text:
holder.idProfissao.setText("" + item.getId_prof());
It might be silly question, but I am having bit hard time to make it work.
I have a custom list view adapter which some textview and imageview.
But when I am updating listview using custom adapter it is only updating last value. here is my code
Adapter code
public class InfoListAdapter extends ArrayAdapter<aSProperty> {
Context mContext;
List<aSProperty> values= null;
public InfoListAdapter(Context mContext, List<aSProperty> data) {
super(mContext, R.layout.list_row, data);
this.mContext = mContext;
this.values = data.;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_row, null);
signal = (ImageView) convertView.findViewById(R.id.images);
type = (TextView) convertView.findViewById(R.id.type);
info = (TextView) convertView.findViewById(R.id.info);
image.setBackgroundResource(R.drawable.abc);
Log.d(TAG, "List Size"+values.size());
for(int i = 0; i < values.size(); i++){
Log.d(TAG, "DATA"+values.get(i).type);
type.setText(values.get(i).type);
}
}
ImageView image;
TextView type;
TextView info;
static String TAG= "InfoListAdapter";
return convertView;
}
}
Code to populate adapter
info_panel = new InfoListAdapter(esActivity.this, allData);
list.setAdapter(info_panel);
But I do not know why, it is only showing last values from values list. In addition I want to change image based on condition and data inside values, any good way to do this.
use following code
public class InfoListAdapter extends ArrayAdapter<aSProperty> {
Context mContext;
List<aSProperty> values= null;
public InfoListAdapter(Context mContext, List<aSProperty> data) {
super(mContext, R.layout.list_row, data);
this.mContext = mContext;
this.values = data.;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_row, null);
signal = (ImageView) convertView.findViewById(R.id.images);
type = (TextView) convertView.findViewById(R.id.type);
info = (TextView) convertView.findViewById(R.id.info);
image.setBackgroundResource(R.drawable.abc);
Log.d(TAG, "List Size"+values.size());
type.setText(values.get(position).type);
}
ImageView image;
TextView type;
TextView info;
static String TAG= "InfoListAdapter";
return convertView;
}
}
First of all, you have to override getCount() method of your adapter like this
#Override
public int getCount() {
return values .size();
}
and then in your getView() method you have to do something like this
#Override
public View getView(int arg0, View convertView, ViewGroup arg2) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.mycartslistcontents,
null);
holder.yourImageView= (ImageView) convertView
.findViewById(R.id.image);
holder.yourTextView= (TextView) convertView
.findViewById(R.id.textview);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
// here set your textView text and image resource
return convertView;
}
Don't forget to take a global variable layoutInflater at the top like this
private LayoutInflater layoutInflater;
and initialise it in your constructor this way
layoutInflater = LayoutInflater.from(yourContext);
and here is your ViewHolder class
static class ViewHolder {
ImageView yourImageView;
TextView yourTextView;
}
I have an ArrayAdapter with ArrayList filled. Each time I click on any of its item I re-fill the ArrayList and send notifyOnDataSetChange() to the adapter. But for unknown for me reason it goes out of ArrayList bounds in it's getView() method where it populates its items. I don't understand why this happens. Can you guys explain the theory of getView() invokation so I understand why this going on. Thanks in advance!
Here it is:
class MAdapter extends ArrayAdapter<String> {
public MAdapter(Context context, int textViewResourceId, List<String> objects) {
super(context, textViewResourceId, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.file_explorer_row, null);
} else {
}
String txt = itemsList.get(position); // Out of bounds happens here
if (!txt.equals("")) {
TextView tt = (TextView) v.findViewById(R.id.file_explorer_tv_filename);
tt.setText(txt);
}
return v;
}
itemsList is declared in Outer Class.
change like this
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null)
{
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.file_explorer_row, parent, false);
}
Though i am not getting a clear view of what you are asking..i am assuming that , you are refilling the entire ArrayAdapter again....
So try this.........
Use removeView() on ListView before setting the adapter to it...
Eg:
ListView.removeView();
ListView.setAdapter(yourAdapter);
String txt = itemsList.get(position);
itemsList.get(position) returns a Integer Value and that you are try to store in a String..this might be the reason.
Try this code:
class MAdapter extends BaseAdapter {
List<String> objects;
Context context;
public MAdapter(Context context,List<String> objects) {
super();
this.context=context;
this.objects=objects;
}
public int getCount() {
return objects.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
Holder holder;
LayoutInflater vi;
if (v == null) {
holder=new Holder();
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.file_explorer_row, null);
holder.tt= (TextView) v.findViewById(R.id.file_explorer_tv_filename);
v.setTag(holder);
} else {
holder = (Holder) v.getTag();
}
String txt = objects.get(position); // Out of bounds happens here
if (!txt.equals("")) {
holder.tt.setText(txt);
}
return v;
}
static class Holder{
TextView tt;
}
}