0
我正在使用圖像+文字的自定義列表視圖,這裏是我的代碼:獲得圖像的ListView
listDoctor.java
public void cek(){
String url_select = "http://10.0.2.2/BloodGlucose/selectDoctor.php";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url_select);
//parameter
ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
try {
//add parameter
httpPost.setEntity(new UrlEncodedFormEntity(param));
HttpResponse httpRespose = httpClient.execute(httpPost);
HttpEntity httpEntity = httpRespose.getEntity();
//read content
InputStream in = httpEntity.getContent();
BufferedReader read = new BufferedReader(new InputStreamReader(in));
String content = "";
String line = "";
while((line = read.readLine())!=null){
content += line;
}
Log.d("ADBUG", "content: "+content);
//json
if(!content.equals("null")){
try {
JSONArray jArr = new JSONArray(content);
for(int i=0;i<jArr.length();i++){
JSONObject jObj = jArr.getJSONObject(i);
String id = jObj.getString("_id");
String name = jObj.getString("name");
String dateofbirth = jObj.getString("dateofbirth");
String phone = jObj.getString("telp");
String address = jObj.getString("clinicaddress");
String file = jObj.getString("file");
String uname = jObj.getString("username_doctor");
String lulusan = jObj.getString("lulusan");
String clinicname = jObj.getString("clinicname");
names.add(name);
date.add(dateofbirth);
telp.add(phone);
clinic.add(address);
usernamedoctor.add(uname);
namaklinik.add(clinicname);
graduate.add(lulusan);
}
setListAdapter(new DoctorArrayAdapter(this, names));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
Toast.makeText(this, "Error", Toast.LENGTH_LONG).show();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
DoctorArrayAddapter.java
package research.android.bloodglucose;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import research.android.bloodglucose.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class DoctorArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final ArrayList<String> values;
private ImageView imageView;
public DoctorArrayAdapter(Context context, ArrayList<String> names) {
super(context, R.layout.list_row, names);
this.context = context;
this.values = names;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_row, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.DoctorName);
ImageView imageView = (ImageView) rowView.findViewById(R.id.list_image);
textView.setText(values.get(position));
// Change icon based on name
String s = values.get(position);
System.out.println(s);
/*if (s.equals("WindowsMobile")) {
imageView.setImageResource(R.drawable.windowsmobile_logo);
} else if (s.equals("iOS")) {
imageView.setImageResource(R.drawable.ios_logo);
} else if (s.equals("Blackberry")) {
imageView.setImageResource(R.drawable.blackberry_logo);
} else {
imageView.setImageResource(R.drawable.android_logo);
}*/
cek();
return rowView;
}
public void cek(){
String url_select = "http://10.0.2.2/BloodGlucose/selectDoctor.php";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url_select);
//parameter
ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
try {
//add parameter
httpPost.setEntity(new UrlEncodedFormEntity(param));
HttpResponse httpRespose = httpClient.execute(httpPost);
HttpEntity httpEntity = httpRespose.getEntity();
//read content
InputStream in = httpEntity.getContent();
BufferedReader read = new BufferedReader(new InputStreamReader(in));
String content = "";
String line = "";
while((line = read.readLine())!=null){
content += line;
}
Log.d("ADBUG", "content: "+content);
//json
if(!content.equals("null")){
try {
JSONArray jArr = new JSONArray(content);
for(int i=0;i<jArr.length();i++){
JSONObject jObj = jArr.getJSONObject(i);
String id = jObj.getString("_id");
String name = jObj.getString("name");
String dateofbirth = jObj.getString("dateofbirth");
String phone = jObj.getString("telp");
String address = jObj.getString("clinicaddress");
String file = jObj.getString("file");
String uname = jObj.getString("username_doctor");
String lulusan = jObj.getString("lulusan");
String clinicname = jObj.getString("clinicname");
String ed = "http://10.0.2.2/BloodGlucose/img/"+uname+".jpg";
URL aURL = new URL(ed);
final URLConnection conn = aURL.openConnection();
conn.connect();
final BufferedInputStream bis = new BufferedInputStream(
conn.getInputStream());
final Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
imageView.setImageBitmap(bm);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
try {
URL imageUrl = new URL(url);
InputStream is = (InputStream) imageUrl.getContent();
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
問題是,當我運行模擬器,圖像沒有顯示,它只顯示文本,任何人都可以幫助我?謝謝
你可能不希望這樣做'content.equals(「空」)' – njzk2
你是一個網絡在UI線程上連接。別。 – njzk2
,並且您在每個getView調用中調用cek()。這似乎很多,因爲它似乎沒有做任何事情後,第一個 – njzk2