我有一个包含多个项目的listview,当我单击一个项目时,进度条应该开始递增,递增的数据来自intentservice,被resultreceiver截取,并且从结果接收器我需要更新进度条。
问题是我不知道怎么做。我试着在adapterlist类中直接升级,但没有成功。据我所知,异步任务需要在后台运行才能正常工作,但我不知道异步任务是如何获得正确的视图来递增的,或者它是如何递增的。
这是我的主要活动:
public class MainActivity extends AppCompatActivity {
ArrayList<PlaylistItem> arrayOfUsers = new ArrayList<PlaylistItem>();
PlaylistAdapter adapter;
ListView listView ;
String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter to convert the array to views
// Attach the adapter to a ListView
Intent downloadIntent = new Intent(this, DownloadService.class);
downloadIntent.putExtra("url", "http://10.0.2.2:8080/downloadPlaylist" );
downloadIntent.putExtra("type","playlist");
downloadIntent.putExtra("receiver", new DownloadReceiver(new Handler()));
startService(downloadIntent);
Intent intentExtras = getIntent();
String editJobID = intentExtras.getStringExtra("name");
name=intentExtras.getStringExtra("name");
if(editJobID!=null)
{
if (!checkPermission()) {
romaneasca(1,editJobID);
} else {
if (checkPermission()) {
requestPermissionAndContinue(editJobID);
} else {
romaneasca(1,editJobID);
}
}
}
}
public class DownloadReceiver extends ResultReceiver {
public DownloadReceiver(Handler handler) {
super(handler);
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
if (resultCode == UPDATE_PROGRESS) {
// vom updata progressbar-ul
Toast.makeText(MainActivity.this, "update progress",Toast.LENGTH_SHORT);
int res= (int) resultData.get("progress");
int pos= (int) resultData.get("position");
// adapter.progressBar.setProgress(res);
adapter.setProgress(res);
}
}
}
这是我的listview适配器
public class PlaylistAdapter extends ArrayAdapter<PlaylistItem> {
Context context;
ProgressBar progressBar;
public PlaylistAdapter(Context context, ArrayList<PlaylistItem> playlist) {
super(context, 0, playlist);
this.context = context;
}
View convertView2;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView2=convertView;
// Get the data item for this position
PlaylistItem user = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_music, parent, false);
}
// Lookup view for data population
TextView title = (TextView) convertView.findViewById(R.id.Title);
ImageButton downloaded = (ImageButton) convertView.findViewById(R.id.Download);
progressBar = (ProgressBar) convertView.findViewById(R.id.simpleProgressBar);
// Populate the data into the template view using the data object
title.setText(user.musicName);
if (user.downloaded)
{
downloaded.invalidate();
}
final int positionForListener = position;
final PlaylistItem play = user;
downloaded.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getContext(),Integer.toString(positionForListener) + "position clicked", Toast.LENGTH_SHORT).show();
Intent intentEditJob = new Intent(context,MainActivity.class);
intentEditJob.putExtra("name", play.musicName);
ProgressBar progressBar2=new ProgressBar(view.getContext());
progressBar2.setProgress(10);
context.startService(intentEditJob);
int incr=10;
}
});
return convertView;
}
public void setProgress(int progress)
{
progressBar = new ProgressBar(getContext());
progressBar.setProgress(progress);
}
downloadservice是执行工作并返回增量的服务。我不知道在哪里安装异步任务来升级进度条。到目前为止,代码没有崩溃,但也没有任何作用。如果有人对该怎么做有想法或建议?谢谢您!
暂无答案!
目前还没有任何答案,快来回答吧!