private void showgift() {
Intent intent = getIntent();
final String Mid = intent.getStringExtra("MemberID");
// TODO Auto-generated method stub
String url = "----------------------------";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Mid", Mid.toString()));
try {
JSONArray data = new JSONArray(getJSONUrl(url,params));
final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, String>();
map.put("NAME_GIFT",c.getString("giftName"));
map.put("VALUE_GIFT",c.getString("giftValue"));
map.put("GID", c.getString("giftMem"));
MyArrList.add(map);
final String status = c.getString("status");
final String im ="";
if(status == "A"){
map.put("STATUS","cad_kid_icons_a");
}else{
map.put("STATUS","cad_kid_icons_b");
}
final ListView lisView1 = (ListView)findViewById(R.id.lisGift);
lisView1.setAdapter(new ImageAdapter(this,MyArrList));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class ImageAdapter extends BaseAdapter
{
private Context context;
private ArrayList<HashMap<String, String>> MyArr = new ArrayList<HashMap<String, String>>();
public ImageAdapter(Context c, ArrayList<HashMap<String, String>> list)
{
// TODO Auto-generated method stub
context = c;
MyArr = list;
}
public int getCount() {
// TODO Auto-generated method stub
return MyArr.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Intent intent = getIntent();
final String Mid = intent.getStringExtra("MemberID");
final String MemGift = intent.getStringExtra("giftMem");
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.corlumgift, null);
}
ImageView imageView = (ImageView) convertView.findViewById(R.id.Imview);
imageView.getLayoutParams().height = 50;
imageView.getLayoutParams().width = 100;
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
int ResID = context.getResources().getIdentifier(MyArr.get(position).get("STATUS"), "drawable", context.getPackageName());
imageView.setImageResource(ResID);
TextView txtPosition = (TextView) convertView.findViewById(R.id.tGName);
txtPosition.setText(MyArr.get(position).get("NAME_GIFT"));
TextView txtPicName = (TextView) convertView.findViewById(R.id.tGValue);
txtPicName.setText(MyArr.get(position).get("VALUE_GIFT"));
final String Gids = (MyArr.get(position).get("GID"));
ImageButton btnDelect = (ImageButton)convertView.findViewById(R.id.BtnDelect);
final AlertDialog.Builder adb1 = new AlertDialog.Builder(TabGift.this);
final AlertDialog.Builder adb2 = new AlertDialog.Builder(TabGift.this);
btnDelect.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
adb1.setTitle("ต้องการลบ หรือไม่ ");
adb1.setMessage("ต้องการลบรึเปล่า ");
adb1.setNegativeButton("Cancel", null);
adb1.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Request to Delete data.
String url = "http://203.150.224.249/cad_save/android/delectgift.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Mid", Mid.toString()));
params.add(new BasicNameValuePair("Gid", Gids.toString()));
Toast.makeText(getBaseContext(), Gids, Toast.LENGTH_SHORT);
String resultServer = getJSONUrl(url,params);
String strStatusID = "0";
String strError = "Unknow Status";
JSONObject c;
try {
c = new JSONObject(resultServer);
strStatusID = c.getString("StatusID");
strError = c.getString("Error");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Prepare Delete
if(strStatusID.equals("0"))
{
// Dialog
adb2.setTitle("Error! ");
adb2.setPositiveButton("Close", null);
adb2.setMessage(strError);
adb2.show();
}
else
{
Toast.makeText(TabGift.this, "ลบเรียบร้อยแล้ว", Toast.LENGTH_SHORT).show();
showgift();// reload data again
}
}});
adb1.show();
}
});
return convertView;
}
}
จากโค้ด ผมส่งค่า status "A" และ "B" และอยากให้มันเรียก รูปขึ้นมาเป็น ของ a และ ของ b จากเงื่อนไข
ขอเพิ่มอีกหน่อยครับ ถ้้าเปลี่ยนจาก Imageview เป็น buttonimage จะทำอย่างไรครับ