获取到数据,却无法显示

我是不是忘了 2018-11-08 07:25:16
我在一个LinearLayout布局里循环插入view,实现图片左右滑动的效果。
思路是:去通过豆瓣api接口获取数据,然后解析JSON数据,把数据得到后,LinearLayout.addview(view)
可是每次运行app,效果都是这样(Fragment之间来回切换也显示不出来)

但是呢,我我点击那个【实时票房】是Fragment跳转activtiy的,跳转activtiy后我再返回【电影】这里,数据就显示出来了...

已经测试过了数据是获取到的

这是代码部分,我直接在Fragment里的onCreateView里面调用 moviceTheaterData();

//获取《正在热映》的数据
public void moviceTheaterData() {

try {
//获取电影名称
String str = "https://api.douban.com/v2/movie/in_theaters";
URL url = new URL(str);
new MoviceThread(url).start();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}


class MoviceThread extends Thread {
URL myurl;

public MoviceThread(URL myurl) {
this.myurl = myurl;
}

@Override
public void run() {
InputStream in = null;
try {
HttpURLConnection conn = (HttpURLConnection) myurl.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
int code = conn.getResponseCode();
if (code == 200) {
in = conn.getInputStream();
BufferedReader reader = null;
reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null)
response.append(line);
json = response + "";
//----------------------------解析数据---------------------------
mHandler.post(new Runnable() {
@Override
public void run() {
jsonMoviceTheaters();
}
});
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//结束子线程,防止占用
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
super.run();
}
}


//解析JSON数据---正在热映
public void jsonMoviceTheaters() {
try {
JSONObject jsonObject = new JSONObject(json);
//第一层解析
JSONArray subjects = jsonObject.optJSONArray("subjects");


for (int i = 0; i < subjects.length(); i++) {
JSONObject jsonObject1 = subjects.getJSONObject(i);
title = jsonObject1.getString("title");
str_id = jsonObject1.getString("id");
id_list_theater.add(str_id);
JSONObject jsonObject2 = jsonObject1.getJSONObject("rating");
double average = jsonObject2.optDouble("average");
JSONObject jsonObject3 = jsonObject1.getJSONObject("images");
String small = jsonObject3.optString("small");


View view1 = mInflater.inflate(R.layout.item_in_theaters,
null, false);

ImageView img = view1.findViewById(R.id.horizon_img);
TextView txt_title = view1.findViewById(R.id.horizon_title);
TextView txt_average = view1.findViewById(R.id.horizon_average);
String straverage = average+"";
txt_title.setText(title);
if(average==0){
txt_average.setText("暂无评分");
}else {
txt_average.setText(straverage);
}



try {
bitmap = ImageService.getImage(small);
} catch (Exception e) {
e.printStackTrace();
}
//图片压缩
img.setImageBitmap(CompressUtils.getInstance(getContext()).compress(bitmap,270,400));
mTheaters.addView(view1);

//点击跳转详情页面
final int pos = i;
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("urlstr",id_list_theater.get(pos));
intent.setClass(getActivity(), MoviceDetailsActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}
});
}


} catch (JSONException e) {
e.printStackTrace();
}

}

item_in_theaters.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<ImageView
android:id="@+id/horizon_img"
android:layout_width="130dp"
android:layout_height="160dp"
android:src="@drawable/ic_launcher_background" />

<TextView
android:id="@+id/horizon_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@color/blue"
android:text="疯狂动物城"
android:textSize="18dp" />
<TextView
android:id="@+id/horizon_average"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="9.1"
android:textColor="@color/colorYellow2"
android:textSize="16dp"
android:textStyle="bold" />


</LinearLayout>


另一个就是LinearLayout
也就是Fragment的xml


-------------------------------------
不是图片压缩的问题,我把图片去掉,数据还是显示不出来。
加了view.invalidate();也没用
...全文
81 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
我是不是忘了 2018-11-13
  • 打赏
  • 举报
回复
问题解决了,生命周期的问题
在OnCreatView里面我运行的子线程所以出错了
放handle里就行了
r00_a2lBUR 2018-11-09
  • 打赏
  • 举报
回复
可能是跟生命周期有关,有部分代码没有执行到
tq1086 2018-11-09
  • 打赏
  • 举报
回复
推荐ViewPager。
我是不是忘了 2018-11-08
  • 打赏
  • 举报
回复
引用 2 楼 YXTS122 的回复:
把你的项目发到我邮箱1444089871@qq.com,我用我的手机试一下

麻烦了,我把简易版的发给你,因为我完整的太大了。
YXTS122 2018-11-08
  • 打赏
  • 举报
回复
把你的项目发到我邮箱1444089871@qq.com,我用我的手机试一下
我是不是忘了 2018-11-08
  • 打赏
  • 举报
回复
模拟器上运行就行= =,好奇怪
求大佬指教

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧