我创建了一个 Activity ,创建了 ListView 并设置了 setItemsCanForus()
方法设置为 true,以便适配器中的每个 View 都可以有自己的 onclicklisteners。我设法列出了列表,但现在 View 没有响应点击。
这是 Activity 的一部分。
private ArrayList<String> filePath;
private ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_video);
**filePath = new ArrayList<>();
lv = (ListView) findViewById(R.id.find_video_list);
fetchVideoList();
lv.setItemsCanFocus(true);**
}
private void fetchVideoList() {
String[] projection = {
MediaStore.Video.Thumbnails._ID
};
// Create the cursor pointing to the SDCard
Cursor cursor = getContentResolver().query(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
MediaStore.Video.Thumbnails.VIDEO_ID);
assert cursor != null;
cursor.close();
filePath = getFilePaths();
VideoFromDeviceItem adapter = new VideoFromDeviceItem(FindVideoActivity.this, filePath);
**lv.setAdapter(adapter);**
}
这是扩展 ArrayAdapter<String>
的类类。
public class VideoFromDeviceItem extends ArrayAdapter<String> {
private final Activity context;
private final ArrayList<String> filePaths;
public VideoFromDeviceItem(Activity context, ArrayList<String> filePaths) {
super(context, R.layout.video_item, filePaths);
this.context = context;
this.filePaths = filePaths;
}
@Override
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.upload_list, null, true);
VideoView videoItem = (VideoView) rowView.findViewById(R.id.videoItem);
TextView tvFilePath = (TextView) rowView.findViewById(R.id.tvFilePath);
TextView tvVideoRunningTime = (TextView) rowView.findViewById(R.id.tvVideoRunningTime);
videoItem.setVideoPath(filePaths.get(position));
videoItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, position, Toast.LENGTH_SHORT).show();
}
});
// remove the file path and simply get the file name.
String fullPath = filePaths.get(position);
String shortName = new File(fullPath).getName();
tvFilePath.setText(shortName);
// get the running time of the video.
long runningTime = getVideoRunningTime(filePaths.get(position));
String strRunningTime = fromMillisecToFormattedTime(runningTime);
tvVideoRunningTime.setText(strRunningTime);
return rowView;
}
正如你在上面看到的,我设置了一个 OnClickListener()
到带有 toast 消息的视频 View ,只是为了测试听众是否真的在响应。并且没有来自点击的响应。
最佳答案
尝试改变这个:
private final Activity context;
为此:
private final Context context;
还有你的构造函数:
public VideoFromDeviceItem(Context context, ArrayList<String> filePaths) {
super(context, R.layout.video_item, filePaths);
this.context = context;
this.filePaths = filePaths;
}
编辑
您可以改用它:
videoItem.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(context, String.valueOf(position), Toast.LENGTH_SHORT).show();
return false;
}
});
Why the
onTouchListener()
notonClickListener()
in this case?
onClickListener()
存在一些错误,因为它是一个 VideoView
...所以执行 onClickListener()
的最佳方法是onTouchListener()
但是 如果你想要 onClickListener()
只需在你的 中添加一个
或任何地方,您可以在其上设置 Button
VideoViewsetOnClickListener()
。
关于Android:ArrayAdapter 中的 onClickListener 没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34161615/