如何在ListView onClickListener中获取调用对象方法?

时间:2018-12-06 22:04:32

标签: android android-studio listview orientation onitemselectedlistener

我是一个相对较新的程序员。我正在制作一个ListView,单击其中一项时,两个或三个单独的TextView(取决于方向)应更改为对象“ Flick”中包含的变量之一。假设我已经在单独的文件中设置了方法(.getDirector()返回director),那么如何在ListView的OnItemClickListener中调用该方法?另外,还应该具有通过单击按钮删除项目的功能,但是当方向改变时它会重置。我该怎么处理?

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;
        }

    }
}

1 个答案:

答案 0 :(得分:0)

暂时忘记OnItemClickListener

getView()类的Adaptor方法中,您可以从列表中获取所需的任何Flick对象,如下所示:

Flick flick = (Flick) list.get(position);
String director = flick.getDirector();

您可以在列表行的String中的任何一个TextView上进行设置。

TextView title = .....
title.setText(director);

基本上,getView()方法负责列表中的 每个 Flick对象。