1

我想要做的:获取 ImageView 的 src 的 id,将其与两个可绘制对象的 id 进行比较,然后使用 AsyncTask 交换它们(只是因为我想了解它是如何工作的)。我在这里读过类似的问题,到目前为止,这就是我所得到的:

public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ImageView image = (ImageView) findViewById(R.id.img);
    Integer integer = (Integer) image.getTag();
}

private class cambiarImagen extends AsyncTask<Integer, Integer, Integer> {
    protected void onPreExecute() {
        ImageView image = (ImageView) findViewById(R.id.img);
        Integer integer = (Integer) image.getTag();
        int img1 = R.drawable.zapato;
        int img2 = R.drawable.zapatod;
    }

    @Override
    protected Integer doInBackground(Integer... values) {
        // parte logica
        int num = values[0];
        int zapato = values[1];
        int zapatod = values[2];
        if (num == zapato) {
            num = zapatod;
        } else if (num == zapatod) {
            num = zapato;
        }
        return num;
    }
    protected Void onPostExecute(Integer... values) {
        int num = values[0];
        ImageView image = (ImageView) findViewById(R.id.img);
        image.setTag(num);
        return null;
    }
}

当然,这是行不通的。1.我不明白如何获取ImageView作为其src的drawable的id。2.不明白AsyncTask中params是怎么传递的;onPreExecute 应该接收 UI 内容,doInbackground 应该接收它来比较它并返回应该设置为 ImageView 的 drawable int,onPreExecute 应该将其设置为 ImageView。

4

3 回答 3

0

我不明白如何获取 ImageView 作为其 src 的可绘制对象的 id。

我不必这样做,所以可能行不通,但您应该可以使用

imageView.getDrawable().getId();

我不明白参数是如何在 AsyncTask 中传递的;

你传入的任何东西task.execute()都会被doInBackground(). 如果你打电话publishProgress(),那么那里发送的任何参数都会被onProgressUpdate(). 并且返回的数据doInBackground()onPostExecute().

AsyncTask,只是让你知道,这不应该是必需的,但我知道你说过你想学习如何使用它。除了这两件事之外,我对您具体遇到的问题有点困惑,所以如果我遗漏了什么,请详细说明。

ImageView 文档

AsyncTask 文档

AsyncTask 示例(以防万一)

于 2013-09-26T14:37:56.733 回答
0

如果你想学习 ASyncTask,你应该做其他任务。如果我想学习 ASyncTask,我会做一个带有进度条的对话框或其他东西。

编辑:正如 Samus Arin 对主要帖子的评论,关于您应该始终跟踪您正在展示的图像。所以改为使用类似的东西

if(currentImage == R.Drawable.image1){

image.setImageResource(R.Drawable.image2);

}else{

    image.setImageResource(R.Drawable.image1);

}
于 2013-09-26T14:47:38.210 回答
0

对于它的价值,看看我在用 AsyncTask 做什么,也许它会给你一些想法。

这是 Monodroid/C# 代码,而不是原始 Java/Android(但语法非常接近)。因此,内部类不会获得对其包含对象的隐式引用,因此我传入一个(在构造函数中称为外部)。我选择将其命名为“_”作为 .NET 的私有数据成员的 _member 命名约定的字典扩展。

public class MonthChangeTask : AsyncTask
{
    private CalendarView _;     // outer class
    private DateTime _newMonth;
    private bool _refreshInspectionRecordsRemote;
    private bool _changingInspector;
    private bool _todayButtonPressed;

    private Android.App.ProgressDialog _progressDialog;

    private IMXController _controller;
    private Dictionary<string, string> _paramz;
    private DateTime _newSelectedDate;

    public MonthChangeTask( CalendarView outer, DateTime newMonth, bool changingInspector, bool refreshInspectionRecordsRemote, bool todayButtonPressed )
    {
        _ = outer;
        _newMonth = newMonth;
        _changingInspector = changingInspector;
        _refreshInspectionRecordsRemote = refreshInspectionRecordsRemote;
        _todayButtonPressed = todayButtonPressed;
    }

    protected override void OnPreExecute()
    {
        base.OnPreExecute();

        _progressDialog = Android.App.ProgressDialog.Show( _ , "", "Loading Inspections...");

        _newSelectedDate = _._calendar.SetMonth(new DateTime(_newMonth.Year, _newMonth.Month, 1));

        AppSettingService.SetCalendarDate(_newMonth);

        _paramz = new Dictionary<string, string>();

        string target = MD.MxNAVIGATION.CONTROLLER.CALENDAR._name;
        string action = MD.MxNAVIGATION.CONTROLLER.ACTION.GET;
        string command = _refreshInspectionRecordsRemote
            ? ((int) MD.MxNAVIGATION.CONTROLLER.CALENDAR.Command.RefreshInspectionRecordsRemote).ToString()
            : ((int) MD.MxNAVIGATION.CONTROLLER.CALENDAR.Command.RefreshInspectionRecordsLocal).ToString();

        string url = target + "/" + action + "/" + command;

        _controller = MXContainer.Instance.GetController(url, ref _paramz);
    }

    protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params)
    {
        if ( _paramz == null )
        {
            Log.Info(FIDB.TAG_APP, "MonthChangeTask.DoInBackground(): paramz = NULL");
        }
        else
        {
            _controller.Load( _paramz );
        }

        return true;
    }

    protected override void OnPostExecute(Java.Lang.Object result)
    {
        base.OnPostExecute(result);

        _progressDialog.Dismiss();

        _.Model = (CalendarVM)_controller.GetModel();   

        if (_changingInspector)
        {
            _._calendar.PermitSwitch = _.Model.Buttons.PermitsVisible;
            _._calendar.ComplaintSwitch = _.Model.Buttons.ComplaintsVisible;
            _._calendar.ProjectSwitch = _.Model.Buttons.ProjectsVisible;
            _._calendar.PeriodicInspectionSwitch = _.Model.Buttons.PeriodicInspectionsVisible;
        }

        _.UpdateCalendar(_.Model.Inspections);

        if( _todayButtonPressed )
        {
            _._calendar.SelectedDate = _._calendar.CurrentDate;
        }
        else
        {
            _._calendar.SelectedDate = _newSelectedDate;            
        }

        _._calendar.Invalidate();
        AppSettingService.SetCalendarDate( _._calendar.SelectedDate );

        if ( _.Model.IsParcelCacheDownloading )
        {
            AnimationTask task = new AnimationTask( _ );
            task.Execute( new Java.Lang.Object[1] );
        }
    }
}
于 2013-09-26T19:17:52.937 回答