如何的onActivityResult方法中添加项自定义的ListView? [英] How to add item in Custom listView in onActivityResult method?

查看:193
本文介绍了如何的onActivityResult方法中添加项自定义的ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用摄像头的Andr​​oid开发视频一个简单的应用程序。在我的应用我有一个有按钮名称进行视频,哪些是用于以显示我的应用程序记录的视频名称的列表视图。现在,当我点击按钮进行视频它会打开我的手机拍照记录,但是当我完成我的拍摄镜头给了我两个选择。 保存和放弃。现在,通过单击保存选项,我要录制的视频的名称添加到我的列表视图。我已经开发了这方面的一些code和它工作得很好,但我现在面临的问题是如何在我的列表视图中的onActivityResult方法中添加录制视频的名称和更新我的列表视图。请帮助我,我会很感激你的。

您可以检查我下面的code。

 公共类MainActivity扩展ListActivity
{    私人的ArrayList<串GT; cameraVideoList =新的ArrayList<串GT;();    上下文CTX;
//资源资源;     INT REQUEST_VIDEO_CAPTURED = 1;
     乌里uriVideo = NULL;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        CTX = getApplicationContext();
        按钮makeVideo =(按钮)findViewById(R.id.button1);
        makeVideo.setOnClickListener(新OnClickListener()
        {
            公共无效的onClick(视图v)
            {
                //意向意图=新意图(android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);
                意向意图=新意图(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
                startActivityForResult(意向,REQUEST_VIDEO_CAPTURED);            }
        });        ListView控件videoList = getListView();        videoList.setOnItemClickListener(新OnItemClickListener()
        {
           @覆盖
            公共无效onItemClick(适配器视图<>为arg0,ARG1查看,INT位置,长ARG3)
           {
                Toast.makeText(MainActivity.this,+位置,Toast.LENGTH_SHORT).show();            }
        });        setListAdapter(新ImageAndTextAdapter(CTX,R.layout.list_item_icon_text,cameraVideoList));
    }    @覆盖
    保护无效的onActivityResult(INT申请code,INT结果code,意图数据)
    {
        // TODO自动生成方法存根
        super.onActivityResult(要求code,结果code,数据);        如果(结果code == RESULT_OK)
        {
            如果(要求code == 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())。);            }
        }
    }    公共字符串getFileNameFromUrl(字符串路径)
    {
        的String [] = pathArray path.split(/);
        返回pathArray [pathArray.length - 1];
    }    公共类ImageAndTextAdapter扩展ArrayAdapter<串GT;
    {
        私人LayoutInflater mInflater;
        私人的ArrayList<串GT; mStrings;
        私人诠释mViewResourceId;        公共ImageAndTextAdapter(上下文的背景下,INT textViewResourceId,ArrayList的<串GT;的对象)
        {
            超(背景下,textViewResourceId,对象);
            mInflater =(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);            mStrings =物体;
            mViewResourceId = textViewResourceId;
        }        公共查看getView(INT位置,查看convertView,父母的ViewGroup)
        {
            convertView = mInflater.inflate(mViewResourceId,NULL);            ImageView的IV =(ImageView的)convertView.findViewById(R.id.icon);
            iv.setImageResource(R.drawable.video_icon);            TextView的电视=(TextView中)convertView.findViewById(R.id.text);
            tv.setText(mStrings.get(位置));            返回convertView;
        }
    }}


解决方案

当你去正确的方式,但这里的一些变化是要求。


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

声明 ImageAndTextAdapter适配器; 全球和私有对象

 的onCreate(){    适配器=新ImageAndTextAdapter(CTX,R.layout.list_item_icon_text,cameraVideoList);
    videoList.setAdapter(适配器);}

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

  @覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据)
{
    // TODO自动生成方法存根
    super.onActivityResult(要求code,结果code,数据);    如果(结果code == RESULT_OK)
    {
        如果(要求code == REQUEST_VIDEO_CAPTURED)
        {
            uriVideo = data.getData();            cameraVideoList.add(getFileNameFromUrl(uriVideo.getPath()的toString())。);            adapter.notifyDatasetChanged(); // 这里
        }
    }
}

I am developing a simple app for videos by using android camera. In my app I have a button having name "Make Video " and a list view which is for to show the video name recorded by my app. Now as I click the button "Make Video " it opens my mobile camera for recording but when I complete my recording the camera gives me two options. "Save" and "Discard". Now by clicking the "Save" option, I want to add the name of the recorded video to my list view. I have developed some code in this respect and it works fine,but I am facing issue that how to add the name of recorded video in my listview in onActivityResult method and update my list view. Please help me I would be very thankful to you.

You can check my code below.

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

}

解决方案

As you going right way but here some change are requirement.

  1. As you create your Custom adapter class object that not work because when you change your data in list object you have to notify by adapter for list view content.

Declare ImageAndTextAdapter adapter; as global and private object

onCreate(){

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

}

Ok now in onActivityResult after adding new record into your list object just call the notifyDataSetChange() of your adapter class

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

这篇关于如何的onActivityResult方法中添加项自定义的ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆