Android Pass字符串为非Activity

时间:2014-08-26 09:27:53

标签: android string android-listview

我的MainActivity有两个String变量即。 filename & URL

我尝试使用一个download manager

我不知道如何从我的播放器活动中传递 filename 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调用filename。如何将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)));
        }

    }
}

}

1 个答案:

答案 0 :(得分:0)

要将任何数据从一个活动传递到另一个活动,请使用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");
}

然后您可以在filename中使用setData(),如下所示: -

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