Android Listview ,点击获取项的名称在列表中

发布时间: 2016/9/26 3:53:24
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有 listView 的电影。当用户点击 movie ,我想要的名称显示在该主任 Toast

Toast工作。我只是不确定如何获取名称。

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;

        if(convertView == null){
            holder = new ViewHolder();
            convertView = inflater.inflate(resource, null);
            holder.ivMovieIcon = (ImageView)convertView.findViewById(R.id.ivIcon);
            holder.tvMovie = (TextView)convertView.findViewById(R.id.tvMovie);
            holder.tvTagline = (TextView)convertView.findViewById(R.id.tvTagline);
            holder.tvYear = (TextView)convertView.findViewById(R.id.tvYear);
            holder.tvDuration = (TextView)convertView.findViewById(R.id.tvDuration);
            holder.tvDirector = (TextView)convertView.findViewById(R.id.tvDirector);
            holder.rbMovieRating = (RatingBar)convertView.findViewById(R.id.rbMovie);
            holder.tvCast = (TextView)convertView.findViewById(R.id.tvCast);
            holder.tvStory = (TextView)convertView.findViewById(R.id.tvStory);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            public void onClick(final View v) {
                Toast.makeText(MainActivity.this, "Name of Director",
                        Toast.LENGTH_LONG).show();
            }
        });


        final ProgressBar progressBar = (ProgressBar) convertView.findViewById(R.id.progressBar);

        ImageLoader.getInstance().displayImage(movieModelList.get(position).getImage(), holder.ivMovieIcon, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                progressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {
                progressBar.setVisibility(View.GONE);
            }
        });

        holder.tvMovie.setText(movieModelList.get(position).getMovie());
        holder.tvTagline.setText(movieModelList.get(position).getTagline());
        holder.tvYear.setText("Year: " + movieModelList.get(position).getYear());
        holder.tvDuration.setText("Duration: " + movieModelList.get(position).getDuration());
        holder.tvDirector.setText("Director: " + movieModelList.get(position).getDirector());

        // rating bar
        holder.rbMovieRating.setRating(movieModelList.get(position).getRating()/2);

        StringBuffer stringBuffer = new StringBuffer();
        for(MovieModel.Cast cast : movieModelList.get(position).getCastList()){
            stringBuffer.append(cast.getName() + ", ");
        }

        holder.tvCast.setText("Cast:" + stringBuffer);
        holder.tvStory.setText(movieModelList.get(position).getStory());
        return convertView;
    }

    class ViewHolder{
        private ImageView ivMovieIcon;
        private TextView tvMovie;
        private TextView tvTagline;
        private TextView tvYear;
        private TextView tvDuration;
        private TextView tvDirector;
        private RatingBar rbMovieRating;
        private TextView tvCast;
        private TextView tvStory;
    }

在此先感谢

解决方法 1:

它是很简单......

 convertView.setOnClickListener(new View.OnClickListener() {
            public void onClick(final View v) {
                Toast.makeText(MainActivity.this, movieModelList.get(position).getDirector(),
                        Toast.LENGTH_LONG).show();
            }
        });
官方微信
官方QQ群
31647020