Я относительно новый программист.Я делаю ListView, и когда щелкает один из элементов, два или три отдельных TextViews (в зависимости от ориентации) должны измениться на одну из переменных, включенных в объект «Flick».Предполагая, что я уже настроил методы (.getDirector () возвращает Director) в отдельном файле, как бы я вызвал метод в OnItemClickListener ListView?Кроме того, должна быть возможность удалять элементы нажатием на кнопку, но она сбрасывается при изменении ориентации;как мне с этим бороться?
public class MainActivity extends AppCompatActivity {
ListView listPort;
ListView listLand;
ArrayList<Flick> flicks;
TextView description;
TextView year;
TextView director;
TextView rating;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listPort = findViewById(R.id.listPort);
listLand = findViewById(R.id.listPort);
description = findViewById(R.id.description);
year = findViewById(R.id.year);
director = findViewById(R.id.director);
flicks = new ArrayList<>();
flicks.add(new Flick("Wizard of Oz", "Metro-Goldwyn-Mayer", "VARIOUS","1939", "", "98"));
flicks.add(new Flick("Star Wars", "LucasFilm", "George Lucas", "1977","","93"));
flicks.add(new Flick("Psycho","Paramount", "Alfred Hitchcock", "1960", "", "97"));
flicks.add(new Flick("King Kong", "RKO Pictures", "Merian C. Cooper & Ernest B. Schoedsack", "1933", "", "98"));
flicks.add(new Flick("2001: A Space Odyssey", "Metro-Goldwyn-Mayer", "Stanley Kubrick", "1968", "", "93"));
flicks.add(new Flick("Citizen Kane", "RKO Pictures", "Orson Welles", "1941", "", "100"));
flicks.add(new Flick("Snow White and the Seven Dwarfs", "Walt Disney Pictures", "VARIOUS", "1937", "","98"));
flicks.add(new Flick("Casablanca", "Warner Bros.", "Michael Curtiz", "1942", "", "97"));
flicks.add(new Flick("The Godfather", "Paramount", "Francis Ford Coppola", "1972", "", "98"));
flicks.add(new Flick("Jaws", "Universal", "Steven Spielberg", "1975", "", "97"));
final CustomAdapter customAdapter = new CustomAdapter(this, R.layout.custom_layout, flicks);
if(listPort != null)
listPort.setAdapter(customAdapter);
else
listLand.setAdapter(customAdapter);
listPort.setClickable(true);
listPort.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
description.setText(flicks.get(position).getThoughts());
year.setText("Year Released: " + flicks.get(position).getYear());
director.setText("Director: " + flicks.get(position).getDirector());
rating.setText("Rating" + flicks.get(position).getRating());
customAdapter.notifyDataSetChanged();
}
});
}
public class CustomAdapter extends ArrayAdapter<Flick>
{
Context context;
int resource;
List<Flick> list;
public CustomAdapter(@NonNull Context context, int resource, @NonNull List<Flick> objects) {
super(context, resource, objects);
this.context=context;
this.resource=resource;
list = objects;
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater layoutInflater=(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
final View adapterView=layoutInflater.inflate(resource,null);
TextView title = adapterView.findViewById(R.id.title);
ImageView imageView=adapterView.findViewById(R.id.imageView);
Button remove=adapterView.findViewById(R.id.remove);
title.setText(list.get(position).getTitle());
switch(list.get(position).getStudio())
{
case "Warner Bros.":
imageView.setImageResource(R.drawable.warnerbros);
break;
case "Paramount":
imageView.setImageResource(R.drawable.paramount);
break;
case "Universal":
imageView.setImageResource(R.drawable.universal);
break;
case "LucasFilm":
imageView.setImageResource(R.drawable.lucasfilm);
break;
case "RKO Pictures":
imageView.setImageResource(R.drawable.rko);
break;
case "Walt Disney Pictures":
imageView.setImageResource(R.drawable.disney);
break;
case "Metro-Goldwyn-Mayer":
imageView.setImageResource(R.drawable.mgm);
break;
}
remove.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
list.remove(position);
flicks.remove(position);
notifyDataSetChanged();
LayoutInflater layoutInflater=(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
final View adapterView=layoutInflater.inflate(resource,null);
}
});
return adapterView;
}
}
}