程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> listview-在ArrayAdapter設置onClickListener

listview-在ArrayAdapter設置onClickListener

編輯:編程綜合問答
在ArrayAdapter設置onClickListener

在程序中我寫了以下的類,所有需要的 imports 都在程序中。

public class More  extends Activity {

    String[] MoreItems = {
            "Transfers",
            "Budgets",
            "Branches",
            "Tools",
            "News",
            "Customer Service",
            "Settings",
            "Help",
            "About"
    };

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

        ListView moreListView = (ListView) findViewById(R.id.moreListView);
        MoreListAdapter listAdapter = new MoreListAdapter();
        moreListView.setAdapter(listAdapter);


    //  accountsTypeListView.setOnItemClickListener(listClickListner);

    }


    class MoreListAdapter extends ArrayAdapter<String> {
        MoreListAdapter() {
            super(More.this, R.layout.list_item, MoreItems);    
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View row;

            if (convertView == null) {
                LayoutInflater inflater = getLayoutInflater();
                row = inflater.inflate(R.layout.list_item, parent, false);
            } else {
                row = convertView;
            }
            TextView tv = (TextView) row.findViewById(R.id.textItem);

            tv.setText(getItem(position));

            return row ;
        }
    }

}

它將生成List,我想在respective click上調用respective activities,比如用戶點擊Transfer ,它就會轉到 transfer Activity。如何在List上調用onClickListener,並且在click上開啟 Activity?

最佳回答:


如果是要實現點擊相對應的Item進入相應的Activity的話,樓主可以這樣寫:
List> p = GlobalApplication.permission;
下面有幾個Activity你就寫幾個map放到list裡面
Map map = new HashMap();
map.put("app", NewsActivity.class);
p.add(map);
下面是ListView的監聽:
gv.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {                  
                    if (list.get(arg2).get("app") != null) {
                        Intent intent = new Intent(HomeActivity.this,
                                (Class<?>) list.get(arg2).get("app"));

                        startActivity(intent);
                    } 
                }
            });

你可以試一下!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved