自定义ListView使用ArrayAdapter,OnClick事件

时间:2014-01-06 04:30:42

标签: android android-listview

我有一个使用Arrayadapter的Custom ListView,它有一部分视图用于处理onClick事件。我在下面的代码中设置了它。当用户点击视图时,它会调出YouTube视频。我需要知道的是每个单独的视图点击都有不同的视频播放。这是我的代码

 public class CustomList extends ArrayAdapter<RowItem> {

Context context;
private static final String[] videoId = {"-Uwjt32NvVA", "J7-8IteUvt8", "XjwZAa2EjKA",};

public CustomList(Context context, int resourceId,
        List<RowItem> items) {
    super(context, resourceId, items);
    this.context = context;
}

/*private view holder class*/
private class ViewHolder {
   // ImageView imageView;
    TextView txtTitle;
    TextView txtCareer;
    TextView txtSeason;
    TextView txtGame;
    RelativeLayout highlight;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    RowItem rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_row, null);
        holder = new ViewHolder();
        holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
        holder.txtCareer = (TextView) convertView.findViewById(R.id.career);
        holder.txtSeason = (TextView) convertView.findViewById(R.id.season);
        holder.txtGame = (TextView) convertView.findViewById(R.id.game);
        holder.highlight = (RelativeLayout) convertView.findViewById(R.id.highlight);

        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();
    holder.txtTitle.setText(rowItem.getTitle());
    holder.txtCareer.setText(rowItem.getCareer());
    holder.txtSeason.setText(rowItem.getSeason());
    holder.txtGame.setText(rowItem.getGame());
    //holder.imageView.setImageResource(rowItem.geturl());

    holder.highlight .setOnClickListener(new OnClickListener() {

        public void onClick(View v) {


            Intent intent = new Intent(Intent.ACTION_VIEW,  Uri.parse("vnd.youtube:"+videoId)); 
            intent.putExtra("VIDEO_ID", videoId); 
            context.startActivity(intent); 

        }
    });

    return convertView;
}


}

一切正常我只是不确定当用户在列表中选择不同的视图时如何播放不同的视频。

2 个答案:

答案 0 :(得分:1)

 Intent intent = new Intent(Intent.ACTION_VIEW,  Uri.parse("vnd.youtube:"+videoId[position])); 
 intent.putExtra("VIDEO_ID", videoId[position]); 
 context.startActivity(intent); 

答案 1 :(得分:0)

ss=(String) ((TextView) view).getText();

Intent intent = new Intent(Intent.ACTION_VIEW,  Uri.parse("vnd.youtube:"+ss)); 
context.startActivity(intent);