ListView没有填充

时间:2016-04-27 08:00:24

标签: android listview

我遇到了我无法弄清楚的问题。我有 FragmentA FragmentB 。当我第一次交易到 FragmentB 时,ListView没有填充。如果我按“返回”并再次执行事务,则ListView正在填充。 我的代码怎么了?

这是我的片段代码:

public class RegionListFrag extends android.support.v4.app.Fragment {

    ArrayList<String> names = new ArrayList<>();
        RegionAddAdapter regionAddAdapter;
    ListView listView;
    DBHelper dbHelper;
    SQLiteDatabase db;
    final String LOG_TAG = "myLogs";
    RegionNameClass regionNameClass;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        ((MainActivity)getActivity()).changeTbOn();
        MainActivity.toolbar.setTitle("Regions");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.region_layout, container, false);
        MainActivity.toolbar.setTitle("Regions");
        listView = (ListView) rootView.findViewById(R.id.region_lv);

        regionAddAdapter  = new RegionAddAdapter(getActivity(),
                setRv());
        listView.setAdapter(regionAddAdapter);
        listView.setPadding(0, 110, 0, 0);

        return rootView;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_toolbar, menu);
        super.onCreateOptionsMenu(menu,inflater);
        Menu myMenu = menu;
        MenuItem nextItem = myMenu.findItem(R.id.accept_category);
        nextItem.setVisible(true);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {

            getFragmentManager().popBackStack();
            getFragmentManager().beginTransaction().commit();
            return true;
        }

        if (item.getItemId() == R.id.accept_category) {

        }

        return super.onOptionsItemSelected(item);
    }

    ArrayList<RegionNameClass>   setRv(){
        dbHelper = new DBHelper(getActivity());
        db = dbHelper.getWritableDatabase();

        Cursor c = db.query("regiontable", null, null, null, null, null,
                null);
        ArrayList<RegionNameClass> rvArray = new ArrayList<RegionNameClass>();
        while(c.moveToNext()){
            String name = c.getString(c.getColumnIndex("regNames"));
            RegionNameClass regionObj = new RegionNameClass(name);
            rvArray.add(regionObj);
        }
        Log.d(LOG_TAG, "array size - " +  rvArray.size());
        return rvArray;
    }
}

这是我的适配器:

public class RegionAddAdapter extends ArrayAdapter<RegionNameClass> {

    private static  ArrayList<RegionNameClass> list = new ArrayList<RegionNameClass>();
    private final Activity context;
    public ArrayList<RegionNameClass> selectedStrings = new ArrayList<RegionNameClass>();
    final String LOG_TAG = "myLogs";
    RegionNameClass regionNameClass;

    public RegionAddAdapter(Activity context, ArrayList<RegionNameClass> top) {
        super(context, R.layout.region_row, list);
        this.context = context;
        list = top;
    }

    static class ViewHolder {

        protected TextView myTv;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;

        RegionNameClass myClass = getItem(position);

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.region_row, parent, false);
        }
        TextView tvName = (TextView) convertView.findViewById(R.id.region_tv);
        tvName.setText(myClass.name);
        return convertView;
    }
}

1 个答案:

答案 0 :(得分:1)

请检查RegionAddAdapter,更新如下

public RegionAddAdapter(Activity context, ArrayList<RegionNameClass> top) {
        super(context, R.layout.region_row, top);
        this.context = context;
        list = top;
}