如何使用图像制作listview片段?

时间:2018-02-19 16:31:03

标签: android listview android-fragments

我试图将此代码放在其他项目中,但它不起作用。

ListView活动项目

MainActivity

public class MainActivity extends AppCompatActivity {

    ListView fst;
    String[] placesname={"Place 1","Place 2","Place 3","Place 4","Place 5","Place 6"};
    String[] desc={"Desc 1","Desc 2","Desc 3","Desc 4","Desc 5","Desc 6"};
    Integer[] imgid={R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5,R.drawable.img6};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fst= (ListView) findViewById(R.id.listview);
        CustomListview customListview=new CustomListview(this,placesname,desc,imgid);
        fst.setAdapter(customListview);
        fst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                if(position==0) {
                    Intent myintent = new Intent(view.getContext(),touristspot1.class);
                    startActivityForResult(myintent,0);
                }
                if(position==1) {
                    Intent myintent = new Intent(view.getContext(),touristspot2.class);
                    startActivityForResult(myintent,1);
                }
            }
        });

    }
}

CustomListview

public class CustomListview extends ArrayAdapter<String> {

    private String[] placesname;
    private String[] desc;
    private Integer[] imgid;
    private Activity context;
    public CustomListview(Activity context, String[] placesname, String[] desc, Integer[] imgid) {
        super(context, R.layout.listview_layout, placesname);

        this.context = context;
        this.placesname = placesname;
        this.desc = desc;
        this.imgid = imgid;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View r=convertView;
            ViewHolder viewHolder=null;
            if (r==null)
            {

                LayoutInflater layoutInflater=context.getLayoutInflater();
                r=layoutInflater.inflate(R.layout.listview_layout,null,true);
                viewHolder=new ViewHolder(r);
                r.setTag(viewHolder);
            }
            else {

                viewHolder= (ViewHolder) r.getTag();
            }

            viewHolder.ivw.setImageResource(imgid[position]);
            viewHolder.tb1.setText(placesname[position]);
            viewHolder.tb2.setText(desc[position]);
            return r;

    }
    class ViewHolder
    {
        TextView tb1;
        TextView tb2;
        ImageView ivw;
        ViewHolder(View v)
        {
            tb1=v.findViewById(R.id.tubbaname);
            tb2=v.findViewById(R.id.tubbadesciption);
            ivw=v.findViewById(R.id.imageView);
        }
    }
}

当我放入片段时,我上面写的代码不起作用。我在下面写的代码是另一个项目,我把上面提到的代码放在

Listview片段

PPTP

public class pptp extends Fragment {


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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_pptp, container, false);
        // Inflate the layout for this fragment
        String[] awayStrings = {
                "Top Wonders",
                "Top Tourist Destinations",
                "Cities to Visit",
                "Romantic Places",
                "Family Destinations",
                "Best Honeymoon Destinations",
                "Adventure Sports Destinations",
                "Amusement Parks",
                "Best Beaches",
                "Monuments",
                "Historical Places",
                "National Parks and wild life sanctuary",
                "Places to Visit",
                "Beautiful Caves",
                "Best Cathedrals",
                "EcoNature Tourism",
                "Top Museums",
                "Beautiful Lakes",
                "Most Beautiful Sandbars",
                "Popular Street foods",
                "General Information",
        };

        ListView lv = (ListView) view.findViewById(R.id.listView);

        ArrayAdapter<String> lva = new ArrayAdapter<String>(
                getActivity(), android.R.layout.simple_list_item_1, awayStrings);
        lv.setAdapter(lva);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                if(position == 0){
                    List1TW fragment = new List1TW();
                    FragmentTransaction transaction = getFragmentManager().beginTransaction();
                    transaction.replace(R.id.fragment_container, fragment);
                    transaction.addToBackStack(null);
                    transaction.commit();

                }
            }
        });
        return view;
    }

}

ViewPageAdapter

public class ViewPageAdapter extends FragmentPagerAdapter{

    ArrayList<Fragment> fragments = new ArrayList<>();
    ArrayList<String> tabTitles = new ArrayList<>();

    public void addFragments (Fragment fragments,String tabTitles){
        this.fragments.add(fragments);
        this.tabTitles.add(tabTitles);
    }

    public ViewPageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabTitles.get(position);
    }
}

在片段中,我将如何在列表视图中查看图像?

1 个答案:

答案 0 :(得分:0)

问题是您在片段中使用简单ArrayAdapter<String>,而在活动中,您使用自定义adapter作为图片。

在你的片段中替换

ArrayAdapter<String> lva = new ArrayAdapter<String>(
            getActivity(), android.R.layout.simple_list_item_1, awayStrings);
    lv.setAdapter(lva);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            if(position == 0){
                List1TW fragment = new List1TW();
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.fragment_container, fragment);
                transaction.addToBackStack(null);
                transaction.commit();

            }
        }
    });

CustomListview customListview=new CustomListview(getActivity(),placesname,desc,imgid);
    lv.setAdapter(customListview);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            if(position == 0){
                List1TW fragment = new List1TW();
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.fragment_container, fragment);
                transaction.addToBackStack(null);
                transaction.commit();

            }
        }
    });