我想做什么:获取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;
}
}
当然这不起作用。 1.我不明白如何获得ImageView作为其src的drawable的id。 我不明白params是如何在AsyncTask中传递的; onPreExecute应该接收UI的东西,doInbackground应该接收它来比较它并返回应该设置为ImageView的drawable int,并且onPreExecute应该将它设置为ImageView。
答案 0 :(得分:0)
我不明白如何获得ImageView作为其src的drawable的id。
我没有必要这样做,所以可能无效,但你应该可以使用
imageView.getDrawable().getId();
我不明白params是如何在AsyncTask中传递的;
task.execute()
收到doInBackground()
传递的内容。如果您拨打publishProgress()
,则会发送任何一个参数,onProgressUpdate()
会收到。 doInBackground()
中返回的数据由onPostExecute()
收到。
AsyncTask
,你知道,不应该为此而需要,但我知道你说你想学习如何使用它。除了这两件事之外,我对你究竟遇到什么问题有点困惑所以请详细说明我是否错过了什么。
AsyncTask Example(如果有帮助的话)
答案 1 :(得分:0)
如果您想学习ASyncTask,您应该完成其他任务。 如果我想学习ASyncTask,我会用进度条或其他东西做对话。
编辑: 正如Samus Arin对主要帖子的评论一样,您应该始终跟踪您正在显示的图像。所以改为使用像
这样的东西if(currentImage == R.Drawable.image1){
image.setImageResource(R.Drawable.image2);
}else{
image.setImageResource(R.Drawable.image1);
}
答案 2 :(得分:0)
为了它的价值,看看我正在用AsyncTask做什么,也许它会给你一些想法。
这是Monodroid / C#代码,不是原始Java / Android(但语法非常接近)。因此,内部类不会获得对其包含对象的隐式引用,因此我传入一个(在构造函数中称为outer)。我选择将其命名为“_”,作为.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] );
}
}
}