如何使我的OnItemClickListener工作并显示项目位置的吐司?

时间:2015-01-14 13:59:32

标签: java android adt onclicklistener onitemclicklistener

我正在尝试使用图标来执行customList我使用自定义视图制作图标+文本 现在我试图获取列表中的项目以响应OnItemClickListener 它没有回应而没有显示我的祝酒词与项目的位置

请帮助。

    ////////////////////////////////////THIS IS My Main Activity///////////////////
    package com.example.mycash;

    import android.app.ActionBar;
    import android.app.Activity;
    import android.content.Context;
    import android.database.DataSetObserver;
    import android.os.Bundle;
    import android.support.v4.widget.DrawerLayout;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.Toast;


    public class MainActivity extends Activity implements OnItemClickListener  {
                DrawerLayout drawerLayout;
                ListView listView;
                AdapterList adapterList;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
         //Hide Action Bar--------------------------------------   
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
            ActionBar actionBar = getActionBar();
            actionBar.hide();
        //Hide Action Bar---------------------------------------  
           listView = (ListView)findViewById(R.id.listMenu);
           final AdapterList adapterList = new AdapterList(this);
           listView.setAdapter(adapterList);


           drawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout); 


        }//OnCreate Closing...

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
            Toast.makeText(getBaseContext(), adapterList.menu[position] + "Test" , Toast.LENGTH_SHORT).show();

        }  
    }



////////////////This IS My AdapterList Class here Im Creating Custom adapter///////////////////
package com.example.mycash;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class AdapterList extends BaseAdapter {

    private Context context;

    String[] menu = {"How Much Did I Work"
            ,"Add/Edit Work Space"
            ,"My Profile"
            ,"Send Work Doc Via Mail"
            ,"Send FeedBack"
            ,"Bug Report"};
     int[] icons = {R.drawable.clock,R.drawable.edit,R.drawable.profile,R.drawable.mail,R.drawable.sendfeed,R.drawable.bug};

    public AdapterList(Context context){
        this.context = context;

    }

    @Override
    public int getCount() {

        return menu.length;
    }

    @Override
    public Object getItem(int position) {

        return menu[position];
    }

    @Override
    public long getItemId(int position) {
        // Not In Use  For Future Uses...
        return position;
    }

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

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.custom_row_layout, null);

        }else{
             row = null;
        }
        TextView customTextView = (TextView)row.findViewById(R.id.customText1);
        ImageView customImageView = (ImageView)row.findViewById(R.id.customImage1);

        customTextView.setText(menu[position]);
        customImageView.setImageResource(icons[position]);

        return row;
    }


}

2 个答案:

答案 0 :(得分:1)

你必须将onItemClick监听器设置为listview:

listView.setOnItemClickListener(this);

答案 1 :(得分:0)

您还没有在事件监听器(OnItemClickListner)上设置listView,执行此操作并且它应该可以正常工作。