android-使用Base适配器在Fragment中实现ListVIew,我做错了什么?

时间:2017-10-17 16:50:02

标签: android listview fragment baseadapter

问题Sloverd,我试图在片段中实现listview而不实现listfragment

  

(只是扩展片段)

和自定义适配器,不扩展任何ArrayAdapter或ListAdapter ..

  

(只是扩展BaseAdapter)

我的以下代码显示没有错误,但listview没有显示在片段中。我正在使用自定义适配器一起显示两个edittext。在调试期间,我可以看到适配器在视图中发送列表,但listview.setadapter()没有绑定任何数据任何人都可以告诉我我做错了什么?我试图在这里找到类似的解决方案,但没有运气。我非常感谢任何反馈或指向类似这样的问题。的片段:

    import android.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ListView;
    import android.widget.TextView;    
    import java.util.ArrayList;


    public class ResultFragment extends Fragment {
        private DBHelper dbHelper=null;
        ArrayList<MedicineModel> medicineModelList=new ArrayList<MedicineModel>();
        String selectedData="";
        CustomAdapterBrandResult adapter;
        ListView listView;

        public ResultFragment() {
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            View view= inflater.inflate(R.layout.fragment_result, container, false);

            listView= (ListView) view.findViewById(R.id.resultList);
            TextView textView=(TextView)view.findViewById(R.id.selected_text);

            Bundle bundle=getArguments();
            if(bundle!=null){
                textView.setText(bundle.getString("selected_data"));
                selectedData=(bundle.getString("selected_data"));
            }

            dbHelper=new DBHelper(this.getActivity());
            medicineModelList=  dbHelper.getAllUsingBrandName(selectedData);

            adapter=new CustomAdapterBrandResult(getActivity(),  medicineModelList);
            listView.setAdapter(adapter);
            return view;
        }

    }

自定义适配器

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;

public class CustomAdapterBrandResult extends BaseAdapter {
    private Context mcontext;
    private ArrayList<MedicineModel> medicineModelList;
    public CustomAdapterBrandResult(Context mcontext, ArrayList<MedicineModel> medicineModelList) {
        this.mcontext = mcontext;
        this.medicineModelList = medicineModelList;
    }
    @Override
    public int getCount() {
        return medicineModelList.size();
    }
    @Override
    public Object getItem(int position) {
        return medicineModelList.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view=View.inflate(mcontext,R.layout.custom_layout_brand_result,null);

        final TextView textViewBrand=(TextView)view.findViewById(R.id.textView2BrandName);
        final TextView textViewManufacture=(TextView)view.findViewById(R.id.textView3Manufacture);

        textViewBrand.setText( medicineModelList.get(position).getBrandName());
        textViewManufacture.setText(medicineModelList.get(position).getNameoftheManufacturer());
        return view;
    }
}

1 个答案:

答案 0 :(得分:1)

最后!能够在我的fragment.Nepe覆盖onCreate方法和初始化列表以及adpater中显示listview,并且在覆盖onCreateView方法中只需在listview上设置适配器,就是它......工作每个都很好。谢谢你们,

@IntelliJAmiya和
@Mikerizzo
谁评论我的问题并花时间去研究它

以下是最终解决方案:片段

x.Bark();