2 xiaoyan 12 xiaoyan_12 于 2012.12.04 13:38 提问

在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?

3个回答

zhenglingkun
zhenglingkun   2012.12.04 18:26
已采纳

如果是要实现点击相对应的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);
                    } 
                }
            });

你可以试一下!

xiaoyan_12
xiaoyan_12 谢谢你的回答,帮我解决了问题,你可以编辑代码格式哦。
大约 5 年之前 回复
LiDaming_and
LiDaming_and   2012.12.04 15:29

有两种选择来处理每一行的单击事件
1)

@Override

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    //使用数组中的位置实现其它的
}

2) 在getView()方法中处理每一行的点击事件

row.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

    }
});
yongyong_21
yongyong_21   2012.12.06 10:11

试试这个方法:

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);
            }
          });
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!