Java 通过JSON检索ImageView中的图像
我创建了一个通过JSON数据填充数组列表的活动,当用户单击一个项目数组时,会显示一个新活动,显示与该项目相关的信息。在帮助下,我只能在该页面上显示字符串 换句话说,在我的单个项目单击中,我能够检索字符串,这很讽刺,因为我能够通过数组列表中的JSON填充图像 在单项目活动的布局中,我创建了一个水平幻灯片库,其中包含4张图片,但希望每个图片都通过JSON数据填充 下面是填充数组列表并引用单项单击活动的活动代码Java 通过JSON检索ImageView中的图像,java,android,json,android-layout,android-activity,Java,Android,Json,Android Layout,Android Activity,我创建了一个通过JSON数据填充数组列表的活动,当用户单击一个项目数组时,会显示一个新活动,显示与该项目相关的信息。在帮助下,我只能在该页面上显示字符串 换句话说,在我的单个项目单击中,我能够检索字符串,这很讽刺,因为我能够通过数组列表中的JSON填充图像 在单项目活动的布局中,我创建了一个水平幻灯片库,其中包含4张图片,但希望每个图片都通过JSON数据填充 下面是填充数组列表并引用单项单击活动的活动代码 public class CasualEventsActivity extends
public class CasualEventsActivity extends Activity {
private static final String URL_WEB_SERVICE = "xxxxx";
private GridView gv;
private ArrayList<Events_List> container;
private ArrayList<Events_List> items;
public Uri list_item_bac;
public String list_item_name;
public String list_item_description;
public String list_item_location;
public String single_list_item_description;
public String list_item_price;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.events_list_layout);
gv = (GridView) findViewById(R.id.gridview);
container = new ArrayList<Events_List>();
//download JSON
listDownload();
GridView s = (GridView) findViewById(R.id.gridview);
s.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(CasualEventsActivity.this,CasualEventsSingleItemActivity.class);
intent.putExtra("list_item_name", container.get(position).getList_item_title());
intent.putExtra("list_item_location", container.get(position).getList_item_location());
intent.putExtra("single_list_item_description", container.get(position).getSingle_list_item_description());
startActivity(intent); //start Activity
}
});
}
public void listDownload(){
RequestQueue volley = Volley.newRequestQueue(this);
JsonObjectRequest json = new JsonObjectRequest(Method.GET, URL_WEB_SERVICE, null, ResponseListener(), ErrorListener());
volley.add(json);
}
private Response.Listener<JSONObject> ResponseListener() {
return new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
//your JSON Array
JSONArray array = response.getJSONArray("list_item");
for(int i = 0; i < array.length(); i++){
container.add(convertirAnuncio(array.getJSONObject(i)));
}
} catch (JSONException e) {
e.printStackTrace();
}
gv.setAdapter(new AdapterEvents(getApplicationContext(),container));
}
};
};
private Response.ErrorListener ErrorListener() {
return new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) { }
};
}
//object JSON
private final Events_List convertirAnuncio(JSONObject obj) throws JSONException {
long id = obj.getLong("id"); //id
String list_item_name = obj.getString("list_item_name");
String list_item_location = obj.getString("list_item_location");
String list_item_description = obj.getString("list_item_description");
String single_list_item_description = obj.getString("single_list_item_description");
Uri uri = Uri.parse(obj.getString("list_item_bac"));
return new Events_List(id, list_item_location, single_list_item_description,list_item_name,list_item_description,list_item_price, uri);
}
}
特别是,下面是我希望JSON数据与ImageView关联的行
ImageView hsvimage1 = (ImageView) findViewById(R.id.hsvimage1);
ImageView hsvimage2 = (ImageView) findViewById(R.id.hsvimage2);
ImageView hsvimage3 = (ImageView) findViewById(R.id.hsvimage3);
单项布局活动代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/blue_bac3"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:layout_marginRight="15dp"
android:alpha="0.9"
android:paddingBottom="3dp"
android:shadowColor="#000000"
android:shadowDx="3"
android:shadowDy="3"
android:shadowRadius="0.01"
android:textColor="#82CAFF"
android:textSize="24sp" />
<TextView
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:layout_centerHorizontal="true"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:alpha="0.8"
android:paddingBottom="5dp"
android:shadowColor="#000000"
android:shadowDx="3"
android:shadowDy="3"
android:shadowRadius="0.01"
android:textAlignment="center"
android:textColor="#f2f2f2"
android:textSize="16sp" />
<ImageView
android:id="@+id/dividertop"
android:layout_width="370dp"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:layout_below="@+id/location"
android:alpha="0.6"
android:background="@drawable/divider11"
android:paddingBottom="3dp" />
<ImageView
android:id="@+id/dividerbottom"
android:layout_width="370dp"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:layout_below="@+id/vsvdescription"
android:alpha="0.6"
android:background="@drawable/divider11"
android:paddingBottom="3dp" />
<ImageView
android:id="@+id/image_head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#000000"
android:padding="1dp" />
<HorizontalScrollView
android:id="@+id/isgallery"
android:layout_width="370dp"
android:layout_height="90dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/dividerbottom"
android:layout_marginTop="8dp"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/hsvimage1"
android:layout_width="148dp"
android:layout_height="90dp"
android:layout_marginRight="6dp"
android:layout_alignParentRight="true"
android:background="#fff"
android:padding="1dp" />
<ImageView
android:id="@+id/hsvimage2"
android:layout_width="148dp"
android:layout_height="90dp"
android:layout_marginRight="6dp"
android:layout_alignParentRight="true"
android:background="#000000"
android:padding="1dp" />
<ImageView
android:id="@+id/hsvimage3"
android:layout_width="148dp"
android:layout_height="90dp"
android:layout_marginRight="6dp"
android:layout_alignParentRight="true"
android:background="#CCC"
android:padding="1dp" />
<ImageView
android:id="@+id/hsvimage4"
android:layout_width="148dp"
android:layout_height="90dp"
android:layout_alignParentRight="true"
android:background="#000000"
android:padding="1dp" />
</LinearLayout>
</HorizontalScrollView>
<Button
android:id="@+id/bConfirm2"
android:layout_width="90dp"
android:layout_height="50dp"
android:layout_below="@+id/isgallery"
android:layout_centerHorizontal="true"
android:alpha="0.7"
android:layout_marginTop="10dp"
android:background="@drawable/gray_bac"
android:text="Confirm"
android:textColor="#2B3856"
android:textStyle="bold" />
<ScrollView
android:id="@+id/vsvdescription"
android:layout_width="370dp"
android:layout_height="250dp"
android:layout_marginTop="7dp"
android:layout_marginBottom="7dp"
android:padding="5dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/dividertop"
>
<RelativeLayout
android:orientation="vertical"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/sdescription"
android:layout_width="370dp"
android:layout_height="250dp"
android:gravity="center"
android:alpha="0.65"
android:textColor="#ffffff"
android:textSize="18sp" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
如果您需要进一步的澄清,请告诉我。
提前谢谢
更新
public class CasualEventsSingleItemActivity extends Activity {
// Declare Variables
String list_item_name;
String list_item_description;
String list_item_price;
String list_item_location;
String single_list_item_description;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_events_single_item);
RequestQueue mRequestQueue = null;
ImageLoader mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
Intent i = getIntent();
list_item_name = i.getStringExtra("list_item_name");
list_item_location = i.getStringExtra("list_item_location");
single_list_item_description = i.getStringExtra("single_list_item_description");
TextView txtname = (TextView) findViewById(R.id.name);
TextView txtlocation = (TextView) findViewById(R.id.location);
TextView txtsdescription = (TextView) findViewById(R.id.sdescription);
NetworkImageView hsvimage1 = (NetworkImageView) findViewById(R.id.hsvimage1);
NetworkImageView hsvimage2 = (NetworkImageView) findViewById(R.id.hsvimage2);
NetworkImageView hsvimage3 = (NetworkImageView) findViewById(R.id.hsvimage3);
// Get image URLs from your previous network request...
// I could not determine where this is stored from code in your question.
String url1 = "list_item_bac"; // e.g. http://example.com/images/image1.png
String url2 = "list_item_bac";
String url3 = "list_item_bac";
// Set the URL of the image that should be loaded into this view, and
// specify the ImageLoader that will be used to make the request.
hsvimage1.setImageUrl(url1, mImageLoader);
hsvimage2.setImageUrl(url2, mImageLoader);
hsvimage3.setImageUrl(url3, mImageLoader);
// Set results to the TextViews
txtname.setText(list_item_name);
txtlocation.setText(list_item_location);
txtsdescription.setText(single_list_item_description);
Button mConfirm2 = (Button)findViewById(R.id.bConfirm2);
mConfirm2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ParseUser currentUser = ParseUser.getCurrentUser();
// Create the class and the columns
currentUser.saveInBackground();
currentUser.put("ActivityName", list_item_name);
currentUser.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
setProgressBarIndeterminateVisibility(false);
if (e == null) {
// Success!
Intent intent = new Intent(CasualEventsSingleItemActivity.this, usermatch.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(CasualEventsSingleItemActivity.this);
builder.setMessage(e.getMessage())
.setTitle(R.string.signup_error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
//CasualEventsSingleItemActivity.this.startActivity(new Intent(CasualEventsSingleItemActivity.this, MatchingActivity.class));
}
});
}
}
公共类CasualEventsSingleItemActivity扩展活动{
//声明变量
字符串列表\项目\名称;
字符串列表\项目\说明;
字符串列表\项目\价格;
字符串列表\项目\位置;
字符串单\u列表\u项目\u描述;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u events\u single\u item);
RequestQueue MRRequestQueue=null;
ImageLoader mImageLoader=新的ImageLoader(mRequestQueue,
新建ImageLoader.ImageCache(){
私人决赛
缓存=新的LruCache(20);
@凌驾
公共位图getBitmap(字符串url){
返回cache.get(url);
}
@凌驾
公共位图(字符串url、位图){
cache.put(url、位图);
}
});
Intent i=getIntent();
list_item_name=i.getStringExtra(“list_item_name”);
list_item_location=i.getStringExtra(“list_item_location”);
单个列表项目描述=i.getStringExtra(“单个列表项目描述”);
TextView txtname=(TextView)findViewById(R.id.name);
TextView txtlocation=(TextView)findViewById(R.id.location);
TextView txtsdescription=(TextView)findViewById(R.id.sddescription);
NetworkImageView hsvimage1=(NetworkImageView)findViewById(R.id.hsvimage1);
NetworkImageView hsvimage2=(NetworkImageView)findViewById(R.id.hsvimage2);
NetworkImageView hsvimage3=(NetworkImageView)findViewById(R.id.hsvimage3);
//从以前的网络请求中获取图像URL。。。
//我无法从您问题中的代码中确定这是存储在哪里的。
字符串url1=“list\u item\u bac”;//例如。http://example.com/images/image1.png
字符串url2=“列表项”;
字符串url3=“列表项”;
//设置应加载到此视图中的图像的URL,然后
//指定将用于发出请求的ImageLoader。
hsvimage1.setImageUrl(url1,mImageLoader);
hsvimage2.setImageUrl(url2,mImageLoader);
hsvimage3.setImageUrl(url3,mImageLoader);
//将结果设置为文本视图
txtname.setText(列表项名称);
setText(列表项位置);
txtsdescription.setText(单个列表项描述);
按钮mConfirm2=(按钮)findViewById(R.id.bConfirm2);
mConfirm2.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
ParseUser currentUser=ParseUser.getCurrentUser();
//创建类和列
currentUser.savenbackground();
currentUser.put(“活动名称”,列表项名称);
currentUser.saveInBackground(新的SaveCallback(){
@凌驾
公共作废完成(Parsee异常){
SetProgressBarInDeterminateVibility(假);
如果(e==null){
//成功!
意向意向=新意向(CasualEventsSingleItemActivity.this,usermatch.class);
intent.addFlags(intent.FLAG\u活动\u新任务);
intent.addFlags(intent.FLAG\u活动\u清除\u任务);
星触觉(意向);
}
否则{
AlertDialog.Builder=新建AlertDialog.Builder(CasualEventsSingleItemActivity.this);
builder.setMessage(例如getMessage())
.setTitle(R.string.signup\u error\u title)
.setPositiveButton(android.R.string.ok,null);
AlertDialog=builder.create();
dialog.show();
}
}
});
//CasualEventsSingleItemActivity.this.startActivity(新意图(CasualEventsSingleItemActivity.this,MatchingActivity.class));
}
});
}
}
我会使用截击,这将为你在后台做所有的艰苦工作
首先,将所有ImageView更改为NetworkImageView:
接下来,创建一个ImageLoader。您很可能希望创建一个单例来保存以下内容:
mImageLoader=newimageloader(Volley.newRequestQueue(this),
新建ImageLoader.ImageCache(){
私人决赛
缓存=新的LruCache(20);
@凌驾
公共位图getBitmap(字符串url){
返回cache.get(url);
}
@凌驾
公共位图(字符串url、位图){
隐藏物
public class Events_List {
public long id;
public String list_item_title;
public String list_item_location;
public String list_item_price;
public Uri single_list_item_bac;
public String list_item_description;
public String single_list_item_description;
public Uri url;
public Events_List(long id, String list_item_location, String single_list_item_description, String list_item_title, String list_item_description, String list_item_price, Uri url){
this.id = id;
this.list_item_title = list_item_title;
this.list_item_location = list_item_location;
this.list_item_description = list_item_description;
this.single_list_item_description = single_list_item_description;
this.list_item_price = list_item_price;
this.url = url;
}
public String getList_item_title()
{
return this.list_item_title;
}
public String getList_item_location()
{
return this.list_item_location;
}
public String getList_item_price()
{
return this.list_item_price;
}
public String getList_item_description()
{
return this.list_item_description;
}
public String getSingle_list_item_description()
{
return this.single_list_item_description;
}
}
public class CasualEventsSingleItemActivity extends Activity {
// Declare Variables
String list_item_name;
String list_item_description;
String list_item_price;
String list_item_location;
String single_list_item_description;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_events_single_item);
RequestQueue mRequestQueue = null;
ImageLoader mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
Intent i = getIntent();
list_item_name = i.getStringExtra("list_item_name");
list_item_location = i.getStringExtra("list_item_location");
single_list_item_description = i.getStringExtra("single_list_item_description");
TextView txtname = (TextView) findViewById(R.id.name);
TextView txtlocation = (TextView) findViewById(R.id.location);
TextView txtsdescription = (TextView) findViewById(R.id.sdescription);
NetworkImageView hsvimage1 = (NetworkImageView) findViewById(R.id.hsvimage1);
NetworkImageView hsvimage2 = (NetworkImageView) findViewById(R.id.hsvimage2);
NetworkImageView hsvimage3 = (NetworkImageView) findViewById(R.id.hsvimage3);
// Get image URLs from your previous network request...
// I could not determine where this is stored from code in your question.
String url1 = "list_item_bac"; // e.g. http://example.com/images/image1.png
String url2 = "list_item_bac";
String url3 = "list_item_bac";
// Set the URL of the image that should be loaded into this view, and
// specify the ImageLoader that will be used to make the request.
hsvimage1.setImageUrl(url1, mImageLoader);
hsvimage2.setImageUrl(url2, mImageLoader);
hsvimage3.setImageUrl(url3, mImageLoader);
// Set results to the TextViews
txtname.setText(list_item_name);
txtlocation.setText(list_item_location);
txtsdescription.setText(single_list_item_description);
Button mConfirm2 = (Button)findViewById(R.id.bConfirm2);
mConfirm2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ParseUser currentUser = ParseUser.getCurrentUser();
// Create the class and the columns
currentUser.saveInBackground();
currentUser.put("ActivityName", list_item_name);
currentUser.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
setProgressBarIndeterminateVisibility(false);
if (e == null) {
// Success!
Intent intent = new Intent(CasualEventsSingleItemActivity.this, usermatch.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(CasualEventsSingleItemActivity.this);
builder.setMessage(e.getMessage())
.setTitle(R.string.signup_error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
//CasualEventsSingleItemActivity.this.startActivity(new Intent(CasualEventsSingleItemActivity.this, MatchingActivity.class));
}
});
}
}