我尝试从MySQL
检索图像并将其显示为listView
但未成功。我错过了什么吗?
这就是我的尝试。
表staff_benefit
public void BuildEditStaffList(final String id)
{
class GetDataJSON extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://192.168.1.7/Android/CRUD/staffRetrieve.php?id="+id);
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
@Override
protected void onPostExecute(String result){
myJSON=result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
protected void showList(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
details= jsonObj.getJSONArray(Config.TAG_RESULTS);
for(int i=0;i<details.length();i++){
JSONObject c = details.getJSONObject(i);
String type = c.getString(Config.TAG_TYPE);
String description = c.getString(Config.TAG_DESCRIPTION);
String amount=c.getString(Config.TAG_AMOUNT);
String image=c.getString(Config.TAG_IMAGE);
byte[] data = Base64.decode(image, 0);
Bitmap b =BitmapFactory.decodeByteArray(data, 0, data.length);
Drawable d=new BitmapDrawable(getResources(),b);
int ID=c.getInt(Config.TAG_ID);
//v.setImageBitmap(b);
HashMap<String,Object> info = new HashMap<String,Object>();
info.put(Config.TAG_TYPE, type);
info.put(Config.TAG_DESCRIPTION, description);
info.put(Config.TAG_AMOUNT,amount);
info.put(Config.TAG_IMAGE,d);
info.put(Config.TAG_ID,ID+"");
StaffDetails.add(info);
}
ListAdapter adapter = new SimpleAdapter(
getActivity(),StaffDetails, R.layout.retrieve_staff,
new String[]{Config.TAG_IMAGE,Config.TAG_TYPE,Config.TAG_AMOUNT,Config.TAG_DESCRIPTION},
new int[]{R.id.image,R.id.type,R.id.amount,R.id.description}
);
listViewEdit.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
staffRetrieve.php
<?php
define('HOST','127.0.0.1:3307');
define('USER','root');
define('PASS','');
define('DB','androiddb');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('unable to connect');
$tws = $_GET['id'];
$sql = "select * from staff_benefit WHERE ts_id= '". $tws."' ";
$res = mysqli_query($con,$sql);
$result=array();
while($row=mysqli_fetch_array($res)){
array_push($result,array('id'=>$row[0],'type'=>$row[1],'amount'=>$row[2],'description'=>$row[3],'image'=>$row[4],
'ts_id'=>$row[5]));
}
echo (json_encode(array("result"=>$result)));
mysqli_close($con);
?>
非常感谢任何帮助。
显示文字但不显示图片
的ListView
图像应显示在左侧。
答案 0 :(得分:1)
被修改
首先创建Staff对象。
public class Staff {
private int id;
private String image;
private String type;
private String amount;
private String description;
public Staff(int id, String type, String description, String amount, String image) {
this.id = id;
this.image = image;
this.type = type;
this.amount = amount;
this.description = description;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
以下是Adapter.class
public class Adapter extends ArrayAdapter<Staff> {
Activity context;
List<Staff> staffs;
static class ViewHolder {
public ImageView image;
public TextView type;
public TextView amount;
public TextView description;
}
@Override
public int getCount() {
return staffs.size();
}
public Adapter(Activity context, List<Staff> staffs) {
super(context, R.layout.retrieve_staff, staffs);
this.context = context;
this.staffs = staffs;
}
@Override
public Staff getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
ViewHolder v = new ViewHolder();
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.retrieve_staff, null);
v.image = (ImageView)convertView.findViewById(R.id.imageview);
v.amount = (TextView)convertView.findViewById(R.id.amount);
v.type = (TextView)convertView.findViewById(R.id.type);
v.description = (TextView)convertView.findViewById(R.id.description);
convertView.setTag(v);
}
holder = (ViewHolder)convertView.getTag();
Log.v("TEST",staffs.get(position).getImage());
byte[] data = Base64.decode(staffs.get(position).getImage(), 0);
Bitmap b = BitmapFactory.decodeByteArray(data, 0, data.length);
Drawable d=new BitmapDrawable(context.getResources(),b);
holder.image.setImageDrawable(d);
holder.amount.setText(staffs.get(position).getAmount());
holder.type.setText(staffs.get(position).getType());
holder.description.setText(staffs.get(position).getDescription());
return convertView;
}
}
这是showList()
方法
protected void showList(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
details= jsonObj.getJSONArray(Config.TAG_RESULTS);
for(int i=0;i<details.length();i++){
JSONObject c = details.getJSONObject(i);
String type = c.getString(Config.TAG_TYPE);
String description = c.getString(Config.TAG_DESCRIPTION);
String amount=c.getString(Config.TAG_AMOUNT);
String image=c.getString(Config.TAG_IMAGE);
Staff staff = new Staff(ID,type,description,amount,image);
staffs.add(staff);
}
Adapter adapter = new Adapter(getActivity(),staffs);
listViewEdit.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}