从活动C返回列表视图位置到活动B然后继续

时间:2014-05-14 09:37:34

标签: android

我在类中有一个名为A的列表视图,如果我按任何列表项,它将转到页面B,在页面B中我有一些其他元素,在活动B中我也有警报框,所以如果我选择从该警报框提交它将带我到页面C。所以我的问题是我使用Bundles从活动A到B的列表项位置但在活动C中如果我点击后退按钮然后再次如果我点击活动B中的警告框中的提交按钮它显示错误它不会活动C.我在谷歌搜索,我实施但它不工作....所以任何人都可以帮助我.... 活动是

公共类MainActivity扩展了ListActivity {

private static final int LIST_ITEM_TYPE_1 = 0;
private static final int LIST_ITEM_TYPE_2 = 1;
private static final int LIST_ITEM_TYPE_COUNT = 2;

private static final int LIST_ITEM_COUNT = 3;
private static final int LIST_ITEM_TYPE_1_COUNT = 1;
private static final int LIST_ITEM_TYPE_2_COUNT = 2;
private MyCustomAdapter mAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActionBar actionbar = getActionBar();
    actionbar.setDisplayShowHomeEnabled(false);
    //actionbar.setDisplayShowTitleEnabled(true);
    // actionbar.setDisplayHomeAsUpEnabled(true);
    mAdapter = new MyCustomAdapter();
    for (int i = 0; i < LIST_ITEM_COUNT; i++) {
        if (i < LIST_ITEM_TYPE_1_COUNT)
            mAdapter.addItem("item type 1");
        else if(i==LIST_ITEM_TYPE_2_COUNT)
            mAdapter.addItem("Wbs for osting Booking");
        else
            mAdapter.addItem("Jake Johnson");
    }
    setListAdapter(mAdapter);
}

private class MyCustomAdapter extends BaseAdapter {

    private ArrayList<String> mData = new ArrayList<String>();
    private LayoutInflater mInflater;

    public MyCustomAdapter() {
        mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void addItem(final String item) {
        mData.add(item);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if(position < LIST_ITEM_TYPE_1_COUNT)
            return LIST_ITEM_TYPE_1;
        else
            return LIST_ITEM_TYPE_2;
    }

    @Override
    public int getViewTypeCount() {
        return LIST_ITEM_TYPE_COUNT;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public String getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        int type = getItemViewType(position);
        if (convertView == null) {
            holder = new ViewHolder();
            switch(type) {
            case LIST_ITEM_TYPE_1:
                convertView = mInflater.inflate(R.layout.emp_sign_off_first_row, null);
                break;
            case LIST_ITEM_TYPE_2:
                convertView = mInflater.inflate(R.layout.emp_sign_off_sec_row, null);
                holder.textView = (TextView)convertView.findViewById(R.id.emp_nametxt);
                holder.textVersion = (TextView)convertView.findViewById(R.id.emp_idtxt);
                holder.img = (ImageView)convertView.findViewById(R.id.imageView2);
                holder.textView.setText(mData.get(position));
                holder.textVersion.setText("1.100235");
                holder.img.setVisibility(View.INVISIBLE);
                break;
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        convertView.setOnClickListener(new View.OnClickListener()
        {

            public void onClick(View v) 
            {

                Intent i=new Intent(MainActivity.this,MenuActivity.class);
                i.putExtra("pos",""+ position);
                startActivity(i);


            }
        });
        return convertView;
    }
}

public static class ViewHolder {
    public TextView textView;
    public TextView textVersion;
    public ImageView img;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    return true;
}

}

我的活动B是

公共类MenuActivity扩展ListActivity实现了View.OnClickListener {

private static final int LIST_ITEM_TYPE_1 = 0;
private static final int LIST_ITEM_TYPE_2 = 1;
private static final int LIST_ITEM_TYPE_3 = 2;
private static final int LIST_ITEM_TYPE_4 = 3;
private static final int LIST_ITEM_TYPE_COUNT = 4;

private static final int LIST_ITEM_COUNT = 4;
private static final int LIST_ITEM_TYPE_1_COUNT = 1;
private static final int LIST_ITEM_TYPE_2_COUNT = 2;
private static final int LIST_ITEM_TYPE_3_COUNT = 3;

private MyCustomAdapter mAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActionBar actionbar = getActionBar();
    actionbar.setDisplayShowHomeEnabled(false);
    actionbar.setDisplayShowTitleEnabled(false);
    actionbar.setDisplayHomeAsUpEnabled(true);
    mAdapter = new MyCustomAdapter();
    for (int i = 0; i < LIST_ITEM_COUNT; i++) {
        if (i < LIST_ITEM_TYPE_1_COUNT)
            mAdapter.addItem("Jake JohnSon");
        else if(i<LIST_ITEM_TYPE_2_COUNT)
            mAdapter.addItem("ITEM TYPE 2");
        else if(i<LIST_ITEM_TYPE_3_COUNT)
            mAdapter.addItem("ITEM TYPE 3");
        else 
            mAdapter.addItem("ITEM TYPE 4");
    }
    setListAdapter(mAdapter);

}

private class MyCustomAdapter extends BaseAdapter {

    private ArrayList<String> mData = new ArrayList<String>();
    private LayoutInflater mInflater;

    public MyCustomAdapter() {
        mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void addItem(final String item) {
        mData.add(item);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if(position < LIST_ITEM_TYPE_1_COUNT)
            return LIST_ITEM_TYPE_1;
        else if(position < LIST_ITEM_TYPE_2_COUNT)
            return LIST_ITEM_TYPE_2;
        else if(position < LIST_ITEM_TYPE_3_COUNT)
            return LIST_ITEM_TYPE_3;
        else 
            return LIST_ITEM_TYPE_4;
    }

    @Override
    public int getViewTypeCount() {
        return LIST_ITEM_TYPE_COUNT;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public String getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        int type = getItemViewType(position);
        if (convertView == null) {
            holder = new ViewHolder();
            switch(type) {
            case LIST_ITEM_TYPE_1:
                convertView = mInflater.inflate(R.layout.emp_sign_off_sec_row, null);
                holder.textView=(TextView)convertView.findViewById(R.id.emp_nametxt);
                holder.textVersion = (TextView)convertView.findViewById(R.id.emp_idtxt);
                holder.img = (ImageView)convertView.findViewById(R.id.imageView2);
                holder.img.setVisibility(View.INVISIBLE);
                holder.textView.setText(mData.get(position));
                holder.textVersion.setText("1.100235");
                break;
            case LIST_ITEM_TYPE_2:
                convertView = mInflater.inflate(R.layout.emp_sign_off_after_page_row2, null);
                holder.textView=(TextView)convertView.findViewById(R.id.textView1);
                holder.textView.setText("Absent");
                holder.text=(Switch)convertView.findViewById(R.id.switch1);
                break;
            case LIST_ITEM_TYPE_3:
                convertView = mInflater.inflate(R.layout.emp_sign_off_details_row3, null);
                break;
            case LIST_ITEM_TYPE_4:
                convertView = mInflater.inflate(R.layout.emp_sign_off_details_row4, null);
                break;
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        return convertView;
    }
}

public static class ViewHolder {
    public TextView textView;
    public TextView textVersion;
    public Switch text;
    public ImageView img;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu1, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode==1)
    {
        if (resultCode == RESULT_OK) {
            //String name = data.getStringExtra("Result");
            Bundle b=getIntent().getExtras();
            String s=b.getString("pos");
            Intent i=new Intent(MenuActivity.this,EmpSignOffAfterPageActivity.class);
            i.putExtra("pos", s);
        }
    }

}

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    switch (item.getItemId()) 
    {
    case android.R.id.home:   
        NavUtils.navigateUpFromSameTask(this);
        break;
    case R.id.action_settings1:

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                MenuActivity.this);
        LayoutInflater inflater =  MenuActivity.this.getLayoutInflater();

        alertDialog.setView(inflater.inflate(R.layout.pop_up_window, null));
        alertDialog.setPositiveButton("Submit",
                new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog,
                    int which) {
                Bundle b=getIntent().getExtras();
                String s=b.getString("pos");
                Toast.makeText(getApplicationContext(), "item"+s, Toast.LENGTH_SHORT).show();
                Intent i=new Intent(MenuActivity.this,EmpSignOffAfterPageActivity.class);
                i.putExtra("pos", s);
                startActivityForResult(i, 1);
            }
        });
        alertDialog.setNeutralButton("Cancel",
                new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog,
                    int which) {
                dialog.cancel();
            }
        });
        alertDialog.show();
        return true;        
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

}

我的活动C是

公共类EmpSignOffAfterPageActivity扩展了ListActivity {

private static final int LIST_ITEM_TYPE_1 = 0;
private static final int LIST_ITEM_TYPE_2 = 1;
private static final int LIST_ITEM_TYPE_3 = 2;
private static final int LIST_ITEM_TYPE_4 = 3;
private static final int LIST_ITEM_TYPE_COUNT = 4;

private static final int LIST_ITEM_COUNT = 4;
private static final int LIST_ITEM_TYPE_1_COUNT = 1;
private static final int LIST_ITEM_TYPE_2_COUNT = 2;
private static final int LIST_ITEM_TYPE_3_COUNT = 3;

private MyCustomAdapter mAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActionBar actionbar = getActionBar();
    actionbar.setDisplayShowHomeEnabled(false);
    actionbar.setDisplayShowTitleEnabled(false);
    actionbar.setDisplayHomeAsUpEnabled(true);
    mAdapter = new MyCustomAdapter();
    for (int i = 0; i < LIST_ITEM_COUNT; i++) {
        if (i < LIST_ITEM_TYPE_1_COUNT)
            mAdapter.addItem("item type 1");
        else if(i<LIST_ITEM_TYPE_2_COUNT)
            mAdapter.addItem("ITEM TYPE 2");
        else if(i<LIST_ITEM_TYPE_3_COUNT)
            mAdapter.addItem("ITEM TYPE 3");
        else 
            mAdapter.addItem("ITEM TYPE 4");
    }
    setListAdapter(mAdapter);

}

private class MyCustomAdapter extends BaseAdapter {

    private ArrayList<String> mData = new ArrayList<String>();
    private LayoutInflater mInflater;

    public MyCustomAdapter() {
        mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void addItem(final String item) {
        mData.add(item);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if(position < LIST_ITEM_TYPE_1_COUNT)
            return LIST_ITEM_TYPE_1;
        else if(position < LIST_ITEM_TYPE_2_COUNT)
            return LIST_ITEM_TYPE_2;
        else if(position < LIST_ITEM_TYPE_3_COUNT)
            return LIST_ITEM_TYPE_3;
        else 
            return LIST_ITEM_TYPE_4;
    }

    @Override
    public int getViewTypeCount() {
        return LIST_ITEM_TYPE_COUNT;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public String getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        int type = getItemViewType(position);
        if (convertView == null) {
            holder = new ViewHolder();
            switch(type) {
            case LIST_ITEM_TYPE_1:
                convertView = mInflater.inflate(R.layout.emp_sign_off_sec_row, null);
                holder.img = (ImageView)convertView.findViewById(R.id.imageView2);
                holder.img.setVisibility(View.INVISIBLE);
                break;
            case LIST_ITEM_TYPE_2:
                convertView = mInflater.inflate(R.layout.emp_sign_off_agter_page_row1, null);
                break;
            case LIST_ITEM_TYPE_3:
                convertView = mInflater.inflate(R.layout.emp_sign_off_after_page_row2, null);
                break;
            case LIST_ITEM_TYPE_4:
                convertView = mInflater.inflate(R.layout.emp_sign_off_after_page_row2, null);
                holder.textView=(TextView)convertView.findViewById(R.id.textView1);
                holder.textView.setText(" Do you understand the hazards you may have been exposed to throughout your work day(falls,crush points,chemicaals,etc)?");
                holder.text=(Switch)convertView.findViewById(R.id.switch1);
                break;
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        return convertView;
    }

}

public static class ViewHolder {
    public TextView textView;
    public TextView textVersion;
    public Switch text;
    public ImageView img;
}


@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.emp_sign_off_after_page, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    switch (item.getItemId()) 
    {
    case android.R.id.home:   
        Bundle b=getIntent().getExtras();
        String s=b.getString("pos");
        Toast.makeText(getApplicationContext(), "item"+s, Toast.LENGTH_SHORT).show();
        Intent i=new Intent();
        i.putExtra("pos", s);
        setResult(RESULT_OK,i);
        finish();
        break;
    case R.id.action_settings1:     
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                EmpSignOffAfterPageActivity.this);
        LayoutInflater inflater =  EmpSignOffAfterPageActivity.this.getLayoutInflater();

        alertDialog.setView(inflater.inflate(R.layout.emp_sign_off_last_dialog, null));
        final EditText pass=(EditText)findViewById(R.id.editText1);

        alertDialog.setPositiveButton("Approve",
                new DialogInterface.OnClickListener() {


            public void onClick(DialogInterface dialog,
                    int which) {
                Bundle b=getIntent().getExtras();
                String s=b.getString("pos");
                Toast.makeText(getApplicationContext(), "item"+s, Toast.LENGTH_SHORT).show();
                Intent i=new Intent(EmpSignOffAfterPageActivity.this,EmpSignOffLastPageActivity.class);
                i.putExtra("pos", s);
                startActivity(i);

            }
        });
        alertDialog.setNeutralButton("Reject",
                new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog,
                    int which) {
                Bundle b=getIntent().getExtras();
                String s=b.getString("pos");
                Toast.makeText(getApplicationContext(), "item"+s, Toast.LENGTH_SHORT).show();
                Intent i=new Intent(EmpSignOffAfterPageActivity.this,EmpSignOffRejectActivity.class);
                i.putExtra("pos", s);
                setResult(RESULT_OK,i);
                finish();
            }
        });

        alertDialog.show();
        return true;        
    }
    return super.onOptionsItemSelected(item);
}

}

所以请告诉我如何将列表项目位置从活动C回到B,然后如果我点击B中的提醒框提交,它应该转到活动C,列表项位置.....

1 个答案:

答案 0 :(得分:0)

在你的B活动中,它是MenuActivity:在onActivityResult

替换它:

 Bundle b=getIntent().getExtras();

with:

 Bundle b=data.getExtras(); 

// onActivityResults已经返回活动的结果。如果你通过getIntent()检查,那么就没有该活动的引用。

此外,当您从对话框中单击时,在Intent中添加null检查,因为您现在从C活动返回并且没有引用该意图。如果来自C活动,你可以设置一个bool,然后获取onActivityResult intent的数据。