简体   繁体   中英

Android: populate listview in a fragment from volley callback

I cannot print my listview, received from a Volley callback.

This is my ListCampaignFragment.java :

public class ListCampaignFragment extends ListFragment {

    private BaseApp app;
    private View v;
    private ListView list_campaign_view;
    private List<ModelCampaign> campaignList = new ArrayList<ModelCampaign>();
    private CustomListCampaignAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.v = inflater.inflate(R.layout.list_campaign_fragment, container, false);

        //registerForContextMenu(this.listView);

        app = (BaseApp) getActivity();
        getCampaign();

        adapter = new CustomListCampaignAdapter(app, campaignList);

        list_campaign_view = (ListView) v.findViewById(android.R.id.list);
        list_campaign_view.setAdapter(adapter);

        return v;
    }

    private void getCampaign() {

        String token = app.token_debug;

        ApiMapper mapper = new ApiMapper();
        //mapper.getCampaign(token);

        mapper.getCampaign(new ApiMapper.VolleyCallback() {
            @Override
            public void onSuccess(JSONArray campaigns) {

                for (int i = 0; i < campaigns.length(); i++) {
                    try {
                        JSONObject obj = campaigns.getJSONObject(i);
                        ModelCampaign campaign = new ModelCampaign();

                        Log.d("TEST", obj.getString("name"));
                        campaign.setNameCampaign(obj.getString("name"));
                        campaign.setTypeCampaign(obj.getString("type"));
                        campaign.setIdCampaign(obj.getInt("id"));

                        // adding campaign to campaigns array
                        campaignList.add(campaign);

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

                }

            }
        }, token);


    }

}

Log.d("TEST", obj.getString("name"));  prints the right name, so callback is executed and code received the JSON object without problem.

This is the CustomListCampaignAdapter.java

public class CustomListCampaignAdapter extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater;
    private List<ModelCampaign> campaignItems;

    public CustomListCampaignAdapter(Activity activity, List<ModelCampaign> campaignItems) {
        this.activity = activity;
        this.campaignItems = campaignItems;
    }

    @Override
    public int getCount() {
        return campaignItems.size();
    }

    @Override
    public Object getItem(int location) {
        return campaignItems.get(location);
    }

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

    public int getIdCampaign(int position){
        ModelCampaign m = campaignItems.get(position);
        int idCampaign = m.getIdCampaign();
        return idCampaign;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.list_campaign_row, null);

        TextView nameCampaign = (TextView) convertView.findViewById(R.id.campaign_name);
        TextView typeCampaign = (TextView) convertView.findViewById(R.id.campaign_type);

        // getting movie data for the row
        ModelCampaign m = campaignItems.get(position);


        // nameCampaign
        nameCampaign.setText(m.getNameCampaign());
        typeCampaign.setText(m.getFormattedTypeCampaign());

        return convertView;
    }

}

And finally this is the layout list_campaign_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        >
    </ListView>
</RelativeLayout>

And this is the list_campaign_row.xml

<?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:orientation="vertical"
    android:padding="@dimen/padding_list_row" >

    <TextView
        android:id="@+id/campaign_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="@style/TextAppearance.AppCompat.Title"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/campaign_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:layout_below="@+id/campaign_name"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

My code compile and executes without errors, but I cannot see anything on the simulator (I would have a list of 6/7 items in my scenario, demostrated from the Log).

Thank you for your help

EDIT

I added a Log.d in the method View of the CustomListCampaignAdapter, but I cannot see nothing in the log.

[...]

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.list_campaign_row, null);

        TextView nameCampaign = (TextView) convertView.findViewById(R.id.campaign_name);

        TextView typeCampaign = (TextView) convertView.findViewById(R.id.campaign_type);

        // getting movie data for the row
        ModelCampaign m = campaignItems.get(position);


        // nameCampaign
        nameCampaign.setText(m.getNameCampaign());
        typeCampaign.setText(m.getFormattedTypeCampaign());

        Log.d("CLCA", m.getFormattedTypeCampaign());

        return convertView;
    }

After you add results to the list you need to inform the adapter of the changes. Call adapter.notifyDataSetChanged() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM