android中如何获取listview中其中一个Item中的进度条对象来更新下载进度?
1个回答
展开全部
public class MainActivity extends Activity
{
private ListView listView;
private DownloadAdapter adapter;
private List<String> lists = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < 10; i++)
{
lists.add("button" + i);
}
listView = (ListView) findViewById(R.id.listView1);
adapter = new DownloadAdapter(this, lists);
listView.setAdapter(adapter);
}
}
public class DownloadAdapter extends BaseAdapter
{
private Context context;
private List<String> list;
private LayoutInflater inflater;
public DownloadAdapter(Context context, List<String> list)
{
this.context = context;
this.list = list;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return list.get(position).hashCode();
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
MyHolder myHolder = null;
if (convertView == null)
{
convertView = inflater.inflate(R.layout.item, null);
myHolder = new MyHolder();
myHolder.bar = (ProgressBar) convertView
.findViewById(R.id.progressBar1);
myHolder.button = (Button) convertView.findViewById(R.id.button1);
convertView.setTag(myHolder);
}
else
{
myHolder = (MyHolder) convertView.getTag();
}
myHolder.button.setText(list.get(position));
MyDownLoadThread down = new MyDownLoadThread(myHolder.bar,
myHolder.button);
myHolder.button.setOnClickListener(down);
return convertView;
}
class MyHolder
{
private ProgressBar bar;
private Button button;
}
public class MyDownLoadThread extends Thread implements OnClickListener
{
private ProgressBar pBar;
private Button btn;
private int index = 0;
private boolean isStart = false;
private Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
pBar.setProgress(index);
}
};
public MyDownLoadThread(ProgressBar pBar, Button btn)
{
this.pBar = pBar;
this.btn = btn;
}
@Override
public void onClick(View v)
{
if (!isStart)
{
isStart = true;
btn.setText("开始下载");
pBar.setMax(100);
this.start();
}
}
@Override
public void run()
{
while (index <= 100)
{
index++;
handler.sendEmptyMessage(0);
try
{
sleep(1000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
if (index > 100)
{
btn.setText("下载完成");
break;
}
}
}
}
}
简单的demo 仅供参考,希望对你有用
{
private ListView listView;
private DownloadAdapter adapter;
private List<String> lists = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < 10; i++)
{
lists.add("button" + i);
}
listView = (ListView) findViewById(R.id.listView1);
adapter = new DownloadAdapter(this, lists);
listView.setAdapter(adapter);
}
}
public class DownloadAdapter extends BaseAdapter
{
private Context context;
private List<String> list;
private LayoutInflater inflater;
public DownloadAdapter(Context context, List<String> list)
{
this.context = context;
this.list = list;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return list.get(position).hashCode();
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
MyHolder myHolder = null;
if (convertView == null)
{
convertView = inflater.inflate(R.layout.item, null);
myHolder = new MyHolder();
myHolder.bar = (ProgressBar) convertView
.findViewById(R.id.progressBar1);
myHolder.button = (Button) convertView.findViewById(R.id.button1);
convertView.setTag(myHolder);
}
else
{
myHolder = (MyHolder) convertView.getTag();
}
myHolder.button.setText(list.get(position));
MyDownLoadThread down = new MyDownLoadThread(myHolder.bar,
myHolder.button);
myHolder.button.setOnClickListener(down);
return convertView;
}
class MyHolder
{
private ProgressBar bar;
private Button button;
}
public class MyDownLoadThread extends Thread implements OnClickListener
{
private ProgressBar pBar;
private Button btn;
private int index = 0;
private boolean isStart = false;
private Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
pBar.setProgress(index);
}
};
public MyDownLoadThread(ProgressBar pBar, Button btn)
{
this.pBar = pBar;
this.btn = btn;
}
@Override
public void onClick(View v)
{
if (!isStart)
{
isStart = true;
btn.setText("开始下载");
pBar.setMax(100);
this.start();
}
}
@Override
public void run()
{
while (index <= 100)
{
index++;
handler.sendEmptyMessage(0);
try
{
sleep(1000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
if (index > 100)
{
btn.setText("下载完成");
break;
}
}
}
}
}
简单的demo 仅供参考,希望对你有用
追问
你传的 MyDownLoadThread down = new MyDownLoadThread(myHolder.bar,myHolder.button);
这里面的参数确定是你点击的那个ITEM里面的对象?而不是每个Item里面的对象?
追答
是的 我已经在我的手机上试过了 你自己也可以试一试
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询