2

我正在使用 android 相机开发一个简单的视频应用程序。在我的应用程序中,我有一个名为“制作视频”的按钮和一个列表视图,用于显示我的应用程序录制的视频名称。现在,当我单击“制作视频”按钮时,它会打开我的移动摄像头进行录制,但是当我完成录制时,摄像头会给我两个选项。“保存”和“丢弃”。现在通过单击“保存”选项,我想将录制的视频的名称添加到我的列表视图中。我在这方面开发了一些代码,它工作正常,但我面临的问题是如何在 onActivityResult 方法的列表视图中添加录制视频的名称并更新我的列表视图。请帮助我,我会非常感谢你。

您可以在下面查看我的代码。

public class MainActivity extends ListActivity 
{

    private ArrayList<String> cameraVideoList = new ArrayList<String>();

    Context ctx;
//  Resources res;

     int REQUEST_VIDEO_CAPTURED =1;
     Uri uriVideo = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ctx = getApplicationContext();


        Button makeVideo = (Button) findViewById(R.id.button1);
        makeVideo.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) 
            {
                //Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
                startActivityForResult(intent, REQUEST_VIDEO_CAPTURED);

            }
        });

        ListView videoList = getListView();

        videoList.setOnItemClickListener(new OnItemClickListener()
        {
           @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) 
           {
                Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();

            }
        });

        setListAdapter(new ImageAndTextAdapter(ctx, R.layout.list_item_icon_text, cameraVideoList));


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK)
        {
            if (requestCode == REQUEST_VIDEO_CAPTURED) 
            {
                uriVideo = data.getData();
//                Toast.makeText(MainActivity.this, uriVideo.getPath(),
//                        Toast.LENGTH_LONG).show();
//                
//                Toast.makeText(MainActivity.this, uriVideo.toString(),
//                        Toast.LENGTH_LONG).show();

                cameraVideoList.add(getFileNameFromUrl(uriVideo.getPath().toString()));

            }
        }
    }

    public String getFileNameFromUrl(String path) 
    {
        String[] pathArray = path.split("/");
        return pathArray[pathArray.length - 1];
    }

    public class ImageAndTextAdapter extends ArrayAdapter<String>
    {
        private LayoutInflater mInflater;
        private ArrayList<String> mStrings;
        private int mViewResourceId;

        public ImageAndTextAdapter(Context context, int textViewResourceId,ArrayList<String> objects) 
        {
            super(context, textViewResourceId, objects);
            mInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            mStrings = objects;
            mViewResourceId = textViewResourceId;
        }

        public View getView(int position, View convertView, ViewGroup parent) 
        {
            convertView = mInflater.inflate(mViewResourceId, null);

            ImageView iv = (ImageView)convertView.findViewById(R.id.icon);
            iv.setImageResource(R.drawable.video_icon);

            TextView tv = (TextView)convertView.findViewById(R.id.text);
            tv.setText(mStrings.get(position));

            return convertView;
        }
    }

}
4

2 回答 2

2

当你走正确的路,但这里有一些改变是必需的。

  1. 当您创建不起作用的自定义适配器类对象时,因为当您更改列表对象中的数据时,您必须通过适配器通知列表视图内容。

声明ImageAndTextAdapter adapter;为全局和私有对象

onCreate(){

    adapter = new ImageAndTextAdapter(ctx, R.layout.list_item_icon_text, cameraVideoList);
    videoList.setAdapter(adapter);

}

现在在 onActivityResult 中添加新记录到列表对象后,只需调用适配器类的 notifyDataSetChange()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK)
    {
        if (requestCode == REQUEST_VIDEO_CAPTURED) 
        {
            uriVideo = data.getData();

            cameraVideoList.add(getFileNameFromUrl(uriVideo.getPath().toString()));

            adapter.notifyDatasetChanged(); // here
        }
    }
}
于 2013-02-16T07:26:32.447 回答
2

第一种方式。

在这条线上。您创建了ImageAndTextAdapter无法访问的对象。

setListAdapter(new ImageAndTextAdapter(ctx, R.layout.list_item_icon_text, cameraVideoList));

ImageAndTextAdapter在变量中提取对象

adapter = new ImageAndTextAdapter(ctx, R.layout.list_item_icon_text, cameraVideoList);
  setListAdapter(adapter);

从这份onActivityResult()呼吁notifyDataSetChanged()的声明中ImageAndTextAdapter

if (requestCode == REQUEST_VIDEO_CAPTURED) 
{
     uriVideo = data.getData();
//   Toast.makeText(MainActivity.this, uriVideo.getPath(),
//    Toast.LENGTH_LONG).show();
//                
//   Toast.makeText(MainActivity.this, uriVideo.toString(),
//   Toast.LENGTH_LONG).show();

     cameraVideoList.add(getFileNameFromUrl(uriVideo.getPath().toString()));

     adapter.notifyDataSetChanged();
}

第二种方式。你可以打电话((ImageAndTextAdapter) getAdapter()).notifyDataSetChanged()

在您的onActivityResult()实现中,您可以这样做

if (requestCode == REQUEST_VIDEO_CAPTURED) 
{
     uriVideo = data.getData();

     cameraVideoList.add(getFileNameFromUrl(uriVideo.getPath().toString()));

     ((ImageAndTextAdapter) getAdapter()).notifyDataSetChanged(); // 
}
于 2013-02-16T07:26:52.537 回答