|
|
|
Android listview image url from JSON รันภาพวนซ้ำคะในแถวแรกคะ |
|
|
|
|
|
|
|
เวลารันแล้ว ในรูปภาพในลิสแถวแรกมันวนซ้ำกันแล้วค่อยมาแสดงในแถวที่ 2 คะ พอมีวิธีแก้ไหมคะ ขอบคุณคะ
Code (Android-Java)
public class HomepageActivity extends Activity {
String selected="";
ArrayList<HashMap<String, String>> myArr;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_tab);
String jsString = "[{\"radioID\":\"0\",\"radioPic\":\"http://www.thainht.org/upload/images/radio-icon(1).png\",\"radioName\":\"Seed FM\",\"Desc\":\"FM 97.5 MHz-Bangkok\"}"
+ ",{\"radioID\":\"1\",\"radioPic\":\"http://radiobeta.com/images/radios/big/logo0_1331153395-virgins.png\",\"radioName\":\"95.5 Virgin HITZ\",\"Desc\":\"FM 95.5 MHz-Bangkok\"}]";
JSONArray data ;
try{
myArr = new ArrayList<HashMap<String,String>>();
data=new JSONArray(jsString);
for(int i =0;i<data.length();i++)
{
JSONObject jsonObject = data.getJSONObject(i);
HashMap<String, String>map ;
map=new HashMap<String, String>();
map.put("name", jsonObject.getString("radioName"));
map.put("pic", jsonObject.getString("radioPic"));
myArr.add(map);
}
}catch (Exception e) {
// TODO: handle exception
Log.e("error", jsString);
}
list=(ListView) findViewById(R.id.listfav);
list.setAdapter(new AdapterTest(this));
}
public class AdapterTest extends BaseAdapter {
Context context;
public AdapterTest(Context c){
this.context=c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return myArr.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(arg1==null)
{
arg1=inflater.inflate(R.layout.list_item, null);
}
TextView txttitle =(TextView) arg1.findViewById(R.id.title);
txttitle.setText(myArr.get(arg0).get("name"));
ImageView img = (ImageView)arg1.findViewById(R.id.icon);
new DownloadImageTask(img).execute(myArr.get(arg0).get("pic"));
return arg1;
}
}
Code (Android-Java)
class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
Tag : Mobile
|
|
|
|
|
|
Date :
2013-03-28 21:57:38 |
By :
tmk_nookky |
View :
1415 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
น่าจะผิดตรงอ่าน JSON ครับ มันน่าจะ Loop แค่รายการแรกครับ ลองดูบทความนี้ครับ
Code (Android-Java)
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("CustomerID", c.getString("CustomerID"));
map.put("Name", c.getString("Name"));
map.put("Email", c.getString("Email"));
map.put("CountryCode", c.getString("CountryCode"));
map.put("Budget", c.getString("Budget"));
map.put("Used", c.getString("Used"));
MyArrList.add(map);
}
Go to : Android and Web Server ของ PHP กับ MySQL แสดงบน ListView ในรูปแบบของ JSON
|
|
|
|
|
Date :
2013-03-29 06:51:49 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|