Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取ImageView可绘制ID并使用AsyncTask更改它_Java_Android_Android Asynctask_Imageview - Fatal编程技术网

Java 获取ImageView可绘制ID并使用AsyncTask更改它

Java 获取ImageView可绘制ID并使用AsyncTask更改它,java,android,android-asynctask,imageview,Java,Android,Android Asynctask,Imageview,我想做的是:获取ImageView的src的id,将其与两个Drawable的id进行比较,然后使用AsyncTask交换它们(只是因为我想了解它是如何工作的)。 我在这里读过类似的问题,到目前为止,我得到的是: public class Main extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

我想做的是:获取ImageView的src的id,将其与两个Drawable的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;
    }
}
public类主扩展活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView图像=(ImageView)findViewById(R.id.img);
整数=(整数)image.getTag();
}
私有类cambiarImagen扩展异步任务{
受保护的void onPreExecute(){
ImageView图像=(ImageView)findViewById(R.id.img);
整数=(整数)image.getTag();
int img1=R.drawable.zapato;
int img2=R.drawable.zapatod;
}
@凌驾
受保护的整数doInBackground(整数…值){
//逻辑部分
int num=值[0];
int zapato=数值[1];
int zapatod=数值[2];
如果(num==zapato){
num=zapatod;
}else if(num==zapatod){
num=zapato;
}
返回num;
}
受保护的Void onPostExecute(整型…值){
int num=值[0];
ImageView图像=(ImageView)findViewById(R.id.img);
image.setTag(num);
返回null;
}
}
这当然行不通。 1.我不明白如何获取ImageView作为其src的可绘制文件的id。 2.我不明白参数是如何在AsyncTask中传递的;onPreExecute应该接收UI内容,doInbackground应该接收它以进行比较并返回应设置为ImageView的可绘制int,onPreExecute应该将其设置为ImageView

我不明白如何获取ImageView作为其src的可绘制文件的id

我没有这样做,所以可能不工作,但你应该能够使用

imageView.getDrawable().getId();
我不明白参数是如何在AsyncTask中传递的

无论您在
task.execute()
中传递什么,都会被
doInBackground()
接收。如果您调用
publishProgress()
,则发送到那里的任何参数都会被
onProgressUpdate()
接收。并且
doInBackground()
中返回的数据会被
onPostExecute()
接收

AsyncTask
,正如你所知,这不应该需要,但我知道你说过你想学习如何使用它。我有点困惑,除了这两件事之外,你到底有什么问题,所以如果我遗漏了什么,请详细说明


(如果有帮助的话)

如果你想开始学习异步任务,你应该做其他任务。 如果我想学习ASyncTask,我会用进度条或其他东西来进行对话

编辑: 正如Samus Arin在主要帖子中评论的那样,你应该始终跟踪你正在显示的图像

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

image.setImageResource(R.Drawable.image2);

}else{

    image.setImageResource(R.Drawable.image1);

}

看看我在异步任务中做了什么,也许它会给你一些想法

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

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] );
        }
    }
}
公共类MonthChangeTask:AsyncTask
{
private CalendarView;//外部类
私人日期时间(newMonth);;
私人bool\u刷新检查记录远程;
私人布卢昌宁监察员;
今天按下私人按钮;
私有Android.App.ProgressDialog\u ProgressDialog;
专用IMXController\u控制器;
私人字典_paramz;
私人日期时间(newselectedate);;
公共月更改任务(日历视图外部、DateTime newMonth、布尔更改监控器、布尔刷新检查记录远程、布尔今天按钮按下)
{
_=外部;
_newMonth=newMonth;
_changingInspector=changingInspector;
_refreshInspectionRecordsRemote=refreshInspectionRecordsRemote;
_todayButtonPressed=todayButtonPressed;
}
受保护的覆盖无效OnPreExecute()
{
base.OnPreExecute();
_progressDialog=Android.App.progressDialog.Show(wu,“,“加载检查…”);
_newSelectedDate=u._calendar.SetMonth(新日期时间(_newMonth.Year,_newMonth.Month,1));
AppSettingService.SetCalendarDate(_newMonth);
_paramz=新字典();
字符串target=MD.MxNAVIGATION.CONTROLLER.CALENDAR.\u name;
string action=MD.MxNAVIGATION.CONTROLLER.action.GET;
string命令=\u refreshInspectionRecordsRemote
((int)MD.MxNAVIGATION.CONTROLLER.CALENDAR.Command.RefreshInspectionRecordsRemote.ToString()
:((int)MD.MxNAVIGATION.CONTROLLER.CALENDAR.Command.RefreshInspectionRecordsLocal.ToString();
字符串url=target+“/”+action+“/”+命令;
_controller=MXContainer.Instance.GetController(url,ref\u paramz);
}
受保护的重写Java.Lang.Object DoInBackground(params Java.Lang.Object[]@params)
{
如果(_paramz==null)
{
Log.Info(FIDB.TAG_APP,“MonthChangeTask.DoInBackground():paramz=NULL”);
}
其他的
{
_控制器加载(_paramz);
}
返回true;
}
受保护的重写void OnPostExecute(Java.Lang.Object结果)
{
base.OnPostExecute(结果);
_progressDialog.disclose();
_.Model=(CalendarVM)_controller.GetModel();
如果(_changingInspector)
{
_._calendar.PermitSwitch=u.Model.Buttons.PermitsVisible;
_.\u calendar.ComplaintSwitch=\u.Model.Buttons.ComplaintsVisible;
_.\u calendar.ProjectSwitch=\u.Model.Buttons。