从json解析图像但无法在listview中显示

时间:2018-04-09 10:35:48

标签: android json

我有json,其中包含图片网址和 我按照this教程,我下载源代码,工作正常,但当我在我的应用程序中实现相同的东西时,它显示我的listview是空的 当我调试我知道的代码时,必须填充列表视图的适配器加载数据,但listview不能 这是我的活动

public class ListNotificationActivity extends AppCompatActivity {
 Toolbar toolbar;

private ProgressDialog pDialog;

private static String url = "http://staging.talentslist.com/api/user/24/notification";
ArrayList<NotificationData> notiList;
ListView listview;
NotificAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_notification);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle(getResources().getString(R.string.notification));
    setSupportActionBar(toolbar);

    notiList = new ArrayList<NotificationData>();
    new GetList().execute(url);

    listview = (ListView) findViewById(R.id.notificationList);

    adapter = new NotificAdapter(getApplicationContext(), R.layout.list_item, notiList);

    listview.setAdapter(adapter);


}

private class GetList extends AsyncTask<String, Void, Boolean> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(ListNotificationActivity.this);
        pDialog.setMessage("Loading, please wait");
        pDialog.setTitle("Connecting server");
        pDialog.show();
        pDialog.setCancelable(false);
    }

    @Override
    protected Boolean doInBackground(String... urls) {
        HttpGet httppost = new HttpGet(urls[0]);
        URL urlObj = null;
        try {
            urlObj = new URL(urls[0]);


            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);
            HttpURLConnection urlConnection = (HttpURLConnection) urlObj.openConnection();
            InputStream is = urlConnection.getInputStream();


            // StatusLine stat = response.getStatusLine();
            int status = response.getStatusLine().getStatusCode();
            //    int status = urlConnection.getResponseCode();

            if (status == 200) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);
                JSONObject jsono = new JSONObject(data);
                JSONArray jarray = jsono.getJSONArray("data");

                for (int i = 0; i < jarray.length(); i++) {
                    JSONObject object = jarray.getJSONObject(i);
                    NotificationData notidata = new NotificationData();

                    notidata.setId(object.getInt("id"));
                    notidata.setUser_name(object.getString("user_name"));
                    notidata.setTitle(object.getString("title"));
                    notidata.setIs_read(object.getString("is_read"));
                    notidata.setCreated_at(object.getString("created_at"));
                    notidata.setImage_link(object.getString("image_link"));
                    notiList.add(notidata);
                }
                return true;


            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    protected void onPostExecute(Boolean result) {
        pDialog.cancel();

       adapter.notifyDataSetChanged();


        if (result == false)
            Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();

    }
}

}

这是我的适配器类

public class NotificAdapter extends ArrayAdapter<NotificationData> {

ArrayList<NotificationData> nDataAdapter;
LayoutInflater vi;
int Resource;
ViewHolder holder;

public NotificAdapter(Context context, int resource, ArrayList<NotificationData> objects) {
    super(context, resource);
    vi = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    Resource = resource;
    nDataAdapter = objects;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        holder = new ViewHolder();
        v = vi.inflate(Resource, null);
        holder.imageview = (ImageView) v.findViewById(R.id.list_image);
        holder.tvSender_name = (TextView) v.findViewById(R.id.sender_name);
        holder.tvNoti_details = (TextView) v.findViewById(R.id.noti_details);
        holder.tvDate = (TextView) v.findViewById(R.id.date);
        v.setTag(holder);
    } else {
        holder = (ViewHolder) v.getTag();
    }

    holder.imageview.setImageResource(R.drawable.noimage);
    new DownloadImageTask(holder.imageview).execute(nDataAdapter.get(position).getImage_link());
    holder.tvSender_name.setText(nDataAdapter.get(position).getUser_name());
    holder.tvNoti_details.setText(nDataAdapter.get(position).getUser_name());
    holder.tvDate.setText(nDataAdapter.get(position).getCreated_at());

    return v;
}

private class ViewHolder {
    public ImageView imageview;
    public TextView tvSender_name;
    public TextView tvNoti_details;
    public TextView tvDate;
}

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    @Override
    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}

} 这是listview的列表项布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >

<!--  ListRow Left sied Thumbnail image -->
<LinearLayout
    android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="5dip"
    android:background="@drawable/image_bg"
    android:orientation="vertical"
    android:padding="3dip">

    <ImageView
        android:id="@+id/list_image"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:src="@drawable/noimage" />

</LinearLayout>

<TextView
    android:id="@+id/sender_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/thumbnail"
    android:layout_toRightOf="@+id/thumbnail"
    android:text="Notification from"
    android:textColor="#040404"
    android:typeface="sans"
    android:textSize="15dip"
    android:textStyle="bold"/>

<TextView
    android:id="@+id/noti_details"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toEndOf="@+id/thumbnail"
    android:text="Notification details"
    android:textColor="#343434"
    android:textSize="10dip" />

<TextView
    android:id="@+id/date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@id/name"
    android:gravity="right"
    android:text="Time"
    android:layout_marginRight="5dip"
    android:textSize="10dip"
    android:textColor="#10bcc9"
    android:textStyle="bold"/>

 </RelativeLayout>

请帮助我!!

1 个答案:

答案 0 :(得分:0)

在获取数据以将所有数据添加到列表中时进行一些更改。 并为下面的设置适配器制作单独的方法..

    protected void onPostExecute(Boolean result) {
    setAdapter();
    if (result == false)
        Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();

}

然后在上面的方法中调用代码中的这个块..

"fromYear": {
            "array": false,
            "type": "number",
            "title": "From Year",
            "minimum": {
                "$data": "1/toYear"
             },
            "key": "fromYear",
            "required": false
        }