I'm not sure what's wrong with this segment of code:
mylistview = (ListView) findViewById(R.id.list);
CustomAdapter adapter = new CustomAdapter(this, rowItems);
mylistview.setAdapter(adapter);
mylistview.setOnItemClickListener(this);
The line causing an error is mylistview.setAdapter(adapter).
CustomAdapter is a java class with the constructor defined as so:
Context context;
List<RowItem> rowItems;
CustomAdapter(Context context, List<RowItem> rowItems){
this.context = context;
this.rowItems = rowItems;
}
The error I'm getting is:
'setAdapter(android.widget.ListAdapter)' in 'android.widget.ListView' cannot be applied to '(com.name.app.CustomAdapter)'
I have no idea what's causing this error and would greatly appreciate some insight as to why I can't set the adapter of my custom adapter to the listview. Thanks.
Try this one
public class SpinnerAdapter extends BaseAdapter{
Context context;
ArrayList<String> list;
LayoutInflater layoutInflater;
SpinnerAdapter(Context context, ArrayList<String> list)
{
layoutInflater = LayoutInflater.from(context);
this.list = list;
this.context = context;
}
#Override
public int getCount()
{
return list.size();
}
#Override
public Object getItem(int position)
{
return list.get(position);
}
#Override
public long getItemId(int position)
{
return list.indexOf(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
convertView = layoutInflater.inflate(R.layout.spinner_s,null);
TextView client = (TextView) convertView.findViewById(R.id.client);
client.setText(list.get(position).toString());
return convertView;
}
The setAdapter requires android.widget.ListAdapter. It looks like you CustomAdapter class needs to implement the ListAdapter interface
Related
im working on a tank-game. in this tank-game i want to display the items which the player can use(shild,firework,atombomb)i want to realise this with a listview and a arraya-dapter. i've made a customized ArrayAdapter like i found it in much answers but it didnt work. The list is not there after i set the adapter. After i debug the project i came to the conclusion that the getView-method in the adapter class isnt called
why?
This is my adapter class
public class Adapter extends ArrayAdapter {
private Context context;
private ArrayList<Item> items;
public Adapter(#NonNull Context context, ArrayList<Item> items) {
super(context, R.layout.listview_item);
this.context = context;
this.items = items;
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = new ViewHolder();
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listview_item, parent, false);
viewHolder.imageView = convertView.findViewById(R.id.imageView);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.imageView.setImageBitmap(items.get(position).getImageView());
return convertView;
}
static class ViewHolder{
ImageView imageView;
}
}
Here i want to set the adapter
public void createList(ListView listView){
Adapter adapter = new Adapter(context,tanks.get(currentTank).getItems());
listView.setAdapter(adapter);
}
public Adapter(#NonNull Context context, ArrayList<Item> items) {
super(context, R.layout.listview_item);
this.context = context;
this.items = items;
}
Instead of this try this :
public MyCustomListAdapter(Context context,int resource, ArrayList<Item> items)
{
super(context,resource,list);
this.context= context;
this.resource=resource;
this.list=list;
}
Also in your main class write this code:
Adapter adapter = new Adapter(context,R.layout.<Thename of your layout>,tanks.get(currentTank).getItems());
I'm creating a ListView with a custom adapter.
In the customer adapter class, I'm passing a Context object to its super class and also passing a Context object to the MainActivity while creating the object for the Adapter class.
It shows the following error
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mylistpractice/com.mylistpractice.MainActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
Now I pass getApplicationContext() rather than the Context object while passing the argument from MainActivity and it doesn't show any error.
I am confused about the difference between passing a Context object and passing getApplicationContext().
But it's not showing any data in the ListView.
This is the Adapter class
public class Myadapter extends ArrayAdapter<Data> {
ArrayList<Data> arraydata;
Context context;
public Myadapter(ArrayList<Data> arraydata,Context context) {
super(context,R.layout.content,arraydata);
this.arraydata=arraydata;
this.context=context;
}
#Override
public int getCount() {
return 0;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Data data=getItem(position);
Viewholder viewholder;
View vi=convertView;
if(vi==null) {
viewholder=new Viewholder();
LayoutInflater inflater = LayoutInflater.from(getContext());
vi = inflater.inflate(R.layout.content, parent, false);
viewholder.name=(TextView)vi.findViewById(R.id.name);
viewholder.id=(TextView)vi.findViewById(R.id.id);
vi.setTag(viewholder);
} else {
viewholder=(Viewholder)vi.getTag();
}
viewholder.name.setText(data.getName());
viewholder.id.setText(data.getId());
return vi;
}
public class Viewholder {
TextView id;
TextView name;
}
}
this is my MainActivity
public class MainActivity extends AppCompatActivity {
ListView list;
ArrayList<Data> arraydata=new ArrayList<>();
Myadapter myadapter;
Context context;
Data data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=(ListView)findViewById(R.id.list);
setList();
myadapter = new Myadapter(arraydata,MainActivity.this);
list.setAdapter(myadapter);
}
public void setList() {
for(int i=0;i<15;i++) {
String s,id;
data = new Data();
data.setName("Farhana:::"+i);
s = data.getName();
data.setId("Id:::"+i);
id = data.getId();
arraydata.add(data);
Log.e("DAtA:--",s);
Log.e("ID:--",id);
}
}
}
but not showing anydata in listview
Because you told it not to create any views.
#Override
public int getCount() {
return 0;
}
You are using an ArrayAdapter. You don't need to implement getCount, but if you do, return the size of the list
#Override
public int getCount() {
return arraydata == null ? 0 : arraydata.size();
}
Anyways, regarding your question, tend to use MainActivity.this like you have done for the Context (Activities are Contexts).
If your error is here
LayoutInflater inflater = LayoutInflater.from(getContext());
Then, that could instead be
LayoutInflater inflater = LayoutInflater.from(this.context);
which you got the context from the constructor
you are overriding method getCount(); and it is returning zero, you have to return the size of the array-list.
I have a special class, a custom adapter for my ListView and I need to get some data from another Activity. But my implementation of the method GetIntent()GetExtras() isn't working. What is wrong?
Here is my custom adapter code:
public class CustomAdapter extends ArrayAdapter<String> {
int myColor,myWidth;
private final Context context;
private final String[] values;
public CustomAdapter(Context context, String[] values) {
super(context, R.layout.list_item, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// return super.getView(position, convertView, parent);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.ColorTextButton);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageViewIcon);
TextView textView1 = (TextView) rowView.findViewById(R.id.HelpButton);
textView.setText(values[position]);
String s = values[position];
System.out.println(s);
if (s.equals("Monday")) {
imageView.setImageResource(R.drawable.arrow2);
textView.setBackgroundColor(Color.YELLOW);
} else if (s.equals("Wednesday")) {
imageView.setImageResource(R.drawable.arrow2);
textView1.setBackgroundColor(Color.GREEN);
} else if (s.equals("Friday")) {
imageView.setImageResource(R.drawable.arrow2);
} else {
imageView.setImageResource(R.drawable.arrow);
}
return rowView;
}
}
If you want the data to be accessable From a few activities you should think about use a singleton data Class. so in stead of trying to get pass the data from your adapter to a second activity both the adaper and the Activity use the same method to get the data class
public class DataProvider {
private static DataProvider instance;
public static DataProvider getInstance()
{
if(null == instance){instance = new DataProvider();}
return instnace;
}
public String[] getObjects(){
return this.myStringArray;
}
// add more methods in here to retrieve and count your data as you need
}
then in your Activity you can
// somewhere
myStingArrayInMyActivity = DataProvider.getInstance().getObjects()
also in you adapter
you can do
public class CustomAdapter extends ArrayAdapter<String> {
int myColor,myWidth;
private final Context context;
private final String[] values;
public CustomAdapter(Context context, String[] values) {
super(context, R.layout.list_item, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// your code
textView.setText(DataProvider.getInstance.getObjects[position]);
String s = DataProvider.getInstance.getObjects[position];
/// your code
return rowView;
}
}
I am trying to create an onDisimiss listener to my app as for now this is what i have
The activity
public class listview_test extends Activity {
ListView list;
String[] web = {
"Google Plus",
"Twitter",
"Windows",
} ;
Integer[] imageId = {
R.drawable.ic_launcher,
R.drawable.icon,
R.drawable.ic_launcher,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_test);
final CustomList adapter = new
CustomList(listview_test.this, web, imageId);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(listview_test.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
}
});
SwipeDismissListViewTouchListener touchListener =
new SwipeDismissListViewTouchListener(
list,
new SwipeDismissListViewTouchListener.DismissCallbacks() {
#Override
public boolean canDismiss(int position) {
return true;
}
#Override
public void onDismiss(ListView listView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
adapter.remove(adapter.getItem(position));
}
adapter.notifyDataSetChanged();
}
}
);
list.setOnTouchListener(touchListener);
// Setting this scroll listener is required to ensure that during ListView scrolling,
// we don't look for swipes.
list.setOnScrollListener(touchListener.makeScrollListener());
}
}
And the custom adapter
public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final String[] web;
private final Integer[] imageId;
public CustomList(Activity context,
String[] web, Integer[] imageId) {
super(context, R.layout.weather_item, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.weather_item, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.city);
ImageView imageView = (ImageView) rowView.findViewById(R.id.info_image);
txtTitle.setText(web[position]);
imageView.setImageResource(imageId[position]);
return rowView;
}
For some reason every time i am swiping an item to dismiss it force closes and gives me this exception
java.lang.UnsupportedOperationException
In this line
adapter.remove(adapter.getItem(position));
java.lang.UnsupportedOperationException
is thrown when you back an Adapter by an array or non-modifiable List. Since you cannot change their size, deleting is impossible.
Instead, modify your Adapter so it accepts a List instead of an array and make sure that the List you pass off is fully flexible.
Something like
List <String> web = new ArrayList <String> ();
web.add ("Google Plus");
web.add ("Twitter");
//etc.
is enough to ensure a flexible list.
This means that your CustomList adapter should also call up to the superclass constructor that also accepts in a List, which in this case is
public ArrayAdapter (Context context, int resource, List<T> objects)
For more information refer to the ArrayAdapter documentation.
why in every example i see that extends the class ArrayAdapter it overrides the constructor that accepts T[] objects and references its own private variable to that and uses the referenced variable in the override of getView?
how can this be dynamic adding of objects?
will values be updated when a new item is added to the ArrayAdapter using add method?
can i instead of implementing my own adapter just use addheaderview in listview?
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MySimpleArrayAdapter(Context context, String[] values) {
super(context, R.layout.rowlayout, 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.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
textView.setText(values[position]);
// change the icon for Windows and iPhone
String s = values[position];
if (s.startsWith("iPhone")) {
imageView.setImageResource(R.drawable.no);
} else {
imageView.setImageResource(R.drawable.ok);
}
return rowView;
}
}