Android ListView与OnItemClick Listener有关

时间:2013-05-17 20:54:02

标签: android listview adapter onitemclicklistener onitemclick

我的应用程序是公司列表,每个公司在我的列表视图中有一行显示:图片,名称和电话(通过适配器)。一切正常!但是,当我点击列表视图中的某个项目时,它应该会显示another activity,其中显示包含详细信息的页面。我遇到onitemclick listener问题(它​​不起作用):

empresa = firm (in portuguese) lv = my listview
  lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
      Intent i = new Intent (getApplicationContext(), detalheEmpresas.class );
      Empresas empresa = (Empresas) parent.getItemAtPosition(position);
      i.putExtra("Nome", empresa.getTitle().toString());
      startActivity(i); }});

我的detalheEmpresas活动

public class detalheEmpresas extends Activity {

@Override
public void onCreate (Bundle savedInstanceState){

super.onCreate(savedInstanceState);
this.setContentView(R.layout.detalhes_empresa);
Intent i = getIntent();
String nome = i.getStringExtra("Nome");
TextView NOME = (TextView) findViewById(R.id.Nome);
NOME.setText(nome);
}}

2 个答案:

答案 0 :(得分:0)

很可能与getApplicationContext有关... 尝试使用活动上下文:

lv.setOnItemClickListener(new OnItemClickListener() {
  public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

  Intent i = new Intent (mSavedContext.getActivity(), detalheEmpresas.class ); <-------------

  Empresas empresa = (Empresas) parent.getItemAtPosition(position);
  i.putExtra("Nome", empresa.getTitle().toString());
  startActivity(i); 
}});

---------- new edit -----------------

好的,你只需要在父活动中添加一个点击处理程序,你就应该好了:

public class ParentActivity extends Activity implements OnItemClickListener {
   your activity code...

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

         Intent i = new Intent (getActivity(), detalheEmpresas.class ); 
         Empresas empresa = (Empresas) parent.getItemAtPosition(position);
         i.putExtra("Nome", empresa.getTitle().toString());
         startActivity(i);
   } 

在你的活动代码中替换

 lv.setOnItemClickListener(new OnItemClickListener() {

 lv.setOnItemClickListener(this); 

答案 1 :(得分:0)

我有一个非常类似的问题。通过设置单击侦听器来解决此问题,以便在列表适配器中查看自身。

public class EmpresasListAdapter extends ArrayAdapter<Empresas> {

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        //recycling view and setting view fields.

        /* Set listener to open firm detail activity on click */
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Your code to open firm detail activity
                Intent i = new Intent (getApplicationContext(), detalheEmpresas.class );
                Empresas empresa = (Empresas) parent.getItemAtPosition(position);
                i.putExtra("Nome", empresa.getTitle().toString());
                startActivity(i);
            }
        });
        return view;
    } 
}