jackson jackson - 1 year ago 124
Android Question

Adapter error on a ListView

I want to add a new layout with some specific data to a ListView.

I made a class which extendeds a BaseAdapter, but when I try to put my data and Context (this) in my new class, it shows an error

package com.bignner.hotel;

public class MainActivity extends AppCompatActivity {
private Context context;
private ListView Placename;
protected void onCreate(Bundle savedInstanceState) {

Button btn= (Button)findViewById(R.id.btnAdd);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Placename =(ListView) findViewById(R.id.ListView);

RadioGroup RG=(RadioGroup)findViewById(R.id.radiogrop);
int SelectedId= RG.getCheckedRadioButtonId();
RadioButton RB= (RadioButton)findViewById(SelectedId);
String textRB=RB.getText().toString();
int place=0;

switch (textRB){
case "Gust-house":
case "Apartment":
case "Hotel":
TextView placeName=(TextView)findViewById(R.id.editTextName);
String name=placeName.getText().toString();
TextView placeAddress=(TextView)findViewById(R.id.editTextAddress);
String address=placeAddress.getText().toString();

List<HotelEntry> tourPlace = new ArrayList<HotelEntry>();
tourPlace.add(new HotelEntry(name,place));
Myadapter adapter = new Myadapter(this,tourPlace);


And my class:

public class Myadapter extends BaseAdapter {

private Context context;
private List<HotelEntry> hotels;

public int getCount() {
return hotels.size();

public Object getItem(int position) {
return hotels.get(position);

public long getItemId(int position) {
return position;

public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.my_layout,parent,false);
TextView placeName = (TextView) convertView.findViewById(R.id.textViewName);
ImageView LetterIcon = (ImageView) convertView.findViewById(R.id.imageViewIcon);
return convertView;


The error is on this line:

Myadapter adapter = new Myadapter(this,tourPlace);

I also replaced MainActivity.this with this, but still I see that error.

I don't know why and how to solve it.

I'd appreciate to get some help.


enter image description here

Answer Source

You haven't applied constructor of Myadapter class. So you need to create it.

public Myadapter(Context context, List<HotelEntry> list) { ... }

And bring MainActivity.this back - without it you are trying to place your OnClickListener as constructor parameter.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download