列表视图与卡视图OnItemclickListner无法正常工作

时间:2015-11-03 03:24:38

标签: android android-listview android-cardview

我是Android的新手,我创建列表视图和卡片视图列表图像时点击项目它不工作任何一个请如何实现这一点,我的列表显示与卡Veiw罚款,但当我点击我的它我的OnClick是不工作任何人帮助我

这里是我的java代码

public class ALL extends Fragment { 

private ListAdapter mAdapter;

    private ArrayList<String> listCountry;
    private ArrayList<Integer> listFlag;
    private ListView listview;

     @Override

     public View onCreateView(LayoutInflater inflater, ViewGroup container,

             Bundle savedInstanceState) {

         View rootView = inflater.inflate(R.layout.tab_layout_one, container, false);

         prepareList();        
        mAdapter = new ListAdapter(getActivity(), listCountry, listFlag);
        //Missed code to here
        listview = (ListView)rootView.findViewById(R.id.card_listView);
        listview.setAdapter(mAdapter);

        listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub


                switch(position){
                case 0:             
                Intent one=new Intent(getActivity(),India.class);
                startActivity(one);
                break;          

                case 1:             

                    Intent two=new Intent(getActivity(),USA.class);
                    startActivity(two);
                    break;              
                case 2:
                    Intent three=new Intent(getActivity(),kanda.class);
                    startActivity(three);
                    break; 
                case 3:                 
                    Intent four=new Intent(getActivity(),resp.class);
                    startActivity(four);
                    break;              
                case 4:                 
                    Intent five=new Intent(getActivity(),koria.class);
                    startActivity(five);
                    break;                  
                case 5:                 
                    Intent six=new Intent(getActivity(),newzlan.class);
                    startActivity(six);
                    break;                  
                case 6:                 
                    Intent seven=new Intent(getActivity(),Dubai.class);
                    startActivity(seven);
                    break;                  
                case 7:                 
                    Intent eight=new Intent(getActivity(),canad.class);
                    startActivity(eight);
                    break;                  
                 case 8:                        
                        Intent nine=new Intent(getActivity(),mexico.class);
                        startActivity(nine);
                        break;              

                }

            }
        });

         return rootView;         
     }

     public void prepareList()
        {
            listCountry = new ArrayList<String>();

            listCountry.add("india.in");
            listCountry.add("Usa.com");
            listCountry.add("Londan.com");
            listCountry.add("Uh=ganda.com");
            listCountry.add("Region.com");
            listCountry.add("canada.com");
            listCountry.add("southafrica.in");
            listCountry.add("mixico.com");
            listCountry.add("turki.com");           


            listFlag = new ArrayList<Integer>();
            listFlag.add(R.drawable.India);
            listFlag.add(R.drawable.USA);
            listFlag.add(R.drawable.AUS);
            listFlag.add(R.drawable.Canda);
            listFlag.add(R.drawable.newzland);
            listFlag.add(R.drawable.Dubai);
            listFlag.add(R.drawable.nws);
            listFlag.add(R.drawable.kan);
            listFlag.add(R.drawable.mal);           

        }
}

我的ListAdapter类

public class ListAdapter extends BaseAdapter{   

    private ArrayList<String> listCountry;
    private ArrayList<Integer> listFlag;
    private LayoutInflater inflater;
    private static Context mcontext;

    public ListAdapter(Context context,ArrayList<String> listCountry, ArrayList<Integer> listFlag)
    {
        super();
        this.listCountry = listCountry;
        this.listFlag = listFlag;
        this.mcontext = context;
    }       

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listCountry.size();
    }
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return listCountry.get(position);
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }  
    public static class ViewHolder
    {
        public ImageView imgViewFlag;
        public TextView txtViewTitle;
    }

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

         ViewHolder view;

// TODO Auto-generated method stub


        if(convertView==null)
        {
             inflater =  (LayoutInflater)mcontext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            view = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_item_design, null);

            view.txtViewTitle = (TextView) convertView.findViewById(R.id.tv_nature);
            view.imgViewFlag = (ImageView) convertView.findViewById(R.id.img_thumbnail);

            convertView.setTag(view);
        }
        else
        {
            view = (ViewHolder) convertView.getTag();
        }

        view.txtViewTitle.setText(listCountry.get(position));
        view.imgViewFlag.setImageResource(listFlag.get(position));

        return convertView;
    }
    }

list_item_design

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.CardView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    card_view:cardCornerRadius="3dp"
    android:layout_marginTop="6dp"
    android:layout_marginLeft="6dp"
    android:layout_marginRight="6dp"
    android:clickable="true"
android:focusable="true"
    card_view:cardElevation="0.001dp"
    android:layout_marginBottom="0dp">
    <RelativeLayout
        android:id="@+id/top_layout"
        android:layout_width="fill_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/img_thumbnail"
            android:layout_width="fill_parent"
            android:layout_height="160dp"

            android:scaleType="centerCrop"

            />



    <TextView
        android:id="@+id/tv_nature"
        android:layout_width="fill_parent"
        android:layout_height="25dp"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="2dp"
        android:paddingBottom="2dp"
         android:layout_marginTop="25dp" 
        android:layout_marginBottom="25dp"
        android:alpha="0.8"
        android:textColor="#24B9FE"
        android:gravity="start"
        android:textSize="15sp"
        android:textStyle="bold"
        android:text="Test Test Test Test Test Test Test"
        android:layout_below="@+id/img_thumbnail"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true" />

    <Button
        android:id="@+id/button1"
        android:layout_width="40dp"
        android:layout_height="20dp"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/tv_nature"
        android:layout_marginRight="31dp"
        android:background="#24B9FE"
        android:text="Book"
        android:textColor="#ffffff" />

    </RelativeLayout>
</android.support.v7.widget.CardView>

0 个答案:

没有答案