【发布时间】:2018-04-05 04:41:11
【问题描述】:
我想显示一个包含文本和图像的列表。 存储在我的在线数据库中的文本和图像, 我使用 JSON 将它们带到我的 android 应用程序中。
JSON 不显示任何错误,显示文本但不显示图像。
我检查了 logcat,这个过程没有错误。我使用 viewAdapter 在列表中显示图像。
请帮帮我,你能给我一些简单的解释如何解决这个问题吗?
谢谢...
注意。这是我的 CustomListAdaper2 代码:
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;
}
}
这是我的 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();
}
}
}
【问题讨论】:
-
为什么不用RecyclerView?
-
这条线
ImageView imageView = (ImageView) convertView.findViewById(imageView5);看起来不对。imageView5是什么? -
@gilbertxenodike 因为我在其他项目中使用相同的代码并且它工作正常。另一个项目显示 1 个图像视图和 1 个文本视图。这个项目必须显示 1 个图像视图和 4 个文本视图。但它仍然没有显示图像视图!
-
@Neeraj imageView5 是图像布局中的项目:
-
那么为什么是
convertView.findViewById(imageView5)而不是convertView.findViewById(R.id.imageView5)呢?是静态导入吗?