Android Can';无法从JSON加载图像
我想显示一个包含文本和图像的列表。 存储在我的在线数据库中的文本和图像, 我使用JSON将它们下载到我的android应用程序中 JSON不显示任何错误,显示文本但不显示图像 我检查了logcat,这个过程没有错误。我使用viewAdapter在列表上显示图像 请帮助我,你能给我一些简单的解释如何解决这个问题吗 谢谢 注意。这是我的CustomListAdaper2代码:Android Can';无法从JSON加载图像,android,json,image,load,Android,Json,Image,Load,我想显示一个包含文本和图像的列表。 存储在我的在线数据库中的文本和图像, 我使用JSON将它们下载到我的android应用程序中 JSON不显示任何错误,显示文本但不显示图像 我检查了logcat,这个过程没有错误。我使用viewAdapter在列表上显示图像 请帮助我,你能给我一些简单的解释如何解决这个问题吗 谢谢 注意。这是我的CustomListAdaper2代码: public class CustomListAdaper2 extends ArrayAdapter<contenu
public class CustomListAdaper2 extends ArrayAdapter<contenus> {
ArrayList<contenus> contenus;
Context context;
int resource;
public CustomListAdaper2(@NonNull Context context, @LayoutRes int resource,
@NonNull ArrayList<contenus> contenus) {
super(context, resource, contenus);
this.contenus = contenus;
this.context = context;
this.resource = resource;
}@NonNull
@Override
public View getView(int position2, @Nullable View convertView, @NonNull ViewGroup parent) {
if(convertView==null){
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.custom_list_layout_layout, null, true);
}
contenus contenus = getItem(position2);
ImageView imageView = (ImageView) convertView.findViewById(imageView5);
Picasso.with(context).load(contenus.getImage()).into(imageView);
TextView textViewtitle = (TextView) convertView.findViewById(R.id.textViewtitle);
textViewtitle.setText((replacehtml(contenus.getNom())));
TextView textViewcity = (TextView) convertView.findViewById(R.id.textViewcity);
textViewcity.setText((replacehtml(contenus.getVille())));
TextView textViewtime = (TextView) convertView.findViewById(R.id.textViewtime);
textViewtime.setText((replacehtml(contenus.getTime())));
TextView textViewprice = (TextView) convertView.findViewById(R.id.textViewprice);
textViewprice.setText((replacehtml(contenus.getPrix())));
return convertView;
}
}
公共类CustomListAdaper2扩展了ArrayAdapter{
ArrayList contenus;
语境;
智力资源;
public CustomListAdaper2(@NonNull上下文,@LayoutRes int-resource,
@非空ArrayList(上下文){
超级(上下文、资源、上下文);
this.contenus=contenus;
this.context=上下文;
这个资源=资源;
}@非空
@凌驾
公共视图getView(int位置2、@Nullable视图convertView、@NonNull视图组父级){
if(convertView==null){
LayoutFlater LayoutFlater=(LayoutFlater)getContext().getSystemService(Activity.LAYOUT\u INFLATER\u SERVICE);
convertView=LayoutFlater.inflate(R.layout.custom\u list\u layout\u layout,null,true);
}
contenus contenus=getItem(位置2);
ImageView ImageView=(ImageView)convertView.findViewById(imageView5);
毕加索.with(context).load(contentus.getImage()).into(imageView);
TextView textViewtitle=(TextView)convertView.findViewById(R.id.textViewtitle);
textViewtitle.setText((replacehtml(contenus.getNom()));
TextView textViewcity=(TextView)convertView.findViewById(R.id.textViewcity);
textViewcity.setText((replacehtml(contenus.getVille()));
TextView textViewtime=(TextView)convertView.findViewById(R.id.textViewtime);
textViewtime.setText((replacehtml(contenus.getTime()));
TextView textViewprice=(TextView)convertView.findViewById(R.id.textViewprice);
textViewprice.setText((replacehtml(contenus.getPrix()));
返回视图;
}
}
这是我的json代码:
public class Multimedia extends AppCompatActivity {
ArrayList<contenus> arrayList2;
ListView lv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multimedia);
arrayList2 = new ArrayList<>();
lv2 = (ListView) findViewById(R.id.ListView2);
runOnUiThread(new Runnable() {
@Override
public void run() {
new ReadJSON().execute("http://wach.ma/mobile/category.php?id=Pour_La_Maison_Et_Jardin");
}
});
}
class ReadJSON extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
return readURL(params[0]);
}
@Override
protected void onPostExecute(String content) {
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(content);
} catch (JSONException e) {
e.printStackTrace();
}
JSONArray jsonArray = null;
try {
jsonArray = jsonObject.getJSONArray("articles");
} catch (JSONException e) {
e.printStackTrace();
}
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject contenusobject = null;
try {
contenusobject = jsonArray.getJSONObject(i);
} catch (JSONException e) {
e.printStackTrace();
}
try {
arrayList2.add(new contenus(
contenusobject.getString("picture"),
contenusobject.getString("name"),
contenusobject.getString("city"),
contenusobject.getString("add_time"),
contenusobject.getString("price")
));
} catch (JSONException e) {
e.printStackTrace();
}
CustomListAdaper2 adaper2 = new CustomListAdaper2(
getApplicationContext(), R.layout.custom_list_layout_layout, arrayList2
);
lv2.setAdapter(adaper2);
}
}
@NonNull
private String readURL(String theURL) {
StringBuilder content = new StringBuilder();
try {
URL url = new URL(theURL);
URLConnection urlConnection = url.openConnection();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
content.append(line + "\n");
}
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
return content.toString();
}
}
}
公共类多媒体活动{
ArrayList ArrayList 2;
ListView lv2;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_多媒体);
ArrayList 2=新的ArrayList();
lv2=(ListView)findViewById(R.id.ListView2);
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
新建ReadJSON()。执行(“http://wach.ma/mobile/category.php?id=Pour_La_Maison_Et_Jardin");
}
});
}
类ReadJSON扩展了异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
返回readURL(参数[0]);
}
@凌驾
受保护的void onPostExecute(字符串内容){
JSONObject JSONObject=null;
试一试{
jsonObject=新的jsonObject(内容);
}捕获(JSONException e){
e、 printStackTrace();
}
JSONArray JSONArray=null;
试一试{
jsonArray=jsonObject.getJSONArray(“文章”);
}捕获(JSONException e){
e、 printStackTrace();
}
for(int i=0;i
只需编辑此代码
ImageView imageView = (ImageView) convertView.findViewById(R.id.your_image_view_id);
否则一切都很好。。。我测试它
更新
在使用图像的url之前,请确保
设置值时,将在模型上指定写入位置
arrayList2.add(new contenus(
//is this "picture" is first argument
contenusobject.getString("picture"),
contenusobject.getString("name"),
contenusobject.getString("city"),
contenusobject.getString("add_time"),
contenusobject.getString("price")
));
//Or Check hare
String url = contenus.getImage();
Log.d("image url", url);
Picasso.with(context).load(url).into(imageView);
关于
imageView 5
这是imageView
的正确id吗?为什么不使用RecyclerView?这行imageView imageView=(imageView)convertView.findViewById(imageView 5)代码>看起来不对。什么是imageView5
?@gilbertxenodike,因为我在其他项目中使用了相同的代码,而且效果很好。另一个项目显示1个图像视图和1个文本视图。这个项目必须显示1个imageview和4个TextView。但它仍然没有显示imageview@Neeraj imageView5是图像布局中的项:那么为什么它是convertView.findViewById(imageView5)
而不是convertView.findViewById(R.id.imageView5)
?是静态导入吗?是的。一切都很好。没有错误。但是当我在模拟器中运行它时,它不会显示图像!