从Custom Listview Adapter

时间:2017-02-25 18:22:10

标签: android mysql listview android-fragments

我有自定义列表视图。这是在我的片段里面。列表视图有名称,评级图像等。所以我使用LazyListview适配器来自定义这个列表视图,现在我想获得list-view的选定项目的id,它是从数据库mysql与json绑定的。所以我实现OnClickListener到LazyListview适配器每一件工作正常但我无法在我的片段中获取所选的id当我尝试调用片段内的方法时它显示错误无法将活动转换为片段。 请帮忙。

public class LazyImageLoadAdapter extends BaseAdapter implements  OnClickListener{

    private Activity activity;
    private String[] data;
    private String[] data1;
    private String[] data2;
    private String[] data3;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader;
    Typeface tf;
    Typeface tf1;
    ArrayList<Country> countrylist;
    ArrayList<Country> mStringFilterList;

    ValueFilter valueFilter;
    ArrayList<Country> filterList;
    public LazyImageLoadAdapter(Activity a, ArrayList<Country> countrylist) {


        activity = a;

        this.countrylist = countrylist;
        mStringFilterList = countrylist;

        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // Create ImageLoader object to download and show image in list
        // Call ImageLoader constructor to initialize FileCache
        imageLoader = new ImageLoader(activity.getApplicationContext());
        //tf = Typeface.createFromAsset(a.getAssets(), "fonts/LobsterTwo-BoldItalic.ttf");
       // tf1 = Typeface.createFromAsset(a.getAssets(), "fonts/LobsterTwo-Italic.ttf");


    }

    public int getCount() {
        return countrylist.size();
    }

    public Object getItem(int position) {
        return countrylist.get(position);
    }

    public long getItemId(int position) {
        return countrylist.indexOf(getItem(position));
    }

    /********* Create a holder Class to contain inflated xml file elements *********/
    public static class ViewHolder{

        public TextView text;
        public TextView text1;
        public TextView textWide;
        public ImageView image2;
        public RatingBar pop_ratingbar;

    }

    public View getView(int position, View convertView, ViewGroup parent) {

        View vi=convertView;
        ViewHolder holder;

        if(convertView==null){

            /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
            vi = inflater.inflate(R.layout.list_gym, null);

            /****** View Holder Object to contain tabitem.xml file elements ******/

            holder = new ViewHolder();
            holder.text = (TextView) vi.findViewById(R.id.title);
            holder.text1=(TextView)vi.findViewById(R.id.txt_exp);
           // holder.text1.setBackgroundResource(R.drawable.rating_background);
            holder.image2=(ImageView)vi.findViewById(R.id.imageview_gym);
            holder.pop_ratingbar=(RatingBar)vi.findViewById(R.id.ratingBar);

           /************  Set holder with LayoutInflater ************/

            vi.setTag( holder );


        }

        else 

        holder=(ViewHolder)vi.getTag();        
        Country country = countrylist.get(position);
        holder.text.setText(country.getName());
        //holder.text.setText(country.getIso_code().toString());
        holder.text1.setText("  Total experience: "+country.getRatingC()+" years");
        String s1 = country.getFlag();
        Float f1= Float.parseFloat(s1);
        holder.pop_ratingbar.setRating(f1);
        //holder.text.setText(country.getName());
        //holder.text.setTypeface(tf);
        //holder.text1.setText("  Based On("+country.getRatingC()+") Rating");
        //holder.text1.setTypeface(tf);
        ImageView image1 = holder.image2;



        //DisplayImage function from ImageLoader Class
        imageLoader.DisplayImage(country.getIso_code().toString(), image1);

        /******** Set Item Click Listner for LayoutInflater for each row ***********/
        vi.setOnClickListener(new OnItemClickListener(Integer.parseInt(country.getRid().toString())));



        return vi;      

    }





    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    }


    /********* Called when Item click in ListView ************/
   private class OnItemClickListener  implements OnClickListener{
        private int mPosition;

       OnItemClickListener(int position){

             mPosition = position;
        }

        @Override
        public void onClick(View arg0) {

            GymFragment act = GymFragment(activity); //HERE IS Problem 
            act.onItemClick();
            Log.i("sdadasdsadas", String.valueOf(mPosition));
        }
    }


}
</code>
//HEre is my Fragment 
<br>
<code>
public class GymFragment  extends android.support.v4.app.Fragment  {
  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.gymfragment, container, false);
        lv = (ListView) rootView.findViewById(android.R.id.list);
 return rootView;
}
public void onItemClick(int mPosition)
    {
         //HERE I Want Selected ID of List-view 
        Toast.makeText(getActivity(),"Selected ID",Toast.LENGTH_LONG).show();
     }
}

1 个答案:

答案 0 :(得分:1)

你的问题在于语法。 你应该替换这一行:

GymFragment act = GymFragment(activity);

这一行:

GymFragment act = new GymFragment(activity);