【问题标题】:Need help on listitem click in android在 android 中点击 listitem 需要帮助
【发布时间】:2013-08-18 22:16:45
【问题描述】:

您好,我创建了下载过程活动,它在单击按钮时运行。此活动在列表项单击时打开。但现在我想在点击 lisitem 时运行下载过程,点击按钮。

ZipDownloader.java

import java.io.File;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.kabelash.sg.util.DecompressZip;
import com.kabelash.sg.util.DownloadFile;
import com.kabelash.sg.util.ExternalStorage;
import com.kabelash.sg.R;

public class ZipDownloader extends Activity {

    protected ProgressDialog mProgressDialog;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.zipdownload );

        // Keep the screen (and device) active as long as this app is frontmost.
        // This is to avoid going to sleep during the download.
        getWindow().addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON );
    }

    /**
     * Invoked when user presses "Start download" button.
     */
    public void startDownload( View v ) {
        String url = "http://sample.co.uk/sample.zip";
        new DownloadTask().execute( url );
    }

    /**
     * Background task to download and unpack .zip file in background.
     */
    private class DownloadTask extends AsyncTask<String,Void,Exception> {

        @Override
        protected void onPreExecute() {
            showProgress();
        }

        @Override
        protected Exception doInBackground(String... params) {
            String url = (String) params[0];

            try {
                downloadAllAssets(url);
            } catch ( Exception e ) { return e; }

            return null;
        }

    }

    //Progress window
    protected void showProgress( ) {
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setTitle( R.string.progress_title );
        mProgressDialog.setMessage( getString(R.string.progress_detail) );
        mProgressDialog.setIndeterminate( true );
        mProgressDialog.setCancelable( false );
        mProgressDialog.show();
    }

    protected void dismissProgress() {
        // You can't be too careful.
        if (mProgressDialog != null && mProgressDialog.isShowing() && mProgressDialog.getWindow() != null) {
            try {
                mProgressDialog.dismiss();
            } catch ( IllegalArgumentException ignore ) { ; }
        }
        mProgressDialog = null;
    }



}

在 MainActivity.java 上

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);

        switch(item.getItemId()){
            case R.id.update:
                Intent intent = new Intent(this, ZipDownloader.class);
                startActivity(intent);
                break;
        }
        return true;

    }

请不要忽略这个问题。提前感谢,对不起我的英语。

【问题讨论】:

  • “请忽略这个问题”。现在我很困惑。
  • 你不想回答吗?
  • 我不知道他在倒数第二句话中写了什么。
  • 对不起我的英语:) 请给我这个问题的解决方案?

标签: java android android-layout android-intent android-listview


【解决方案1】:

您是否尝试过将您的 AsyncTask 代码带入您希望点击列表项的活动中,然后只是

switch(item.getItemId()){
        case R.id.update:
          String url = "http://sample.co.uk/sample.zip";
          new DownloadTask().execute( url );
            break;
    }
    return true;

onclick 调用后台任务?

【讨论】:

  • 感谢您的想法。希望它会奏效。我试过后会告诉你的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多