【问题标题】:Cannot populate my ListView无法填充我的 ListView
【发布时间】:2016-05-19 11:45:08
【问题描述】:

所以我的布局膨胀在于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

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 类中,我使用 Retrofit 库进行回调以获取我的数据。回调工作正常,数据没有损坏,我已经在其他模式下对其进行了测试,它能够根据给定的地理位置创建地图对象。

我的TestAdapter 班级:

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>

感谢您的宝贵时间。

【问题讨论】:

    标签: java android listview android-viewholder


    【解决方案1】:

    如下更改您的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
    }
    

    【讨论】:

    • 会做的,谢谢你的帮助。调试后我会尽快回复。
    • 它成功了,我的布局完全搞砸了,但我确信我能做到。感谢您的时间和感激之情。我现在了解如何处理适配器和回调。非常感谢。已将您标记为答案 ;)
    猜你喜欢
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    • 1970-01-01
    相关资源
    最近更新 更多