How can I set onClickListener on ArrayAdapter?

后端 未结 4 1496
半阙折子戏
半阙折子戏 2020-12-03 00:23

I\'m making class like as below

// All necessary imports are here

public class More extends Activity {

    String[] MoreItems = { \"Transfers\", \"Budgets\         


        
相关标签:
4条回答
  • 2020-12-03 01:12

    There are two option to handle click event for each row.

    1) If your class extends ListActivity, you can override following method.

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
      super.onListItemClick(l, v, position, id);
      //do something here using the position in the array
    }
    

    2) Handle click event of row in getView() method

    row.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
    
        }
    });
    
    0 讨论(0)
  • 2020-12-03 01:12

    Your "More" class has to extend ListActivity instead of Activity, then you can override onListItemClick

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        //do something here using the position in the arrya
    }
    

    Edit: Forgot to say, in your layout your ListView has to be called: android:id="@android:id/list"

    0 讨论(0)
  • 2020-12-03 01:13

    you can also do like this..

    moreListView.setOnItemClickListener(new OnItemClickListener() {
    
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                    // TODO Auto-generated method stub
                    Log.d("############","Items " +  MoreItems[arg2] );
                }
    
            });
    
    0 讨论(0)
  • 2020-12-03 01:23

    For example:

     ListView lv = getListView();
    
            lv.setAdapter(listAdapter); 
    
            lv.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
    
                    Intent i = new Intent(More.this, NextActvity.class);
                   //If you wanna send any data to nextActicity.class you can use
                     i.putExtra(String key, value.get(position));
    
                startActivity(i);
                }
              });
    
    0 讨论(0)
提交回复
热议问题