ListView的两种监听某一项的单击事件的方法

  • Post author:
  • Post category:其他



第一种,通过


Adapter





parent.getAdapter


()方法来获取


adapter=new FirstListViewAdapter(list,getActivity());


listView.setAdapter(adapter);


//


监听单击项的事件


listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {


@Override


public voidonItemClick(AdapterView<?> parent, View view, int position, long id) {


Adapter nadapter=parent.getAdapter();


//


获得单条的数据


Map<String,Object> map= (Map<String, Object>)nadapter.getItem(position);


//


获得当前点击的新闻的编号


StringnId=map.get(“id”).toString();


//


跳转窗体


Intent intent=newIntent(getActivity(), NewContentActivity.class);


intent.putExtra(“id”,nId);


getActivity().startActivity(intent);


}


});


}


第二种,通过


TextView


获得


id


的值得方法来获取



listView



.setOnItemClickListener(

new

AdapterView.OnItemClickListener(){


@Override


public void

onItemClick(AdapterView<?>parent, View view,

int

position,

long

id) {


TextView textid = (TextView)view.findViewById(R.id.


textid


);




lvid

= textid.getText().toString();

Intent intent =

new

Intent(

context

,ContentActivity.

class

);

intent.putExtra(

“id”

,

lvid

);


context

.startActivity(intent);

}

});



版权声明:本文为languobeibei原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。