package net.apptools.jsonnetwork;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import util_parse.HttpParser;
import util_parse.RowData;
import util_parse.GetImage;
public class MainActivity extends AppCompatActivity {
ArrayList<RowData> arrayList;
ListView listView;
Adapter adapter;
String serverUrl = "http://study.saybox.net:8080/lky/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayList=new ArrayList<>();
HttpParser httpParser =
new HttpParser(serverUrl + "json.php");
String str = httpParser.JsonParse();
try {
JSONArray jsonArray = new JSONArray(str);
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
int idx = jsonObject.getInt("idx");
String category = jsonObject.getString("category");
String img = jsonObject.getString("img");
int sort = jsonObject.getInt("sort");
RowData rowData=new RowData(idx,category,img,sort);
arrayList.add(rowData);
}
} catch (JSONException e) {
e.printStackTrace();
}
listView = (ListView) findViewById(R.id.listView);
adapter = new Adapter();
listView.setAdapter(adapter);
}
class Adapter extends BaseAdapter {
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = View.inflate(MainActivity.this, R.layout.listview_row, null);
}
TextView category = (TextView) convertView.findViewById(R.id.category);
category.setText(arrayList.get(position).getCategory());
TextView idx = (TextView) convertView.findViewById(R.id.idx);
idx.setText(arrayList.get(position).getIdx() + "");
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);
String imgUrl = serverUrl + arrayList.get(position).getImg();
Bitmap bitmap = new GetImage().bitmap(imgUrl);
imageView.setImageBitmap(bitmap);
return convertView;
}
}
}
[ÀÌ °Ô½Ã¹°Àº ÀÌ°æ¿ë´Ô¿¡ ÀÇÇØ 2016-08-11 21:26:17 ºÎ»ê18±â¿¡¼ º¹»ç µÊ]
[ÀÌ °Ô½Ã¹°Àº ÀÌ°æ¿ë´Ô¿¡ ÀÇÇØ 2016-12-22 20:52:32 ºÎ»ê20±â¿¡¼ º¹»ç µÊ]