问题描述
列表适配器中未显示的图像,您能帮我吗?
在日志猫中,我收到最后一条消息,这表示我正在获取http结果,但是适配器无法正常工作
www.i I/System.out﹕ [com.justedhak.www.i.Listitem@2c7c0f18, com.justedhak.www.i.Listitem@1fbed71, com.justedhak.www.i.Listitem@3102b756, com.justedhak.www.i.Listitem@148873d7, com.justedhak.www.i.Listitem@af04c4, com.justedhak.www.i.Listitem@1190efad]
E/d﹕ ppppp
I/System.out﹕ [com.justedhak.www.i.Listitem@2c7c0f18, com.justedhak.www.i.Listitem@1fbed71, com.justedhak.www.i.Listitem@3102b756, com.justedhak.www.i.Listitem@148873d7, com.justedhak.www.i.Listitem@af04c4, com.justedhak.www.i.Listitem@1190efad, com.justedhak.www.i.Listitem@308af2e2]
E/d﹕ ppppp
I/System.out﹕ [com.justedhak.www.i.Listitem@2c7c0f18, com.justedhak.www.i.Listitem@1fbed71, com.justedhak.www.i.Listitem@3102b756, com.justedhak.www.i.Listitem@148873d7, com.justedhak.www.i.Listitem@af04c4, com.justedhak.www.i.Listitem@1190efad, com.justedhak.www.i.Listitem@308af2e2, com.justedhak.www.i.Listitem@3df67673]
E/d﹕ ppppp
I/System.out﹕ [com.justedhak.www.i.Listitem@2c7c0f18, com.justedhak.www.i.Listitem@1fbed71, com.justedhak.www.i.Listitem@3102b756, com.justedhak.www.i.Listitem@148873d7, com.justedhak.www.i.Listitem@af04c4, com.justedhak.www.i.Listitem@1190efad, com.justedhak.www.i.Listitem@308af2e2, com.justedhak.www.i.Listitem@3df67673, com.justedhak.www.i.Listitem@9f9a930]
E/d﹕ ppppp
I/System.out﹕ [com.justedhak.www.i.Listitem@2c7c0f18, com.justedhak.www.i.Listitem@1fbed71, com.justedhak.www.i.Listitem@3102b756, com.justedhak.www.i.Listitem@148873d7, com.justedhak.www.i.Listitem@af04c4, com.justedhak.www.i.Listitem@1190efad, com.justedhak.www.i.Listitem@308af2e2, com.justedhak.www.i.Listitem@3df67673, com.justedhak.www.i.Listitem@9f9a930, com.justedhak.www.i.Listitem@1bf9d9a9]
E/d﹕ ppppp
I/System.out﹕ [com.justedhak.www.i.Listitem@2c7c0f18, com.justedhak.www.i.Listitem@1fbed71, com.justedhak.www.i.Listitem@3102b756, com.justedhak.www.i.Listitem@148873d7, com.justedhak.www.i.Listitem@af04c4, com.justedhak.www.i.Listitem@1190efad, com.justedhak.www.i.Listitem@308af2e2, com.justedhak.www.i.Listitem@3df67673, com.justedhak.www.i.Listitem@9f9a930, com.justedhak.www.i.Listitem@1bf9d9a9, com.justedhak.www.i.Listitem@b695b2e]
E/d﹕ ppppp
这是获取json的代码
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray("result");
Listitem = new ArrayList<Listitem>();
for(int i=0;i<peoples.length();i++){
JSONObject c = peoples.getJSONObject(i);
String id = c.getString("id");
String url = c.getString("path");
Listitem.add(new Listitem(id,url));
Log.e("d", "ppppp");
System.out.println(Listitem);
}
if (mListener != null)
mListener.myMethod(Listitem);
这是片段中的代码,但是adpater根本不起作用
public void downloadImage(){// OkHttpHandler handler = new OkHttpHandler();
//handler.execute();
String image = null;
try {
OkHttpHandler handler = new OkHttpHandler(getActivity(),
new OkHttpHandler.MyInterface() {
@Override
public void myMethod(ArrayList result) {
Toast.makeText(getActivity(), "Connection Succesful",
Toast.LENGTH_LONG).show();
GridViewAdapter adapter = new GridViewAdapter(getActivity(), R.layout.grid_item_layout, result);
adapter.notifyDataSetChanged();
list.setAdapter(adapter);
}
});
image = handler.execute().get();
Log.d("e", "hhhh handler");
这是适配器
public class GridViewAdapter extends ArrayAdapter<Listitem> {
private Context mcontext;
private int layoutResourceId;
public GridViewAdapter(Context context, int layoutResourceId, ArrayList<Listitem> listitem) {
super(context, layoutResourceId, listitem);
this.layoutResourceId = layoutResourceId;
this.mcontext =context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder;
if (row == null) {
LayoutInflater inflater = LayoutInflater.from(mcontext);
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.imageTitle = (TextView) row.findViewById(R.id.text);
holder.imageView = (ImageView) row.findViewById(R.id.imageView);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
Listitem item = getItem(position);
System.out.println(item.getUrl());
holder.imageTitle.setText(item.getId());
Picasso.
with(mcontext).
load(item.getUrl())
.placeholder(R.drawable.logo)
.fit()
.into(holder.imageView);
return row;
}
static class ViewHolder {
TextView imageTitle;
ImageView imageView;
}
这是适配器的xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="vertical"
android:padding="5dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="300dp"
android:layout_height="300dp"
/>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:maxLines="2"
android:layout_below="@+id/grid_item_image"
android:ellipsize="marquee"
android:textSize="12sp" />
</RelativeLayout>
因为它的片段,我在这里初始化列表
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View v = inflater.inflate(R.layout.latestjokesfrag, container, false);
list = (ListView) v.findViewById(R.id.ListView);
return inflater.inflate(R.layout.latestjokesfrag, container, false);
}
我添加了这个
new OkHttpHandler.MyInterface() {
@Override
public void myMethod(ArrayList result) {
System.out.println("result");
这是结果
www.i I/System.out﹕ result
10-24 15:22:35.000 31426-31426/com.justedhak.www.i I/System.out﹕ [com.justedhak.www.i.Listitem@34435c8a, com.justedhak.www.i.Listitem@239ca2fb, com.justedhak.www.i.Listitem@2c7c0f18, com.justedhak.www.i.Listitem@1fbed71, com.justedhak.www.i.Listitem@3102b756, com.justedhak.www.i.Listitem@148873d7, com.justedhak.www.i.Listitem@af04c4, com.justedhak.www.i.Listitem@1190efad, com.justedhak.www.i.Listitem@308af2e2, com.justedhak.www.i.Listitem@3df67673, com.justedhak.www.i.Listitem@9f9a930, com.justedhak.www.i.Listitem@1bf9d9a9]
fragmetn.xml
public class LatestJokesFrag extends Fragment{
ListView list;
public LatestJokesFrag() {
// Required empty public constructor
}
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
downloadImage();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void downloadImage() {
// OkHttpHandler handler = new OkHttpHandler();
//handler.execute();
String image = null;
try {
OkHttpHandler handler = new OkHttpHandler(getActivity(),
new OkHttpHandler.MyInterface() {
@Override
public void myMethod(ArrayList result) {
System.out.println("result");
System.out.println(result);
Toast.makeText(getActivity(), "Connection Succesful",
Toast.LENGTH_LONG).show();
GridViewAdapter adapter = new GridViewAdapter(getActivity(), R.layout.grid_item_layout, result);
adapter.notifyDataSetChanged();
list.setAdapter(adapter);
}
});
image = handler.execute().get();
Log.d("e", "hhhh handler");
// System.out.print(image);
//if (image != null && image.length > 0){
// Log.isLoggable("e",image.length);
//Log.d("e", "ddddddd entered the try");
// Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
// imageView.setImageBitmap(bitmap);
// txtBytes.setText("Total bytes downloaded: " + image.length);
} catch (Exception e) {
Log.d("e", "rrrrrr error");
e.printStackTrace();
// txtBytes.setText("Hmm sorry, something went wrong!");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View v = inflater.inflate(R.layout.latestjokesfrag, container, false);
list = (ListView) v.findViewById(R.id.ListView);
return inflater.inflate(R.layout.latestjokesfrag, container, false);
}
}
1楼
您的代码有问题。
您在Fragment onCreate()
内调用downloadMethod,但您应该按照源代码中所述在onActivityCreated()
内进行操作
onCreated() can be called while the fragment's activity is * still in the process of being created. As such, you can not rely * on things like the activity's content view hierarchy being initialized * at this point. If you want to do work once the activity itself is * created, see {@link #onActivityCreated(Bundle)}.
所以我们开始
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
downloadImage();
}
另外,您在onCreateView
犯了一个错误:它应该返回您夸大的视图
例如,它应该如下所示
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View v = inflater.inflate(R.layout.latestjokesfrag, container, false);
list = (ListView) v.findViewById(R.id.ListView);
return v;
}