求助:android开发中 如何设置、更新ListView中的ProgressBar的进度???
2个回答
展开全部
主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>
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>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询