樣品列表視圖:
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import com.example.beans.SongInfo;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class BuddiesActivity extends Activity {
int statusClass = 2;
String taskResult="test";
ListView list;
LazyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new HttpAsyncTask().execute("http://en.wikipedia.org/w/api.php?format=json&action=query&titles=Main%20Page&prop=revisions&rvprop=content");
do {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (statusClass == 2);
setContentView(R.layout.activity_buddies);
// TextView textview=(TextView)findViewById(R.id.buddies);
// textview.setText(taskResult);
ArrayList<SongInfo> songsList1 = new ArrayList<SongInfo>();
System.out.println("Emply ArrayList Created");
songsList1=getListData();
System.out.println("Data added to arrayList :"+songsList1.size()+" Data is: "+songsList1);
list=(ListView)findViewById(R.id.list);
System.out.println("ListView Got");
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, songsList1);
System.out.println("Object Of Lazy Adapter created");
list.setAdapter(adapter);
System.out.println("adapter set..");
// Click event for single list row
/*list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
initiatePopupWindow();
}
});*/
}
public ArrayList<SongInfo> getListData(){
ArrayList<SongInfo> list=new ArrayList<SongInfo>();
list.add(new SongInfo("Song", "1", "Someone Like You", "Adele", "4:47", "http://api.androidhive.info/music/images/adele.png"));
list.add(new SongInfo("Song", "2", "Space Bound", "Eminem", "4:34", "http://api.androidhive.info/music/images/adele.png"));
list.add(new SongInfo("Song", "3", "Stranger In Moscow", "Michael Jackson", "5:55", "http://api.androidhive.info/music/images/adele.png"));
list.add(new SongInfo("Song", "4", "Love The Way You Lie", "Rihanna", "4:23", "http://api.androidhive.info/music/images/adele.png"));
list.add(new SongInfo("Song", "5", "Someone Like You", "Adele", "4:47", "http://api.androidhive.info/music/images/adele.png"));
list.add(new SongInfo("Song", "6", "Space Bound", "Eminem", "4:34", "http://api.androidhive.info/music/images/adele.png"));
list.add(new SongInfo("Song", "7", "Stranger In Moscow", "Michael Jackson", "5:55", "http://api.androidhive.info/music/images/adele.png"));
list.add(new SongInfo("Song", "8", "Love The Way You Lie", "Rihanna", "4:23", "http://api.androidhive.info/music/images/adele.png"));
list.add(new SongInfo("Song", "9", "Someone Like You", "Adele", "4:47", "http://api.androidhive.info/music/images/adele.png"));
list.add(new SongInfo("Song", "10", "Space Bound", "Eminem", "4:34", "http://api.androidhive.info/music/images/adele.png"));
list.add(new SongInfo("Song", "11", "Stranger In Moscow", "Michael Jackson", "5:55", "http://api.androidhive.info/music/images/adele.png"));
list.add(new SongInfo("Song", "12", "Love The Way You Lie", "Rihanna", "4:23", "http://api.androidhive.info/music/images/adele.png"));
return list;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.buddies, menu);
return true;
}
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
AlertDialog.Builder builder;
protected void onPreExecute() {
super.onPreExecute();
builder = new AlertDialog.Builder(BuddiesActivity.this);
}
@Override
protected String doInBackground(String... urls) {
return POST(urls[0]);
}
@Override
protected void onPostExecute(String result) {
}
}
public String POST(String url) {
System.out.println("I am in post data./..............................");
HttpClient httpclient = new DefaultHttpClient();
HttpGet httppost = new HttpGet(url);
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
int status = response.getStatusLine().getStatusCode();
System.out.println("Status is : " + status);
// ParseProductJson parseJson = new ParseProductJson();
if (status == 200) {
result = EntityUtils.toString(response.getEntity());
System.out.println("################result1###############################"+ result);
/*FileOutputStream fos = openFileOutput("productsJson.json",
Context.MODE_PRIVATE);
fos.write(result.getBytes());
fos.close();*/
System.out.println("Done");
taskResult=result;
statusClass = status;
} else {
statusClass = 400;
result = "Did not work!";
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
當您使用ListActivity你不需要與XML在其中列表視圖。 – Sunny 2012-02-10 15:02:22