[英]Android: populate listview in a fragment from volley callback
I cannot print my listview, received from a Volley callback. 我无法打印从Volley回调收到的列表视图。
This is my ListCampaignFragment.java
: 这是我的
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
这是
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
最后是布局
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
这是
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). 我的代码可以编译并执行,没有错误,但是我在模拟器上看不到任何东西(我的场景中有6/7个项目的列表,从日志中进行了演示)。
Thank you for your help 谢谢您的帮助
I added a Log.d in the method View of the CustomListCampaignAdapter, but I cannot see nothing in the log. 我在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;
}
After you add results to the list you need to inform the adapter of the changes. 将结果添加到列表后,需要将更改通知适配器。 Call
adapter.notifyDataSetChanged()
. 调用
adapter.notifyDataSetChanged()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.