我想在GridView布局中显示我的特定文件夹vedios
。
但是我在GridView布局中得到了黑色物品 Screenshot showing GridView layout
这是我的课程,用于从内部存储器获取数据。
public class Spacecraft {
String name;
boolean Checked=false;
private Uri uri;;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Uri getUri() {
return uri;
}
public void setUri(Uri uri) {
this.uri = uri;
}
public boolean isChecked(){
return Checked;
}
public void toggleChecked(){
Checked = !Checked;
}
}
这是我的Adapter Class
。
public class VedioAdapter extends BaseAdapter {
Context c;
ArrayList<Spacecraft> spacecrafts;
public VedioAdapter(Context c, ArrayList<Spacecraft> spacecrafts) {
this.c = c;
this.spacecrafts = spacecrafts;
}
@Override
public int getCount() {
return spacecrafts.size();
}
@Override
public Object getItem(int i) {
return spacecrafts.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(view==null)
{
//INFLATE CUSTOM LAYOUT
view= LayoutInflater.from(c).inflate(R.layout.whatsapp_vedio,viewGroup,false);
}
final Spacecraft s= (Spacecraft) this.getItem(i);
if(s.isChecked()){
view.setBackgroundColor(Color.parseColor("#06ad94"));
}
else view.setBackgroundColor(Color.TRANSPARENT);
// TextView nameTxt= (TextView) view.findViewById(R.id.textView);
VideoView vedio=(VideoView)view.findViewById(R.id.vedio);
vedio.setVideoURI(s.getUri());
//BIND DATA
// nameTxt.setText(s.getName());
return view;
}
}
这是我的VedioLayout活动类。
public class VedioGrid extends AppCompatActivity {
Spacecraft s;
ArrayList<Spacecraft> list = new ArrayList<>();
ArrayList<Spacecraft> list_items = new ArrayList<>();
int count = 0;
public ArrayList<Spacecraft> getPlayList(String rootPath) {
ArrayList<Spacecraft> fileList = new ArrayList<>();
try {
File rootFolder = new File(rootPath);
File[] files = rootFolder.listFiles(); //here you will get NPE if directory doesn't contains any file,handle it like this.
for (File file : files) {
if (file.isDirectory()) {
if (getPlayList(file.getAbsolutePath()) != null) {
fileList.addAll(getPlayList(file.getAbsolutePath()));
} else {
break;
}
} else if (file.getName().endsWith(".mp4")) {
s = new Spacecraft();
s.setName(file.getName());
s.setUri(Uri.fromFile(file));
fileList.add(s);
}
}
return fileList;
} catch (Exception e) {
return null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.images);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
String path = Environment.getExternalStorageDirectory().getAbsolutePath().toString() + "/and/";
list = getPlayList(path);
GridView gv = (GridView) findViewById(R.id.simpleGridView);
final VedioAdapter adapter = new VedioAdapter(this, list);
gv.setAdapter(adapter);
gv.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
gv.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
Spacecraft s = list.get(position);
s.toggleChecked();
if (s.isChecked()) {
adapter.notifyDataSetChanged();
count = count + 1;
mode.setTitle(count + " Deleted Items");
list_items.add(s);
} else {
if (count != 0) {
adapter.notifyDataSetChanged();
count = count - 1;
mode.setTitle(count + " Deleted Items");
list_items.remove(s);
}
}
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
final ActionMode mod = mode;
switch (item.getItemId()) {
case R.id.action_open:
new AlertDialog.Builder(VedioGrid.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Are you sure?")
.setMessage("Do you want to delete this image?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int p) {
int i;
for (i = 0; i < list_items.size(); ++i) {
Spacecraft s1 = list_items.get(i);
File f = new File(s1.getUri().getPath());
Boolean deleted = f.delete();
VedioGrid.this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(s1.getUri().getPath()))));
if (deleted == true) {
list.remove(s1);
adapter.notifyDataSetChanged();
}
}
list_items.clear();
Toast.makeText(VedioGrid.this, count + "Deleted", Toast.LENGTH_SHORT).show();
count = 0;
mod.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
})
.show();
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode mode) {
}
});
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Spacecraft s = list.get(position);
Toast.makeText(getApplicationContext(), s.getName(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
}
我没有安排视频的布局,即使尝试选择多个项目,活动中也没有任何项目。 我做错了吗?
我正在使用
gv.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
gv.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener()
但是什么也没发生。 早些时候,我尝试通过此操作从内部存储器中获取图像,但仍获得了期望的结果并能够选择项目,但是在这种情况下,我使用相同的逻辑来获取视频并以GridView布局显示,但没有得到结果。 我得到了黑色的物品,如上面的屏幕截图所示,无法选择。 请帮助我。