Android:在排球回调的片段中填充listview

时间:2015-09-23 12:30:43

标签: java android android-fragments

我无法打印我的列表视图,从Volley回调中收到。

这是我的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.

这是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;
    }

}

最后这是布局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>

这是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>

我的代码编译并执行没有错误,但我在模拟器上看不到任何东西(我会在我的场景中有一个6/7项目列表,从日志中演示)。

感谢您的帮助

修改

我在CustomListCampaignAdapter的View方法中添加了一个Log.d,但我在日志中看不到任何内容。

[...]

@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;
    }

1 个答案:

答案 0 :(得分:1)

将结果添加到列表后,您需要通知适配器更改。致电[[Prototype]]