Please help me resolve this issue. I'm making a drawer for my app and I'm receiving these errors from ADT:
The constructor Object(Context, int, List) is undefined
The method getView(int, View, ViewGroup) of type CustomDrawerAdapter
must override or implement a supertype method
This is my code for CustomDrawerAdapter class:
package com.example.ico;
import java.util.List;
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.ImageView;
import android.widget.TextView;
public class CustomDrawerAdapter {
Context context;
List<DrawerItem> drawerItemList;
int layoutResID;
public CustomDrawerAdapter(Context context, int layoutResourceID, List<DrawerItem> listItems) {
super(context, layoutResourceID, listItems);
this.context = context;
this.drawerItemList = listItems;
this.layoutResID = layoutResourceID;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
DrawerItemHolder drawerHolder;
View view = convertView;
if (view == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
drawerHolder = new DrawerItemHolder();
view = inflater.inflate(layoutResID, parent, false);
drawerHolder.ItemName = (TextView) view
.findViewById(R.id.drawer_itemName);
drawerHolder.icon = (ImageView) view.findViewById(R.id.drawer_icon);
view.setTag(drawerHolder);
} else {
drawerHolder = (DrawerItemHolder) view.getTag();
}
DrawerItem dItem = (DrawerItem) this.drawerItemList.get(position);
drawerHolder.icon.setImageDrawable(view.getResources().getDrawable(
dItem.getImgResID()));
drawerHolder.ItemName.setText(dItem.getItemName());
return view;
}
private static class DrawerItemHolder {
TextView ItemName;
ImageView icon;
}
}
To fix all your problems, you need to make your class extend ArrayAdapter.
public class CustomDrawerAdapter extends ArrayAdapter {
Related
package com.example.test;
import android.content.Context;
import android.provider.DocumentsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class ProgramAdapter extends ArrayAdapter<String> {
Context context;
int[] images;
String[] programName;
String[] programDescription;
public ProgramAdapter( Context context, String [] programName, int[] images, String[] programDescription) {
super(context, R.layout.single_item, R.id.textView1, programName);
this.context = context;
this.images = images;
this.programName = programName;
this.programDescription = programDescription; }
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View singleItem = convertView;
ProgramViewHolder holder = null;
if(singleItem == null){
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
singleItem = LayoutInflater.inflate(R.layout.single_item, parent, false);
holder = new ProgramViewHolder(singleItem);
singleItem.setTag(holder); }
else{
holder = (ProgramViewHolder) singleItem.getTag(); }
holder.itemImage.setImageResource(images[position]);
holder.programTitle.setText(programName[position]);
holder.programDescription.setText(programDescription[position]);
singleItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "You clicked:" + programName[position], Toast.LENGTH_SHORT ).show();
}
});
return singleItem;
}
}
I am trying to create a Listview image with text in Android Studio. There is only one error shown up in the following line:
singleItem = LayoutInflater.inflate(R.layout.single_item, parent, false);
I have 3 classes: MainActivity, ProgramAdapter and ProgramViewHolder
Non-static method 'inflate(int, android.view.ViewGroup, boolean)' cannot be referenced from a static context
You are not using layoutInflater object
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater is a class.
singleItem = LayoutInflater.inflate(R.layout.single_item, parent, false);
Change it to
singleItem = layoutInflater.inflate(R.layout.single_item, parent, false);
I'm trying to pass a String list from my custom array class to another class, but for some reason when I try to access the list, it always seems empty.
Here is my Custom Adapter class:
import android.app.Activity;
import android.content.Context;
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.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class CustomAdapter extends ArrayAdapter {
private final List<Model> orders;
private final Context context;
boolean checkAll_flag = false;
boolean checkItem_flag = false;
public List<String> selectedItems = new ArrayList<>();
public CustomAdapter(#NonNull Context context, List<Model> orders) {
super(context, R.layout.company_list_order, orders);
this.context = context;
this.orders = orders;
}
static class ViewHolder {
protected TextView orderText;
protected CheckBox checkOrder;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
ViewHolder viewHolder;
selectedItems = new ArrayList<>();
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.company_list_order, parent, false);
viewHolder = new ViewHolder();
viewHolder.orderText = (TextView) convertView.findViewById(R.id.textViewList);
viewHolder.checkOrder = (CheckBox) convertView.findViewById(R.id.listCheckBox);
viewHolder.checkOrder.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
orders.get(getPosition).setSelected(buttonView.isChecked());
if (orders.get(getPosition).isSelected()) {
selectedItems.add(orders.get(getPosition).getName());
Log.d("Add", "Item added: " + orders.get(getPosition).getName());
Log.d("Size", "Selected items " + selectedItems.size());
} else {
selectedItems.remove(orders.get(getPosition).getName());
Log.d("Remove", "Item removed: " + orders.get(getPosition).getName());
Log.d("Size", "Selected items " + selectedItems.size());
}
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.textViewList, viewHolder.orderText);
convertView.setTag(R.id.listCheckBox, viewHolder.checkOrder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkOrder.setTag(position);
viewHolder.orderText.setText(orders.get(position).getName());
viewHolder.checkOrder.setChecked(orders.get(position).isSelected());
return convertView;
}
public List<String> getListItems() {
return selectedItems;
}
}
At the bottom I have the method returning the list. How should I access this in another class?
Probably because you create a new ArrayList everytime getView() is called, which is called a lot. And when you call the method getListItems() the method getView() is called before due to scrolling the listview or something. Remove the line
selectedItems = new ArrayList<>();
from getView() method.
Another option is to make a class with static variables to hold the value during the applications lifetime
public class GlobalVars {
public static List<String> selectedItems;
}
I created a simple android application with a ListView in it. I have my custom list adapter below:
package com.example.android.efaas;
import java.util.List;
import com.example.android.R;
import com.example.android.efaas.bean.ListItem;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomListAdapter extends BaseAdapter {
LayoutInflater inflater = null;
Context ctx;
List<ListItem> data;
public CustomListAdapter(Activity activity, List<ListItem> data){
ctx = activity;
this.data = data;
inflater = ( LayoutInflater )ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int pos) {
return pos;
}
#Override
public long getItemId(int pos) {
return pos;
}
#Override
public View getView(int pos, View view, ViewGroup vgroup) {
View rowView = inflater.inflate(R.layout.list_menu, null);
ImageView img = (ImageView) rowView.findViewById(R.id.list_menu_image);
TextView title = (TextView) rowView.findViewById(R.id.list_menu_title);
ListItem item = data.get(pos);
img.setImageResource(item.getId());
title.setText(item.getTitle());
return rowView;
}
public ListItem getListItem(int pos){
return data.get(pos);
}
}
I have a public method getListItem, I tried to call it during ListView onClick but somehow I cant seem to make it work. Here's the image of the problem:
How do I resolve this?
You should cast adapter.getAdapter() to CustomListAdapter class then
the compile time error is due of the fact that adapter is not your CustomListAdapter. What you can do is cast the return value of adapter.getAdapter() to CustomListAdapter. Conversely you should use what's already available to retrieve the object at position. Like getItemAtPosition(int)
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.actionbarsherlock.app.SherlockFragment;
public class FragmentTab2 extends SherlockFragment {
private String[]names = {"Video", "Audio"};
private String[]urls = {"http://c172200.r0.cf3.rackcdn.com/104824.mp4", "http://c172200.r0.cf3.rackcdn.com/106973.mp4"};
#Override
public View onCreateView(LayoutInflater inflater1, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from fragmenttab2.xml
View view = inflater1.inflate(R.layout.fragmenttab2, container, false);
return view;
ListView lv = (ListView)findViewById(R.id.List);
VideoAdapter adapter = new VideoAdapter(this, names, urls);
lv.setAdapter(adapter);
}
}
I'm to make a listview in a fragmenttab. I have the created the interface using a tutorial. http://www.youtube.com/watch?v=54wC1lgmbKQ In the video you can see application. I'm not sure where to declare and to declare the adapter. Right now i'm declaring in in the fragmenttab1 activity it self, but this causes errors to appear, saying that "The Constructor in undefined" and that "The method findViewById(int) in undefined within the type FragmentTab2". Can anyone help me? This would really boost my school project. Thanks in advance!
The code of the adapter:
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
public class VideoAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] names;
private final String[] urls;
public VideoAdapter(Context context, String[] names, String[] urls) {
super(context, R.layout.videorow, names);
this.context = context;
this.names = names;
this.urls = urls;
}
#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.videorow, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.textView2);
Button button = (Button) rowView.findViewById(R.id.grootheden);
textView.setText(names[position]);
button.setTag(urls[position]);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse((String) v.getTag()), "video/mp4");
context.startActivity(intent);
}
});
return rowView;
}
}
Try this:
ListView lv = (ListView)findViewById(R.id.List);
Should be:
ListView lv = (ListView) view.findViewById(R.id.List);
EDIT:
Also, you need to return the view at the END of your onCreate();
return view;
Your code, fixed:
public class FragmentTab2 extends SherlockFragment {
private String[]names = {"Video", "Audio"};
private String[]urls = {"http://c172200.r0.cf3.rackcdn.com/104824.mp4", "http://c172200.r0.cf3.rackcdn.com/106973.mp4"};
#Override
public View onCreateView(LayoutInflater inflater1, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from fragmenttab2.xml
View view = inflater1.inflate(R.layout.fragmenttab2, container, false);
ListView lv = (ListView) view.findViewById(R.id.List);
VideoAdapter adapter = new VideoAdapter(this, names, urls);
lv.setAdapter(adapter);
return view;
}
}
I would like to load only selected fields from my ArrayList to ListView.
I couldn't find that example so I ask.
I have an ArrayList of a structure as follows:
ArrayList<LogInfo> logInfoArray
Where LogInfo class have fields as follows:
public ArrayList<Point[][]> strokes;
public LinkedList<byte[]> codes;
public int[] times; //contains fields of calendar class
I want to put in my ListView in each row selected fields from "times" and "codes"
How can I achieve that? I would like to use a cursor, if possible.
You can use a custom adapter extending from ArrayAdapter to which you can pass the ArrayList<LogInfo> .
You can then ovverride the getView(..) method of the Adapter to set the fields you want in the row of the Listview .
UPDATE
From this example at Android Custom Adapters
import java.util.List;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
public class InteractiveArrayAdapter extends ArrayAdapter<LogInfo> {
private final List<LogInfo> list;
private final Activity context;
public InteractiveArrayAdapter(Activity context, List<LogInfo> list) {
super(context, R.layout.rowbuttonlayout, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text1, text2;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.rowbuttonlayout, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text1 = (TextView) view.findViewById(R.id.label1);
viewHolder.text2 = (TextView) view.findViewById(R.id.label2);
view.setTag(viewHolder);
} else {
view = convertView;
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text1.setText(list.get(position).getName1());
holder.text2.setText(list.get(position).getName2());
return view;
}
}