无法填充我的 ListView
Cannot populate my ListView
所以我的布局 inflation 包含 content_frame
-> listview_fragment
-> list_row_item
在 MainActivity
上,我应该选择要显示的模式,出于调试原因我强制选择了它
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener{
//region Properties
private GoogleMap map;
private static final int LAYOUT_CHOOSER = 123;
private static final String TAG="MainActivity";
//endregion
//region Methods
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
if(LAYOUT_CHOOSER == 1){
Log.v(TAG, "Im on the MapMode");
getSupportFragmentManager().beginTransaction()
.add(R.id.content_frame, new SecurityMapFragment())
.commit();
}
else {
Log.v(TAG, "Im on the ListMode");
getSupportFragmentManager().beginTransaction()
.add(R.id.content_frame, new SecurityListFragment())
.commit();
}
它将调用 SecurityListFragment
class
public class SecurityListFragment extends BaseFragment {
private TestAdapter mTestAdapter;
private SecurityResponse securityResponse;
private FetchDataInterface dataInterface;
public SecurityListFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.v("ListFragment", "I entered the onCreateView!");
final View root = inflater.inflate(R.layout.security_listview_fragment, container, false);
Log.v("ListFragment", "rootView created");
//set adapter
ListView lista = (ListView) root.findViewById(R.id.listview_security);
lista.setAdapter(mTestAdapter);
Log.v("dasd", "asdasdasd");//for breakpoint usage
return root;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dataInterface = RetrofitUtils.createGsonRetrofitInterface();
RetrofitUtils.testGetPspDataFromApi(new Callback<SecurityResponse>() {
@Override
public void onResponse(Call<SecurityResponse> call, Response<SecurityResponse> response) {
if (response.isSuccessful()) {
securityResponse = response.body();
mTestAdapter = new TestAdapter(getActivity(), R.layout.list_security_row_item, securityResponse.features);
Log.v("ListFragment", "great success");
}
}
@Override
public void onFailure(Call<SecurityResponse> call, Throwable t) {
}
}, dataInterface);
Log.v("adasdadsa","asdasdasdasdasasdas");//more breakpoint usage
}
我在 SecurityListFragment
class 中使用 Retrofit 库进行回调以获取数据。回调工作正常并且数据没有损坏,我已经在其他模式下对其进行了测试,它能够根据给定的地理位置创建地图对象。
我的TestAdapter
class:
public class TestAdapter extends ArrayAdapter<Feature> {
protected Context mContext;
protected int layoutResourceId;
protected List<Feature> mSecurityResponse;
public TestAdapter(Context context, int resourceId, List<Feature> objects) {
super(context, resourceId, objects);
this.layoutResourceId = resourceId;
this.mContext = context;
this.mSecurityResponse = objects;
}
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder viewHolder;
if(convertView==null){
// inflate the layout
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
// well set up the ViewHolder
viewHolder = new ViewHolder();
viewHolder.distance = (TextView)convertView.findViewById(R.id.security_list_distance);
viewHolder.icon = (ImageView)convertView.findViewById(R.id.security_list_icon);
viewHolder.name = (TextView)convertView.findViewById(R.id.security_list_name);
viewHolder.description = (TextView)convertView.findViewById(R.id.security_list_description);
// store the holder with the view.
convertView.setTag(viewHolder);
}else{
// we've just avoided calling findViewById() on resource everytime
// just use the viewHolder
viewHolder = (ViewHolder) convertView.getTag();
}
// object item based on the position
Feature featureItem = mSecurityResponse.get(position);
// assign values if the object is not null
if(featureItem != null) {
// get the TextView from the ViewHolder and then set the text (item name) and tag (item ID) values
viewHolder.distance.setText("42"+ " km");
viewHolder.description.setText(featureItem.attributes.description);
viewHolder.name.setText(featureItem.attributes.name);
viewHolder.icon.setImageResource(R.mipmap.ic_launcher);
}
return convertView;
}
public static class ViewHolder{
public ImageView icon;
public TextView name;
public TextView description;
public TextView distance;
}
差点忘了,这里是用到的布局:
content_frame
布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
security_listview_fragment
:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
>
<ListView
android:id="@+id/listview_security"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
和 list_security_row_item
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/security_list_icon"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="20px">
<TextView
android:id="@+id/security_list_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/security_list_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/security_list_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
感谢您的宝贵时间。
如下更改您的SecurityListFragment
public class SecurityListFragment extends BaseFragment {
private TestAdapter mTestAdapter;
private SecurityResponse securityResponse;
private FetchDataInterface dataInterface;
private ListView lista;//**changed**
public SecurityListFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.v("ListFragment", "I entered the onCreateView!");
final View root = inflater.inflate(R.layout.security_listview_fragment, container, false);
Log.v("ListFragment", "rootView created");
//set adapter
lista = (ListView) root.findViewById(R.id.listview_security);//**changed**
Log.v("dasd", "asdasdasd");//for breakpoint usage
return root;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dataInterface = RetrofitUtils.createGsonRetrofitInterface();
RetrofitUtils.testGetPspDataFromApi(new Callback<SecurityResponse>() {
@Override
public void onResponse(Call<SecurityResponse> call, Response<SecurityResponse> response) {
if (response.isSuccessful()) {
securityResponse = response.body();
mTestAdapter = new TestAdapter(getActivity(), R.layout.list_security_row_item, securityResponse.features);
lista.setAdapter(mTestAdapter);//**changed**
Log.v("ListFragment", "great success");
}
}
@Override
public void onFailure(Call<SecurityResponse> call, Throwable t) {
}
}, dataInterface);
Log.v("adasdadsa","asdasdasdasdasasdas");//more breakpoint usage
}
所以我的布局 inflation 包含 content_frame
-> listview_fragment
-> list_row_item
在 MainActivity
上,我应该选择要显示的模式,出于调试原因我强制选择了它
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener{
//region Properties
private GoogleMap map;
private static final int LAYOUT_CHOOSER = 123;
private static final String TAG="MainActivity";
//endregion
//region Methods
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
if(LAYOUT_CHOOSER == 1){
Log.v(TAG, "Im on the MapMode");
getSupportFragmentManager().beginTransaction()
.add(R.id.content_frame, new SecurityMapFragment())
.commit();
}
else {
Log.v(TAG, "Im on the ListMode");
getSupportFragmentManager().beginTransaction()
.add(R.id.content_frame, new SecurityListFragment())
.commit();
}
它将调用 SecurityListFragment
class
public class SecurityListFragment extends BaseFragment {
private TestAdapter mTestAdapter;
private SecurityResponse securityResponse;
private FetchDataInterface dataInterface;
public SecurityListFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.v("ListFragment", "I entered the onCreateView!");
final View root = inflater.inflate(R.layout.security_listview_fragment, container, false);
Log.v("ListFragment", "rootView created");
//set adapter
ListView lista = (ListView) root.findViewById(R.id.listview_security);
lista.setAdapter(mTestAdapter);
Log.v("dasd", "asdasdasd");//for breakpoint usage
return root;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dataInterface = RetrofitUtils.createGsonRetrofitInterface();
RetrofitUtils.testGetPspDataFromApi(new Callback<SecurityResponse>() {
@Override
public void onResponse(Call<SecurityResponse> call, Response<SecurityResponse> response) {
if (response.isSuccessful()) {
securityResponse = response.body();
mTestAdapter = new TestAdapter(getActivity(), R.layout.list_security_row_item, securityResponse.features);
Log.v("ListFragment", "great success");
}
}
@Override
public void onFailure(Call<SecurityResponse> call, Throwable t) {
}
}, dataInterface);
Log.v("adasdadsa","asdasdasdasdasasdas");//more breakpoint usage
}
我在 SecurityListFragment
class 中使用 Retrofit 库进行回调以获取数据。回调工作正常并且数据没有损坏,我已经在其他模式下对其进行了测试,它能够根据给定的地理位置创建地图对象。
我的TestAdapter
class:
public class TestAdapter extends ArrayAdapter<Feature> {
protected Context mContext;
protected int layoutResourceId;
protected List<Feature> mSecurityResponse;
public TestAdapter(Context context, int resourceId, List<Feature> objects) {
super(context, resourceId, objects);
this.layoutResourceId = resourceId;
this.mContext = context;
this.mSecurityResponse = objects;
}
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder viewHolder;
if(convertView==null){
// inflate the layout
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
// well set up the ViewHolder
viewHolder = new ViewHolder();
viewHolder.distance = (TextView)convertView.findViewById(R.id.security_list_distance);
viewHolder.icon = (ImageView)convertView.findViewById(R.id.security_list_icon);
viewHolder.name = (TextView)convertView.findViewById(R.id.security_list_name);
viewHolder.description = (TextView)convertView.findViewById(R.id.security_list_description);
// store the holder with the view.
convertView.setTag(viewHolder);
}else{
// we've just avoided calling findViewById() on resource everytime
// just use the viewHolder
viewHolder = (ViewHolder) convertView.getTag();
}
// object item based on the position
Feature featureItem = mSecurityResponse.get(position);
// assign values if the object is not null
if(featureItem != null) {
// get the TextView from the ViewHolder and then set the text (item name) and tag (item ID) values
viewHolder.distance.setText("42"+ " km");
viewHolder.description.setText(featureItem.attributes.description);
viewHolder.name.setText(featureItem.attributes.name);
viewHolder.icon.setImageResource(R.mipmap.ic_launcher);
}
return convertView;
}
public static class ViewHolder{
public ImageView icon;
public TextView name;
public TextView description;
public TextView distance;
}
差点忘了,这里是用到的布局:
content_frame
布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
security_listview_fragment
:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
>
<ListView
android:id="@+id/listview_security"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
和 list_security_row_item
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/security_list_icon"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="20px">
<TextView
android:id="@+id/security_list_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/security_list_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/security_list_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
感谢您的宝贵时间。
如下更改您的SecurityListFragment
public class SecurityListFragment extends BaseFragment {
private TestAdapter mTestAdapter;
private SecurityResponse securityResponse;
private FetchDataInterface dataInterface;
private ListView lista;//**changed**
public SecurityListFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.v("ListFragment", "I entered the onCreateView!");
final View root = inflater.inflate(R.layout.security_listview_fragment, container, false);
Log.v("ListFragment", "rootView created");
//set adapter
lista = (ListView) root.findViewById(R.id.listview_security);//**changed**
Log.v("dasd", "asdasdasd");//for breakpoint usage
return root;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dataInterface = RetrofitUtils.createGsonRetrofitInterface();
RetrofitUtils.testGetPspDataFromApi(new Callback<SecurityResponse>() {
@Override
public void onResponse(Call<SecurityResponse> call, Response<SecurityResponse> response) {
if (response.isSuccessful()) {
securityResponse = response.body();
mTestAdapter = new TestAdapter(getActivity(), R.layout.list_security_row_item, securityResponse.features);
lista.setAdapter(mTestAdapter);//**changed**
Log.v("ListFragment", "great success");
}
}
@Override
public void onFailure(Call<SecurityResponse> call, Throwable t) {
}
}, dataInterface);
Log.v("adasdadsa","asdasdasdasdasasdas");//more breakpoint usage
}