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

Posted

技术标签:

【中文标题】如何在 ListView onClickListener 中获取调用对象方法?【英文标题】:How to get call object methods in ListView onClickListener? 【发布时间】:2019-05-08 16:19:01 【问题描述】:

我是一个相对较新的程序员。我正在制作一个 ListView,当单击其中一项时,两个或三个单独的 TextView(取决于方向)应更改为对象“Flick”中包含的变量之一。假设我已经在一个单独的文件中设置了方法(.getDirector() 返回导演),我将如何在 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;
        

    

【问题讨论】:

不知道是不是看懂了你根本就没有调用“OnItemClickListener”方法,点击item的时候系统会调用这个方法。你只需要设置监听器告诉它应该做什么。对于第二个问题,当方向改变时,整个活动被破坏和重建,所以基本上它是从头开始的。您必须保存相关数据,例如在 instanceState 中,然后使用这些数据重新初始化 UI。我建议你看看这里developer.android.com/guide/components/activities/… 不是,我的意思是调用Object类的方法(都是字符串变量,我只需要一个例子) 【参考方案1】:

暂时忘记OnItemClickListener

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

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

您可以在列表行中的任何TextViews 中设置此String

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

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

【讨论】:

但该项目要求用户能够选择项目,以便 TextViews 与 ListView 分开显示附加信息。 你打算把你的TextViews放在哪里?在另一个 Activity 中还是在与 ListView 相同的 Activity 中?请提供更多细节或项目逻辑,以便我们研究并提出建议。

以上是关于如何在 ListView onClickListener 中获取调用对象方法?的主要内容,如果未能解决你的问题,请参考以下文章

从数据库检索数据到列表视图失败

如何在适配器的 Kotlin 中提供长点击监听器

onActivityResult 方法调用按钮单击[重复]

如何得到listview中有多少列数据?

如何在 ListView 的项目构建器之前添加初始小部件,或者如何在 ListView.itemBuilder 之上注入小部件?

WPF:如何在拖动 ListView 项目时允许在 ListView 中使用鼠标滚轮滚动?