【问题标题】:Android Pass string to non ActivityAndroid将字符串传递给非Activity
【发布时间】:2014-08-26 09:27:53
【问题描述】:

我的 MainActivity 有两个 String 变量,即。 文件名 & url.

我尝试使用download manager。

我不知道如何从我的玩家活动中传递 文件名 String。

  buttondownload = (TextView )findViewById(R.id.download);
        buttondownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                DownloadHelper.addNewTask(Player.this, url,
                        new PreDownloadStatusListener() {

                            @Override
                            public void notFoundSDCard(int errorCode, String errorInfo) {
                                Toast.makeText(Player.this, "No SD Card", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void sdCardCannotWriteOrRead(int errorCode, String errorInfo) {
                                Toast.makeText(Player.this, "Not read and write SD card", Toast.LENGTH_SHORT).show();

                            }

                            @Override
                            public void moreTaskCount(int errorCode, String errorInfo) {
                                Toast.makeText(Player.this, "Task list is full", Toast.LENGTH_SHORT).show();
                            }

                        });


                 Intent intent1 = new Intent(Player.this, DownloadListActivity.class);
                 startActivity(intent1);
            }
        });

        DownloadHelper.startAllTask(Player.this);

        mReceiver = new DownloadReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(DownloadValues.Actions.BROADCAST_RECEIVER_ACTION);
        registerReceiver(mReceiver, filter);

他们从另一个公共类 DownloadUtils 调用文件名。我如何将 filname 传递给 DownloadUtils

我想将字符串从 Player 传递给 ViewHolder.java

    package com.download.main;
 public class ViewHolder {

public static final int KEY_URL         = 0;
public static final int KEY_SPEED       = 1;
public static final int KEY_PROGRESS    = 2;
public static final int KEY_IS_PAUSED   = 3;

public TextView         titleText;
public ProgressBar      progressBar;
public TextView         speedText;
public ImageButton          pauseButton;
public ImageButton          deleteButton;
public ImageButton      continueButton;

private boolean         hasInited       = false;

public ViewHolder(View parentView) {
    if (parentView != null) {
        titleText = (TextView) parentView.findViewById(R.id.title);
        speedText = (TextView) parentView.findViewById(R.id.speed);
        progressBar = (ProgressBar) parentView.findViewById(R.id.progress_bar);
        pauseButton = (ImageButton) parentView.findViewById(R.id.btn_pause);
        deleteButton = (ImageButton) parentView.findViewById(R.id.btn_delete);
        continueButton = (ImageButton) parentView.findViewById(R.id.btn_continue);
        hasInited = true;
    }
}

public static HashMap<Integer, String> getItemDataMap(String url, String speed, String progress, String isPaused) {
    HashMap<Integer, String> item = new HashMap<Integer, String>();
    item.put(KEY_URL, url);
    item.put(KEY_SPEED, speed);
    item.put(KEY_PROGRESS, progress);
    item.put(KEY_IS_PAUSED, isPaused);
    return item;
}

public void setData(HashMap<Integer, String> item) {
    if (hasInited) {
        titleText.setText(///////**I want here my my string calle "filename" from player Activity**//////);
        speedText.setText(item.get(KEY_SPEED));
        String progress = item.get(KEY_PROGRESS);
        if (TextUtils.isEmpty(progress)) {
            progressBar.setProgress(0);
        }
        else {
            progressBar.setProgress(Integer.parseInt(progress));
        }
        if (Boolean.parseBoolean(item.get(KEY_IS_PAUSED))) {
            onPause();
        }
    }
}

public void onPause() {
    if (hasInited) {
        pauseButton.setVisibility(View.GONE);
        continueButton.setVisibility(View.VISIBLE);
    }
}

public void setData(String url, String speed, String progress) {
    setData(url, speed, progress, false + "");
}

public void setData(String url, String speed, String progress, String isPaused) {
    if (hasInited) {
        HashMap<Integer, String> item = getItemDataMap(url, speed, progress, isPaused);

        titleText.setText(///////**I want here my my string calle "filename" from player Activity**//////);
        speedText.setText(speed);
        if (TextUtils.isEmpty(progress)) {
            progressBar.setProgress(0);
        }
        else {
            progressBar.setProgress(Integer.parseInt(item.get(KEY_PROGRESS)));
        }

    }
}

}

【问题讨论】:

  • 你的问题不清楚。请详细说明您的第三行。
  • 我现在改了..你能检查一下github源码吗?请

标签: android string android-listview


【解决方案1】:

要将任何数据从一个活动传递到另一个活动,请使用Intent。示例:-

在Player.java 类中使用以下代码:-

Intent intent = new Intent(FromClass.this, ToClass.class);
intent.putExtra("filename", filename);
startActivity(intent);

在ViewHolder.java 类中编写以下代码:-

别忘了扩展Activity

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

    Intent intent = getIntent();
    String filename= intent.getStringExtra("filename");
}

然后你可以像这样在你的setData() 中使用filename:-

public void setData(HashMap<Integer, String> item) {
    if (hasInited) {
        titleText.setText(filename);
        speedText.setText(item.get(KEY_SPEED));
        String progress = item.get(KEY_PROGRESS);
        if (TextUtils.isEmpty(progress)) {
            progressBar.setProgress(0);
        }
        else {
            progressBar.setProgress(Integer.parseInt(progress));
        }
        if (Boolean.parseBoolean(item.get(KEY_IS_PAUSED))) {
            onPause();
        }
    }
}

【讨论】:

  • 但我不想开始 DownloadUtils 类.. 它不是一个活动
  • 您最好发布更多您编写的代码,以使您的问题易于理解。
  • 您想从源代码中使用哪个class 或method?
  • 我的班级里没有 onCreate()。我再次发布了完整的班级文件检查。
  • 还有什么?顺便说一句,您的player 班级在哪里?我想看看您是如何将 文件名 存储在 filename 中的。
猜你喜欢
  • 1970-01-01
  • 2013-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多