回来到活动中不会对一个ListView创建onItemClick后,一个新的 [英] Come back to the activity without creating a new one after onItemClick on a ListView

查看:126
本文介绍了回来到活动中不会对一个ListView创建onItemClick后,一个新的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序的选项菜单中,显示所有用户添加的地图上的标记列表。标记重新presented使用一个ListView,当用户选择的ListView的项目之一,我想回去(简历?)的主要活动传递标记位置作为一个参数,但没有创建新活动。那可能吗?现在,我创建一个新的活动,当用户点击任何项目,它是可以简单地回来的主要活动?

MarkersList活动code:

  mainListView.setOnItemClickListener(新OnItemClickListener()
{
    公共无效onItemClick(适配器视图<>为arg0,视图V,INT位置,长的ID)
    {
            意向意图=新意图(getBaseContext(),MainActivity.class);
        MyMarkerObj标记=(MyMarkerObj)table.get(位置);
        intent.putExtra(位置,marker.getId());
        startActivity(意向);
        完();
    }
});

MainActivity的code:

  @覆盖
保护无效onResume(){    意向意图= getIntent();
    如果(意向!= NULL){
        捆绑包= intent.getExtras();
        如果(捆绑!= NULL){
            INT reportId = bundle.getInt(位置);
            Toast.makeText(getApplicationContext(),Integer.toString(reportId),Toast.LENGTH_SHORT).show();
              尝试{
                data.open();
               }赶上(例外五){
                    Log.i(你好,你好);
               }
            //获取标记在列表视图中选择
            字符串位置= data.getMarkerById(reportId);
            //System.out.println(\">>>>>>>>>>>>>>>位置=+位置);
        }
    }
    super.onResume();
}


解决方案

使用 StartActivityForResult(意向,REQ code)而不是 startActivity (意向)

在你的第一次
第一项活动

 意向书I =新意图(MainActivity.this,MarkerActivity.class);
startActivityForResult(I,1); < - 1请求code,你可以付出一切。

,然后在第二个活动项目单击

 意向意图= getIntent();
    MyMarkerObj标记=(MyMarkerObj)table.get(位置);
    intent.putExtra(位置,marker.getId());
   的setResult(响应code,意向);
    完();

而在

FirstActivity的onActivityForResult

得到这样的价值

 保护无效的onActivityResult(INT申请code,INT结果code,意图数据)
    {
     如果(要求code == 1安培;&安培;结果code == RESULT_OK)
        {       捆绑馍= data.getExtras();
       INT位置= bun.getInt(位置);
        }
    }

I have an option menu in my app that shows a list of all the markers that user added on the map. Markers are represented using a ListView, and when user selects one of the items of that ListView, I want to go back (resume?) to the main activity passing the marker position as a parameter, but without creating a new activity. Is that possible? Now I am creating a new activity when user clicks on any item, is it possible to simply come back to the main activity?

Code of MarkersList activity:

mainListView.setOnItemClickListener(new OnItemClickListener()
{
    public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
    {
            Intent intent = new Intent(getBaseContext(), MainActivity.class);
        MyMarkerObj marker = (MyMarkerObj)table.get(position);
        intent.putExtra("position", marker.getId());
        startActivity(intent);
        finish();
    }
});

Code of MainActivity:

 @Override
protected void onResume() {  

    Intent intent = getIntent(); 
    if (intent != null) { 
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            int reportId = bundle.getInt("position");
            Toast.makeText(getApplicationContext(), Integer.toString(reportId), Toast.LENGTH_SHORT).show();
              try {
                data.open();
               } catch (Exception e) {
                    Log.i("hello", "hello");
               } 


            //Get the marker selected in the listview
            String position = data.getMarkerById(reportId);
            //System.out.println(">>>>>>>>>>>>>>> position = " + position); 
        }
    }
    super.onResume();
}

解决方案

Use StartActivityForResult(intent,reqcode) instead of startActivity(intent).

first in your First Activity

Intent i=new Intent(MainActivity.this, MarkerActivity.class);
startActivityForResult(i,1);  <-- 1 is request code, you can give anything.

and then on ItemClick of second Activity

 Intent intent = getIntent();
    MyMarkerObj marker = (MyMarkerObj)table.get(position);
    intent.putExtra("position", marker.getId());
   setResult(responsecode,intent);
    finish();

And in

onActivityForResult of FirstActivity

get the value like this

 protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
     if(requestCode == 1 && resultCode == RESULT_OK)
        {

       Bundle bun=data.getExtras();
       int position= bun.getInt("position");     
        }
    }

这篇关于回来到活动中不会对一个ListView创建onItemClick后,一个新的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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