Android: onListItemClick in Activity

前端 未结 8 1982
故里飘歌
故里飘歌 2020-12-28 21:23

Previous time I asked a question here I learned a lot so I guess it\'s worth a shot to try it again.

I am using the lazy list by Fedor from this link: Lazy load of i

相关标签:
8条回答
  • 2020-12-28 21:32

    I am writing my answer as:

    1) the code by @Falmarri needs some update
    2) My suggested edit was totally rejected XD
    3) Stackoverflow is not allowing me to write a comment.

    Here is the code:

    ListView listView = (ListView) findViewById(R.id.my_listview_in_layout);
    listview.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id){
            //Do stuff
            //...
        }
    });
    


    Reference: According to android.widget.AdapterView.OnItemClickListener , public method onItemClick() is the method invoked when an item is clicked {instead of unknown protected method onListItemClick() }

    0 讨论(0)
  • 2020-12-28 21:36

    I have been working on this all day and after making my own ArrayAdapter I couldn't figure out how to change classes in my list.

    Here's how I found out how to do it. After I called my array i simply finished out my code in that method by doing.

    ListView lv =getListView();
    lv.setOnItemClickListener(this);
    

    Then after all my text i put

    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
        String item = (String) getListAdapter().getItem(position);
    
        if (item.equals("Economy"))
        {
            Intent intent = new Intent(packages.this, economy.class);
            startActivity(intent);
        }
        else if (item.equals("Basic"))
        {
            Intent intent = new Intent(packages.this, basic.class);
            startActivity(intent);
        }
        else if (item.equals("Professional"))
        {
            Intent intent = new Intent(packages.this, professional.class);
            startActivity(intent);
        }
        else if (item.equals("Custom Applications"))
        {
            Intent intent = new Intent(packages.this, applications.class);
            startActivity(intent);
        }
    }
    

    Between I managed to completely customize my ListView with custom font and backgrounds. I'm sure a ton of you don't really care. But I'm exited and was hoping by posting this that I might help someone in the future.

    0 讨论(0)
  • 2020-12-28 21:38

    I have simply added following in onCreate():

        listvview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                setPaymentDetails();
            }
        });
    

    Outside of onCreate() added setPaymentDetails():

    protected void setPaymentDetails()
    {
        Intent intent = new Intent(this, SetPaymentDetailsActivity.class);
        startActivity(intent);
    }
    
    0 讨论(0)
  • 2020-12-28 21:40

    If you are using ListActivity then you want to do something like this:

    public class YourClass extends ListActivity implements OnItemClickListener{
    
        @Override
        public void onCreate(Bundle icicle){
            super.onCreate(icicle);
            setContentView(R.layout.your_layout);
    
            getListView().setOnItemClickListener(this);
        }
    
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // your stuff here
        }
    }
    

    This is not the only way to set an OnItemClickListener. Look other answers. This is the way I like to do it since it's clearer and easier to read.

    0 讨论(0)
  • 2020-12-28 21:43

    For a non-ListActivity to have an item-clicked-listener for a ListView, you have to call the setOnItemClickedListener() on the ListView (you may need to get that using findViewById() if it's coming from XML)

    Rather than just overriding ListActivity's onListItemClickListener(), here you'd have your invoking Activity implement AdapterView.onItemClickedListener() and pass it as the parameter to setOnItemClickedListener().

    (If you read the source code for ListActivity (which I recommend), you'll see it just does exactly that behind the scenes by creating an internal listener object that calls your overridden onListItemClick()).

    0 讨论(0)
  • 2020-12-28 21:47

    FE.java

    package com.example.rfe;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    import android.app.ListActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.Toast;
    public class FE extends ListActivity {
     public List<String> d = null;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.file);
            d = new ArrayList<String>();    
            Scanner in = null;
            File f = new File("/sdcard/input.txt");
            try
            {
            in = new Scanner(new FileReader(f));
            while(in.hasNext()==true)
            {
                d.add(in.nextLine());   
            }
            }catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
            ArrayAdapter<String> fileList =
                      new ArrayAdapter<String>(this, R.layout.row, d);
                     setListAdapter(fileList);
        }
     @Override
     protected void onListItemClick(ListView l, View v, int position, long id) 
     {
         String selection = l.getItemAtPosition(position).toString();
         Toast.makeText(this, selection, Toast.LENGTH_LONG).show();
        super.onListItemClick(l, v, position, id);
    }
    }
    
    0 讨论(0)
提交回复
热议问题