如何在onActivityResult方法的自定义listView中添加项目?

时间:2013-02-16 07:18:59

标签: android listview

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

您可以查看下面的代码。

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

}

2 个答案:

答案 0 :(得分:2)

当你正确的方式,但这里需要改变一些。

  1. 在创建自定义适配器类对象时,由于在列表对象中更改数据时必须通过适配器通知列表视图内容。
  2. 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
            }
        }
    }
    

答案 1 :(得分: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()的{​​{1}}声明

onActivityResult()

第二种方式。您可以致电if (requestCode == REQUEST_VIDEO_CAPTURED) { uriVideo = data.getData(); cameraVideoList.add(getFileNameFromUrl(uriVideo.getPath().toString())); ((ImageAndTextAdapter) getAdapter()).notifyDataSetChanged(); // }

在{{1}}实施中,你可以这样做

{{1}}