在片段内设置列表视图的适配器

时间:2016-05-01 16:28:42

标签: android android-fragments

我有一个listView的自定义适配器,它位于片段内。我收到一个错误,我不知道是什么导致它。

这是SummerJobsFragment.java:

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.app.ListActivity;

import java.util.ArrayList;

public class SummerJobsFragmnet extends Fragment {

    public SummerJobsFragmnet() {
        // Required empty public constructor
    }

    // TODO: Rename and change types and number of parameters
    public static Fragment getInstance() {
        Fragment fragment = new SummerJobsFragmnet();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        MainActivity activity = (MainActivity) getActivity();
        String [] places = activity.getAllPositionsNamesPhone().toArray(new String[activity.getAllPositionsNamesPhone().size()]);

        ListView list = (ListView) getView().findViewById(R.id.joblistView);
        int [] prgmImages={R.mipmap.ic_launcher};
        list.setAdapter(new CustomListAdapter(this,places,prgmImages));
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.displayjobs, container, false);
    }
}

这里是CustomListAdapter:

import android.widget.BaseAdapter;

        import android.content.Context;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.view.ViewGroup;
        import android.widget.BaseAdapter;
        import android.widget.ImageView;
        import android.widget.TextView;
        import android.widget.Toast;

public class CustomListAdapter extends BaseAdapter {
    String [] result;
    Context context;
    int [] imageId;
    private static LayoutInflater inflater=null;
    public CustomListAdapter(MainActivity mainActivity, String[] places, int[] prgmImages) {
        // TODO Auto-generated constructor stub
        result=places;
        context=mainActivity;
        imageId=prgmImages;
        inflater = ( LayoutInflater )context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return result.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public class Holder
    {
        TextView tv;
        ImageView img;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Holder holder=new Holder();
        View rowView;
        rowView = inflater.inflate(R.layout.display_job_cell_view, null);
        holder.tv=(TextView) rowView.findViewById(R.id.Itemname);
        holder.img=(ImageView) rowView.findViewById(R.id.icon);
        holder.tv.setText(result[position]);
        holder.img.setImageResource(imageId[position]);
        rowView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
            }
        });
        return rowView;
    }

}

这里是错误的logcat:

Click here for logCat

1 个答案:

答案 0 :(得分:0)

您正在获取索引超出范围的异常,因为这些列表的大小不同。

    String [] places = activity.getAllPositionsNamesPhone().toArray(new String[activity.getAllPositionsNamesPhone().size()]);

    int [] prgmImages={R.mipmap.ic_launcher};

您的适配器正在抛出错误

    list.setAdapter(new CustomListAdapter(this,places,prgmImages));

在这些方面

    holder.tv.setText(result[position]);
    holder.img.setImageResource(imageId[position]);