Android在小部件中显示json

时间:2014-01-31 10:27:36

标签: java android json android-widget

我成功解析了json,我可以在listview上显示json,也成功创建了widget。现在我想在小部件(标题和描述)上显示jsons第一个数据的图像和文本

这是我的json

enter image description here

这也是我的BaseAdapter java类

public class BRIgeAdapter extends BaseAdapter {
    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater = null;
    public ImageLoader imageLoader;

    private int screenSize;
    MainActivity main;

    public BRIgeAdapter(Activity a, ArrayList<HashMap<String, String>> d,
                        int screenSize) {
        this.activity = a;
        this.data = d;
        this.screenSize = screenSize;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader = new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null)
            vi = inflater.inflate(R.layout.list_row, null);

        TextView journal = (TextView) vi.findViewById(R.id.smalljournal);
        TextView title = (TextView) vi.findViewById(R.id.smalltitle);
        TextView description = (TextView) vi
                .findViewById(R.id.smallDescription);
        ImageView thumb_image = (ImageView) vi.findViewById(R.id.smallthumb);
        TextView statId = (TextView) vi.findViewById(R.id.smallstatID);
        TextView DateTime = (TextView) vi.findViewById(R.id.smallDateTime);
        HashMap<String, String> itemList = new HashMap<String, String>();
        itemList = data.get(position);

        journal.setText(itemList.get(MainActivity.KEY_journal));
        statId.setText(itemList.get(MainActivity.KEY_statID));
        journal.setTypeface(MainActivity.tf2);

        String titleString = itemList.get(MainActivity.KEY_title);

        Calendar cal = new GregorianCalendar(2013, 11, 20);
        DateFormat df = new SimpleDateFormat("dd.MM.yyyy");
        String date = df.format(cal.getTime());

        String DateTimeTxt = itemList.get(MainActivity.KEY_pubDate);
        DateTimeTxt = date;

        DateTime.setText(DateTimeTxt);
        if (screenSize == Configuration.SCREENLAYOUT_SIZE_NORMAL)
            description.setVisibility(View.INVISIBLE);

        else
            description.setVisibility(View.VISIBLE);
        title.setText(titleString);
        title.setTypeface(MainActivity.tf2);
        description.setText(itemList.get(MainActivity.KEY_description));
        description.setTypeface(MainActivity.tf2);

        String url = itemList.get(MainActivity.KEY_image);
        //  url = url.replace("-c.jpg", ".jpg");

        imageLoader.DisplayImage(url, thumb_image);

        return vi;
    }

}

如何在小部件中显示json?

0 个答案:

没有答案