我是Android开发的新手,我有这个Android活动类,它是一个带有简单适配器的ListView。活动显示带有标题和图像的产品列表。
我设法得到每个项目的标题并显示它们但是如何在没有任何其他类的情况下显示我的图像(我不想使用自定义适配器类)。
这是我的活动课程:
public class BookletsActivity extends Activity {
private ProgressDialog pDialog;
// URL to get JSON
private static String url = "url.json";
// JSON Node names
private static final String TAG_NAME = "shop_name";
private static final String TAG_ADDRESS_STRING = "shop_address";
private static final String TAG_URL = "shop_url";
private static final String TAG_BOOKLET_URL = "feuilletez";
private static final String TAG_ID = "id";
TextView shop_address;
TextView shop_name;
TextView shop_url;
ImageButton feuilletez;
private ListView list;
private ImageButton button1;
private ImageButton button2;
private ImageButton button3;
private ImageButton button4;
// contacts JSONArray
JSONArray contacts = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Portrait only
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_booklet_list);
// Calling async task to get json
new GetJson().execute();
}
/**
* Async task class to get json by making HTTP call
* */
private class GetJson extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(BookletsActivity.this);
pDialog.setMessage("Mise à jour...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
JSONArray array = null;
try {
array = new JSONArray(jsonStr);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
ArrayList<String> mythumbs = new ArrayList<String>();
ArrayList<String> myurls = new ArrayList<String>();
for (int i = 0; i <array.length(); i++){
JSONObject jsonObject = array.getJSONObject(i);
String id = jsonObject.getString("id");
System.out.println("id --->" + id);
String url = jsonObject.getString("url");
System.out.println("url --->" + url);
String created_at = jsonObject.getString("created_at");
System.out.println("created_at --->" + created_at);
String updated_at = jsonObject.getString("updated_at");
System.out.println("updated_at --->" + updated_at);
String title = jsonObject.getString("title");
System.out.println("title --->" + title);
String thumbnail_url = jsonObject.getString("thumbnail_url");
System.out.println("thumbnail_url --->" + thumbnail_url);
mythumbs.add(thumbnail_url);
String document_url = jsonObject.getString("document_url");
System.out.println("document_url --->" + document_url);
myurls.add(document_url);
String presented = jsonObject.getString("presented");
System.out.println("presented --->" + presented);
System.out.println("mythumbs --->" + mythumbs);
System.out.println("myurls --->" + myurls);
HashMap<String, String> map = new HashMap<String, String>();
if(title == "null"){
map.put(TAG_NAME, "Notre catalogue");
}
else {
map.put(TAG_NAME, title);
}
map.put(TAG_ADDRESS_STRING, thumbnail_url );
map.put(TAG_URL, url);
map.put(TAG_ID, id);
map.put(TAG_BOOKLET_URL, document_url);
oslist.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
private 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 myImage = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
myImage = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
myImage = Bitmap.createScaledBitmap(myImage, 300, 200, false);;
return myImage;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
list = (ListView) findViewById(R.id.list);
ListAdapter adapter = new SimpleAdapter(BookletsActivity.this, oslist,
R.layout.listvie_booklet_row, new String[] { TAG_NAME,
TAG_ADDRESS_STRING, TAG_BOOKLET_URL, TAG_ID }, new int[] { R.id.name_shop,
R.id.address_shop});
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(BookletsActivity.this, PdfReader.class);
// Toast.makeText(ShopsActivity.this, "You Clicked at "+oslist.get(+position).get("shop_url"), Toast.LENGTH_SHORT).show();
intent.putExtra("pdfurl",oslist.get(+position).get("feuilletez"));
intent.putExtra("pdfname",oslist.get(+position).get("id"));
overridePendingTransition(R.anim.animationin, R.anim.animationout);
startActivity(intent);
}
});
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
}
}
}
答案 0 :(得分:0)
您可以使用ArrayAdapter
仅显示行中的标题,但如果您要创建自定义行,则必须创建自定义适配器。
ArrayAdapter当数据源是数组时使用此适配器。通过 默认情况下,ArrayAdapter通过调用为每个数组项创建一个视图 每个项目上的toString()并将内容放在TextView中。
要自定义每个项目的外观,您可以覆盖 数组中对象的toString()方法。或者,创建一个视图 对于除TextView以外的其他项目(例如,如果 你想要每个数组项的ImageView,扩展ArrayAdapter class并重写getView()以返回所需的视图类型 每个项目。