Kong Kong - 4 months ago 32
Android Question

Android Listview, on click get name of item in list

I have a listView with movies. When user clicks on a movie, i would like the name of that director to show up in the Toast.

The Toast is working. I am just not sure how to get the name.

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


Thanks in advance

Answer

it is very simple.....

 convertView.setOnClickListener(new View.OnClickListener() {
            public void onClick(final View v) {
                Toast.makeText(MainActivity.this, movieModelList.get(position).getDirector(),
                        Toast.LENGTH_LONG).show();
            }
        });