gpt4 book ai didi

java - Android:从 Volley 回调中填充 fragment 中的 ListView

转载 作者:行者123 更新时间:2023-12-01 11:07:49 25 4
gpt4 key购买 nike

我无法打印从 Volley 回调收到的 ListView 。

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

最佳答案

将结果添加到列表后,您需要将更改通知适配器。调用adapter.notifyDataSetChanged()

关于java - Android:从 Volley 回调中填充 fragment 中的 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32739692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com