开发者

problem with listener for listview

开发者 https://www.devze.com 2023-03-11 09:44 出处:网络
I have an app in android in which I\'m setting up a list view...and now try to display a message when I press on one of the items....but it\'s not working...I\'m sure is something simple but I just ca

I have an app in android in which I'm setting up a list view...and now try to display a message when I press on one of the items....but it's not working...I'm sure is something simple but I just can figure out what it is...and my listener won't work!

public class Server3 extends ListActivity {

    static final String[] items=new String[] {"Car1",  "Car2", "Car3","Car4", "Car5"};




    public class MyCustomAdapter extends ArrayAdapter<String>{

        public MyCustomAdapter(Context context, int textViewResourceId, String[] objects) {
                 super(context, textViewResourceId, objects);
                 // TODO Auto-generated constructor stub
                }

                @Override
                public View getView (int position, View convertView, ViewGroup parent) {
                 // TODO Auto-generated method stub
                 LayoutInflater inflater=getLayoutInflater();
                 View row=inflater.inflate(R.layout.row, parent, false);
                 TextView label=(TextView)row.findViewById(R.id.item);
                 label.setText(items[position]);
                 ImageView icon=(ImageView)row.findViewById(R.id.icon);

                 if (items[position]=="Car1"){
                  icon.setImageResource(R.drawable.car1);
                 }
                 if (items[position]=="Car2"){
                      icon.setImageResource(R.drawable.car2);
                     }
                 if (items[position]=="Car3"){
                     icon.setImageResource(R.drawable.car3);             
                 }
                 if (items[position]=="Car4"){
                     icon.setImageResource(R.drawable.car4);             
                 }
                 if (items[position]=="Car5"){
                     icon.setImageResource(R.drawable.car5);             
                 }

                 return row;
                }
                }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.server3);

         setListAdapter(new MyCustomAdapter(Server3.this, R.layout.row, items));

        final ListV开发者_开发百科iew listView = getListView();


        listView.setItemsCanFocus(false);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 





    }

     @Override 
    /* public void onCreateContextMenu(ContextMenu menu, View v,  ContextMenuInfo menuInfo) {  

             menu.setHeaderTitle("Choose");  
             menu.add(0, MENU_track, 0, "Track");  
             menu.add(0, MENU_delete, 0,  "Delete");

     } 
    @Override
    public boolean onContextItemSelected(MenuItem item) {  

        switch (item.getItemId()) {  
            case MENU_track:  
                  Intent j1 = new Intent(Server3.this, Server4.class);
                   startActivity(j1);
                return true;  

            case MENU_delete:    
                  return true;  

            default:    
                return super.onContextItemSelected(item);  
        }
    }*/


    public void onListItemClick(ListView listView,View v,int position,long id)
    {

        if(items[position]=="car1")
        {
            Toast.makeText(this, "am apasat", 0).show();
        }
        if(items[position]=="car2")
        {
            System.out.println("am selectat masina 2");
        }

    }









}

If someone could tell mey why there is no message display when I click on it I would be gratefull.thx


in the onListItemClick you have written `if(items[position]=="car1")

`

It's Car1 . System.out in your car2 (wrong, it's Car2) will not work.


Looks like the Toast isn't formed properly

Toast.makeText(getApplicationContext(), "am apasat", Toast.LENGTH_SHORT);

Is the right way to make a Toast


Override onListItemClickListener or you can implement your onClickListener inside your getView() of MyCustomAdapter. something like: ...

View row=inflater.inflate(R.layout.row, parent, false);
row.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "u clicked", Toast.LENGTH_LONG).show();               
            }
        });

...


there is no message because you set the time how long the Toast is displayed 0

Toast.makeText(this, "am apasat", 0).show();

use this:

Toast.makeText(getApplicationContext(), "your message", Toast.LENGTH_SHORT);

You can use these constants

Toast.LENGTH_SHORT
Toast.LENGTH_LONG

And if you want to check which item is selected you have to do it like this:

if (items.get(position).equals("car1")) {
   //do some work
}


Its simple

you need to attach the onCLickListener in the getVIew() method itself like this

Locate the line View row=inflater.inflate(R.layout.row, parent, false); in your getView()

and do this

row.setOnClickListerer()

this will work

0

精彩评论

暂无评论...
验证码 换一张
取 消