简体   繁体   English

需要帮助listitem单击android

[英]Need help on listitem click in android

Hi I have created download process activity and it's run on button click. 嗨,我创建了下载过程活动,并且它在单击按钮时运行。 This activity opens on listitem click. 单击listitem时将打开此活动。 But Now I want to run the download process on lisitem click, instread of button click. 但是,现在我要在lisitem click(按钮点击安装)上运行下载过程。

ZipDownloader.java 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;
    }



}

on MainActivity.java 在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;

    }

Please don't ignore this question. 请不要忽略这个问题。 Thanks in advance and sorry for my English. 在此先感谢您,我的英语很抱歉。

Have you tried bringing your AsyncTask code into the activity you want your listitem clicked, then just 您是否尝试过将AsyncTask代码放入您要单击列表项的活动中,然后

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

call the background task onclick? 调用后台任务onclick?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM