ProgressDialog
[功能]
ProgressDialog 也是一种Dialog
一般 在出现ProgressDialog 后台都会再开辟Thread 来做一些耗时的工作 我演示的是从100数到0 这段时间既不太长 不用浪费时间等待 同时 也能明显地看出效果
[代码]
1. ProgressDialog 使用
public void startProgress(){ //to start Progress pd = ProgressDialog.show(this, "loop from 100 to 0!", "is looping...", true, false); //pd.setIcon(R.drawable.icon); }
2. 开辟一个Thread来从100数到0 在数完后 关闭ProgressDialog
public class TaskLoop implements Runnable { @Override public void run() { // TODO Auto-generated method stub loop(5000); messageListener.sendEmptyMessage(TASK_LOOP_COMPLETE); } } public void loop(long i){ long j = i; while(j>0){ Log.d("tag",j+""); j = j-1; } }
3. 运行该Thread
Thread loop = new Thread(new TaskLoop()); loop.start();
4.定义一个Handler 用于接受 关闭ProgressDialog 的通知
private Handler messageListener = new Handler(){ public void handleMessage(Message msg) { switch(msg.arg1){ case TASK_LOOP_COMPLETE: pd.dismiss(); break; } } };
[所有代码]
public class MyProgressUsage extends Activity { public final static int TASK_LOOP_COMPLETE = 0; ProgressDialog pd; TextView tv; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViewById(R.id.start).setOnClickListener(new OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub startLoop(); } }); } public void startLoop(){ startProgress(); Thread loop = new Thread(new TaskLoop()); loop.start(); } public class TaskLoop implements Runnable { @Override public void run() { // TODO Auto-generated method stub loop(5000); messageListener.sendEmptyMessage(TASK_LOOP_COMPLETE); } } public void startProgress(){ //to start Progress pd = ProgressDialog.show(this, "loop from 100 to 0!", "is looping...", true, false); //pd.setIcon(R.drawable.icon); } //to do some time-cost task public void loop(long i){ long j = i; while(j>0){ Log.d("tag",j+""); j = j-1; } } private Handler messageListener = new Handler(){ public void handleMessage(Message msg) { switch(msg.arg1){ case TASK_LOOP_COMPLETE: pd.dismiss(); break; } } }; }
http://dl.iteye.com/upload/picture/pic/52676/11cd6622-cb13-337a-bd52-007185e972c0.png
over!
GridView
[功能]
以前提及过GridView 说也是一种AdapterView 和ListView有点像 今天花了时间 用了一些 有点心得 和大家分享分享
[思路]
1. 既然和ListView像 那么应该还是通过 setAdapter() 来设置吧
2. 因为我想显示的是一些图片信息 必须用到ImageView 不是默认的Adapter用到的TextView 所以只能自己扩展了
[代码]
1. 定义包含GridView 的 main.xmk
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <GridView android:id="@+id/gride" android:layout_width="fill_parent" android:layout_height="wrap_content" android:numColumns="3" android:verticalSpacing="5dip" /> </LinearLayout>
这行应该注意一下:
android:numColumns="3"
用来设定GridView每行显示的View数目 如果没有这行 会默认每行显示一个View 和ListView 的一样
2. 自定义 class ImageList extends BaseAdapter 其中主要是:
用于显示目标ImageView
public class ImageList extends BaseAdapter { Activity activity; //construct public ImageList(Activity a ) { activity = a; } @Override public int getCount() { // TODO Auto-generated method stub return image.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return image[position]; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ImageView iv = new ImageView(activity); iv.setImageResource(image[position]); return iv; } }
3. 给GridView指定Adapter
GridView gv = (GridView) findViewById(R.id.gride); ImageList adapter = new ImageList(this); gv.setAdapter(adapter);
所以最后效果图是这样的 网格布局
不太懂你的意思 你要控制滚动条干嘛? 如果GridView包含的View 太多的话 滚动条会自动出现 否则 会隐藏的
我是想让程序记住滚动条的位置,免得用户每次都要拖滚动条。
我是想让程序记住滚动条的位置,免得用户每次都要拖滚动条。
你是想让程序记得上次选中的item
下次再打开 直接到目标 是吗?
如果是 那不如这样做 或许会简单一点
1. GridView.setSelection(int position) 每次新打开GridView 调用之
2. SharePerference 保存最后的item position
如果不是 就不知道了 因为几乎没人会关心这个吧!
<p>就像一个DataGrid,有header</p>
<p> </p>
<p> </p>
<p>
</p>
<table border="1" width="439" frame="rows"><tbody>
<tr>
<td>Username</td>
<td>Old</td>
<td>Sex</td>
</tr>
<tr>
<td>a</td>
<td>23</td>
<td>Boy</td>
</tr>
<tr>
<td>b</td>
<td>24</td>
<td>Gril</td>
</tr>
</tbody></table>
<p> </p>
<p>如何使用GridView实现,比较急?</p>
就像一个DataGrid,有header
<table border="1" width="439" frame="rows"><tbody>
<tr>
<td>Username</td>
<td>Old</td>
<td>Sex</td>
</tr>
<tr>
<td>a</td>
<td>23</td>
<td>Boy</td>
</tr>
<tr>
<td>b</td>
<td>24</td>
<td>Gril</td>
</tr>
</tbody></table>
如何使用GridView实现,比较急?
1.当然可以 GridView 只是一种AdapterVire
2. 何谓AdapterVire ,参考ListView ,通俗说法:适配器 能接受不固定数据 然后通过之显示
3. 如何实现: 先定义BaseAdapter 然后通过setAdapter()使用之
请原谅我在这挖坟了
多谢!
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {}
}
List values = urlConnection.getHeaderFields().get("content-Length")
if (values != null && !values.isEmpty()) {
// getHeaderFields() returns a Map with key=(String) header
// name, value = List of String values for that header field.
// just use the first value here.
String sLength = (String) values.get(0);
if (sLength != null) {
//parse the length into an integer...
...
}
下面是可以参考的代码
Download.java import java.io.*; import java.net.*; import java.util.*; // This class downloads a file from a URL. class Download extends Observable implements Runnable { // Max size of download buffer. private static final int MAX_BUFFER_SIZE = 1024; // These are the status names. public static final String STATUSES[] = {"Downloading", "Paused", "Complete", "Cancelled", "Error"}; // These are the status codes. public static final int DOWNLOADING = 0; public static final int PAUSED = 1; public static final int COMPLETE = 2; public static final int CANCELLED = 3; public static final int ERROR = 4; private URL url; // download URL private int size; // size of download in bytes private int downloaded; // number of bytes downloaded private int status; // current status of download // Constructor for Download. public Download(URL url) { this.url = url; size = -1; downloaded = 0; status = DOWNLOADING; // Begin the download. download(); } // Get this download's URL. public String getUrl() { return url.toString(); } // Get this download's size. public int getSize() { return size; } // Get this download's progress. public float getProgress() { return ((float) downloaded / size) * 100; } // Get this download's status. public int getStatus() { return status; } // Pause this download. public void pause() { status = PAUSED; stateChanged(); } // Resume this download. public void resume() { status = DOWNLOADING; stateChanged(); download(); } // Cancel this download. public void cancel() { status = CANCELLED; stateChanged(); } // Mark this download as having an error. private void error() { status = ERROR; stateChanged(); } // Start or resume downloading. private void download() { Thread thread = new Thread(this); thread.start(); } // Get file name portion of URL. private String getFileName(URL url) { String fileName = url.getFile(); return fileName.substring(fileName.lastIndexOf('/') + 1); } // Download file. public void run() { RandomAccessFile file = null; InputStream stream = null; try { // Open connection to URL. HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // Specify what portion of file to download. connection.setRequestProperty("Range", "bytes=" + downloaded + "-"); // Connect to server. connection.connect(); // Make sure response code is in the 200 range. if (connection.getResponseCode() / 100 != 2) { error(); } // Check for valid content length. int contentLength = connection.getContentLength(); if (contentLength < 1) { error(); } /* Set the size for this download if it hasn't been already set. */ if (size == -1) { size = contentLength; stateChanged(); } // Open file and seek to the end of it. file = new RandomAccessFile(getFileName(url), "rw"); file.seek(downloaded); stream = connection.getInputStream(); while (status == DOWNLOADING) { /* Size buffer according to how much of the file is left to download. */ byte buffer[]; if (size - downloaded > MAX_BUFFER_SIZE) { buffer = new byte[MAX_BUFFER_SIZE]; } else { buffer = new byte[size - downloaded]; } // Read from server into buffer. int read = stream.read(buffer); if (read == -1) break; // Write buffer to file. file.write(buffer, 0, read); downloaded += read; stateChanged(); } /* Change status to complete if this point was reached because downloading has finished. */ if (status == DOWNLOADING) { status = COMPLETE; stateChanged(); } } catch (Exception e) { error(); } finally { // Close file. if (file != null) { try { file.close(); } catch (Exception e) {} } // Close connection to server. if (stream != null) { try { stream.close(); } catch (Exception e) {} } } } // Notify observers that this download's status has changed. private void stateChanged() { setChanged(); notifyObservers(); } }