如何从SherlockActivity共享动态值

时间:2013-11-30 02:47:26

标签: java android android-fragmentactivity

我的列表视图:

listView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent,
            View view, int position, long id) {

        UtamaBean mb = (UtamaBean) adapter
                .getItem(position);

        Intent in = new Intent(getSherlockActivity(),
                DetailAct.class);

        in.putExtra(Constants.TAG_Title, mb.getTitle());
        in.putExtra(Constants.TAG_Created, mb.getCreated());
        in.putExtra(Constants.TAG_Img, mb.getImg());
        in.putExtra(Constants.TAG_Link, mb.getLink());
        in.putExtra(Constants.TAG_Post, mb.getPost());

        try {
            startActivityForResult(in, 500);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
});

我想使用Intent in = getIntent();中的数据并获取其内容:

String judulString imgString tanggalString deskString link

public class DetailAct extends SherlockActivity {

    ImageLoader img_loader;
    ImageView imgPhoto = null;

    Typeface regular;
    Typeface medium;
    Typeface bold;
    ShareActionProvider mShareActionProvider;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail);

        getSupportActionBar().setTitle("");
        getSupportActionBar().setIcon(R.drawable.new_log);
        getSupportActionBar().setBackgroundDrawable(
                new ColorDrawable(Color.parseColor("#FFFFFF")));
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        regular = Typeface.createFromAsset(getAssets(),
                "font/Alstom Regular.otf");
        medium = Typeface
                .createFromAsset(getAssets(), "font/Alstom Medium.otf");
        bold = Typeface.createFromAsset(getAssets(), "font/Alstom Bold.otf");

        Calendar c = Calendar.getInstance();
        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        String NamaHari = "";

        if (Calendar.MONDAY == dayOfWeek)
            NamaHari = "Senin";
        else if (Calendar.TUESDAY == dayOfWeek)
            NamaHari = "Selasa";
        else if (Calendar.WEDNESDAY == dayOfWeek)
            NamaHari = "Rabu";
        else if (Calendar.THURSDAY == dayOfWeek)
            NamaHari = "Kamis";
        else if (Calendar.FRIDAY == dayOfWeek)
            NamaHari = "Jumat";
        else if (Calendar.SATURDAY == dayOfWeek)
            NamaHari = "Sabtu";
        else if (Calendar.SUNDAY == dayOfWeek)
            NamaHari = "Minggu";

        final Date currentTime = new Date();
        final SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");

        // Give it to me in GMT time.
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+7"));
        System.out.println("GMT time: " + sdf.format(currentTime));

        TextView strTanggal = (TextView) findViewById(R.id.tanggal);
        strTanggal.setText(NamaHari + ", " + sdf.format(currentTime));
        strTanggal.setTextColor(Color.WHITE);

        Intent in = getIntent();

        String judul = in.getStringExtra(Constants.TAG_Title);
        String img = in.getStringExtra(Constants.TAG_Img);
        String tanggal = in.getStringExtra(Constants.TAG_Created);
        String desk = in.getStringExtra(Constants.TAG_Post);
            String link = in.getStringExtra(Constants.TAG_Link);

        TextView lblJudul = (TextView) findViewById(R.id.strJUDUL);
        TextView lblTanggal = (TextView) findViewById(R.id.strTANGGAL);
        TextView lblDeskripsi = (TextView) findViewById(R.id.strDESK);

        img_loader = new ImageLoader(DetailAct.this);
        imgPhoto = (ImageView) findViewById(R.id.strIMG);
        getImagesSize();

        try {
            if (img.length() > 0) {
                img_loader.DisplayImage(img, imgPhoto, 4);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        lblJudul.setTypeface(bold);
        lblTanggal.setTypeface(medium);
        lblDeskripsi.setTypeface(regular);
        strTanggal.setTypeface(medium);

        lblJudul.setText(judul);
        lblTanggal.setText(tanggal);
        lblDeskripsi.setText(desk);
    }

    private void getImagesSize() {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int w = dm.widthPixels;
        int h = dm.heightPixels;

        if (w <= 240 && h <= 320) {
            Constant.MAX_ThumbImage_SIZE = 100;
            Constant.MAX_Image_SIZE = 400;

        } else if (w <= 320 && h <= 480) {
            Constant.MAX_ThumbImage_SIZE = 100;
            Constant.MAX_Image_SIZE = 600;

        } else if (w <= 480 && h <= 854) {
            Constant.MAX_ThumbImage_SIZE = 150;
            Constant.MAX_Image_SIZE = 900;

        } else {
            Constant.MAX_ThumbImage_SIZE = 200;
            Constant.MAX_Image_SIZE = 1000;

        }
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.main, menu);
        mShareActionProvider = (ShareActionProvider) menu.findItem(
                R.id.btn_share).getActionProvider();
        mShareActionProvider.setShareIntent(getDefaultShareIntent());

        return super.onCreateOptionsMenu(menu);
    }

    private Intent getDefaultShareIntent() {
        Intent intent = new Intent(Intent.ACTION_SEND);

        intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_TITLE, "judul_HERE");
    intent.putExtra(Intent.EXTRA_SUBJECT, "tanggal_HERE");
    intent.putExtra(Intent.EXTRA_TEXT, "link_HERE");
    return intent;
    }


    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

        case android.R.id.home:
            finish();
            break;
        }
        return (super.onOptionsItemSelected(item));
    }

}

在本节中:

intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TITLE, "judul_HERE");
intent.putExtra(Intent.EXTRA_SUBJECT, "tanggal_HERE");
intent.putExtra(Intent.EXTRA_TEXT, "link_HERE");
return intent;

我如何获得所有数据:

String judul = in.getStringExtra(Constants.TAG_Title);
String img = in.getStringExtra(Constants.TAG_Img);
String tanggal = in.getStringExtra(Constants.TAG_Created);
String desk = in.getStringExtra(Constants.TAG_Post);
String link = in.getStringExtra(Constants.TAG_Link);

1 个答案:

答案 0 :(得分:0)

// try this
**DetailAct you send your dynamic data back to SherlockActivity like this way**
private Intent getDefaultShareIntent() {
        Intent intent = new Intent(this,SherlockActivity.class);

        intent.putExtra(Intent.EXTRA_TITLE, "judul_HERE");
        intent.putExtra(Intent.EXTRA_SUBJECT, "tanggal_HERE");
        intent.putExtra(Intent.EXTRA_TEXT, "link_HERE");
        setResult(RESULT_OK,intent);
        finish();
}

**SherlockActivity you get data from DetailAct like this way**
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK && requestCode==500){
            String judul = data.getStringExtra(Constants.TAG_Title);
            String img = data.getStringExtra(Constants.TAG_Img);
            String tanggal = data.getStringExtra(Constants.TAG_Created);
            String desk = data.getStringExtra(Constants.TAG_Post);
            String link = data.getStringExtra(Constants.TAG_Link);
        }
}