jackson jackson - 2 months ago 27
Android Question

Adapter error for listview in android studio

i want to add new layout with specific data to a listview in android studio, i make a class that is extended with Basicadapter but when i want to put my data and Context(this) in my new class it shows error

package com.bignner.hotel;

public class MainActivity extends AppCompatActivity {
private Context context;
private ListView Placename;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btn= (Button)findViewById(R.id.btnAdd);
btn.setOnClickListener(new View.OnClickListener() {
@Override
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":
place=R.drawable.g;
break;
case "Apartment":
place=R.drawable.a;
break;
case "Hotel":
place=R.drawable.h;
break;
}
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);
Placename.setAdapter(adapter);

}
});
}


}

And my class is definde:

public class Myadapter extends BaseAdapter {

private Context context;
private List<HotelEntry> hotels;

@Override
public int getCount() {
return hotels.size();
}

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

@Override
public long getItemId(int position) {
return position;
}

@Override
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);
placeName.setText(hotels.get(position).getHotelName());
ImageView LetterIcon = (ImageView) convertView.findViewById(R.id.imageViewIcon);
LetterIcon.setImageResource(hotels.get(position).getTypeIcon());
return convertView;

}


}

Android studio shows me error in line:

Myadapter adapter = new Myadapter(this,tourPlace);


and i also replace MainActivity.this with this but again i see error i don't know why and how to solve it. i'd appreciate to help me

Error:
enter image description here

Answer

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.