求助:android开发中 如何设置、更新ListView中的ProgressBar的进度???

 我来答
yangxiaoyi00
2013-12-16 · TA获得超过303个赞
知道小有建树答主
回答量:280
采纳率:0%
帮助的人:132万
展开全部
主Activity:----------
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

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);

}

}

Adapter :
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class DownloadAdapter extends BaseAdapter
{
private Context context;
private List<String> list;
private LayoutInflater inflater;
private ExecutorService pool = Executors.newFixedThreadPool(2);

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);
myHolder.textView = (TextView) convertView
.findViewById(R.id.textView1);
convertView.setTag(myHolder);
}
else
{
myHolder = (MyHolder) convertView.getTag();
}

MyDownLoadThread down = new MyDownLoadThread(myHolder.bar,
myHolder.button, myHolder.textView, false);

myHolder.button.setOnClickListener(down);
return convertView;
}

class MyHolder
{
private ProgressBar bar;
private Button button;
private TextView textView;
}

public class MyDownLoadThread extends Thread implements OnClickListener
{

private ProgressBar pBar;
private Button btn;
private TextView textView;
private int index = 0;
private boolean isStart = false;

private Handler handler = new Handler()
{

@Override
public void handleMessage(Message msg)
{
if (isStart)
{
pBar.setProgress(index);
textView.setText(index + "%");
if (index == 100)
{
btn.setText("下载完成");
isStart = false;
}

}
}

};

public MyDownLoadThread(ProgressBar pBar, Button btn,
TextView textView, boolean b)
{
this.pBar = pBar;
this.btn = btn;
this.textView = textView;
isStart = true;
}

@Override
public void onClick(View v)
{
if (isStart)
{
isStart = true;
btn.setText("开始下载");
pBar.setMax(100);
pool.execute(this);
}

}

@Override
public void run()
{
while (index < 100)
{
index += 5;

if (isStart)
{
handler.sendEmptyMessage(0);
}
try
{
sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}

}

}
}

}

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>

</LinearLayout>
item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="Button" />

<RelativeLayout
android:id="@+id/RelativeLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/button1" >

<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#000000"
android:text="0%" />
</RelativeLayout>

</RelativeLayout>
g肯定Jk
2017-07-26 · TA获得超过2.4万个赞
知道大有可为答主
回答量:2.5万
采纳率:6%
帮助的人:2.5亿
展开全部
- -简单点就用CMS那些后台 你模块加好了 新增新闻会自己排列的 。如果自己写代码的话,PHP这些内容肯定都是通过SQL动态获得 你SQL语句按时间排序下 取最新的几十条显示不就行了嘛
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式