Android Open Source - Scanner-For-Zotero Bib Detail J S O N Adapter






From Project

Back to project page Scanner-For-Zotero.

License

The source code is released under:

GNU General Public License

If you think the Android project Scanner-For-Zotero listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/** 
 * Copyright 2011 John M. Schanck//from  w  ww  . j  ava 2s .  c om
 * 
 * ScannerForZotero is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ScannerForZotero is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ScannerForZotero.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.ale.scanner.zotero;

import java.util.ArrayList;

import org.ale.scanner.zotero.data.ItemField;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;

public class BibDetailJSONAdapter extends BaseAdapter implements ListAdapter {

    private JSONObject mBacker;
    private ArrayList<String> mLabels;
    private int mNumFilledFields;

    private final LayoutInflater mInflater;

    public BibDetailJSONAdapter(Context context, JSONObject json){
        mInflater =
            (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        setJSON(json);
    }

    private void setLabelsToDisplay(JSONObject obj){
        mLabels = new ArrayList<String>();
        try {
            JSONArray labels = obj.names();
            if(labels == null)
                return;
            for(int i=0; i < labels.length(); i++){
                String label = (String) labels.get(i);
                String entry = obj.optString(label);
                if(!TextUtils.isEmpty(entry.trim())){
                    mLabels.add(label);
                }
            }
        } catch (JSONException e) {
            // XXX: There's bad data in the database!
        }

        // Don't include creator/title/notes/tags/itemType fields
        mLabels.remove(ItemField.creators);
        mLabels.remove(ItemField.title);
        mLabels.remove(ItemField.notes);
        mLabels.remove(ItemField.tags);
        mLabels.remove(ItemField.itemType);
    }

    public void setJSON(JSONObject replacement){
        mBacker = replacement;
        setLabelsToDisplay(replacement);
        mNumFilledFields = mLabels.size();
    }

    public void fillLinearLayout(LinearLayout layout){
        int childCount = layout.getChildCount();
        // Layout might not be the view we used last time, so
        // remove any views we won't need
        if(mNumFilledFields < childCount){
            layout.removeViews(mNumFilledFields, layout.getChildCount()-mNumFilledFields);
        }

        // Edit any of the already attached views
        for(int entry=0; entry < mNumFilledFields; entry++){
            if(entry < childCount){
                View v = layout.getChildAt(entry);
                getView(entry, v, layout);
            }else{
                layout.addView(getView(entry, null, layout));
            }
        }
    }

    @Override
    public int getCount() {
        return mNumFilledFields;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = mInflater.inflate(R.layout.bib_entry, null);
        }
        String label = mLabels.get(position);
        String value = mBacker.optString(label);
        TextView tv_label = (TextView) convertView.findViewById(R.id.entry_lbl);
        TextView tv_value = (TextView) convertView.findViewById(R.id.entry_content);
        tv_label.setText(ItemField.Localized.get(label) + ":");
        Util.fillBibTextField(tv_value, value);
        return convertView;
    }
}




Java Source Code List

org.ale.scanner.zotero.BibDetailJSONAdapter.java
org.ale.scanner.zotero.BibItemListAdapter.java
org.ale.scanner.zotero.Dialogs.java
org.ale.scanner.zotero.EditItemActivity.java
org.ale.scanner.zotero.LoginActivity.java
org.ale.scanner.zotero.MainActivity.java
org.ale.scanner.zotero.ManageAccountsActivity.java
org.ale.scanner.zotero.PString.java
org.ale.scanner.zotero.PendingListAdapter.java
org.ale.scanner.zotero.SafeViewFlipper.java
org.ale.scanner.zotero.Util.java
org.ale.scanner.zotero.data.Access.java
org.ale.scanner.zotero.data.Account.java
org.ale.scanner.zotero.data.BibItemDBHandler.java
org.ale.scanner.zotero.data.BibItem.java
org.ale.scanner.zotero.data.Collection.java
org.ale.scanner.zotero.data.CreatorType.java
org.ale.scanner.zotero.data.Database.java
org.ale.scanner.zotero.data.Group.java
org.ale.scanner.zotero.data.ItemField.java
org.ale.scanner.zotero.data.ItemType.java
org.ale.scanner.zotero.web.APIHandler.java
org.ale.scanner.zotero.web.APIRequest.java
org.ale.scanner.zotero.web.HttpsClient.java
org.ale.scanner.zotero.web.RequestQueue.java
org.ale.scanner.zotero.web.googlebooks.GoogleBooksAPIClient.java
org.ale.scanner.zotero.web.googlebooks.GoogleBooksHandler.java
org.ale.scanner.zotero.web.worldcat.WorldCatAPIClient.java
org.ale.scanner.zotero.web.worldcat.WorldCatHandler.java
org.ale.scanner.zotero.web.zotero.ZoteroAPIClient.java
org.ale.scanner.zotero.web.zotero.ZoteroHandler.java