2

我的应用程序是一个公司列表,每个公司都是我的列表视图中的一行,显示:图片、姓名和电话(通过适配器)。一切正常!但是当我点击我的列表视图中的一个项目时,它应该开始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); }});

我的详细信息Empresas 活动

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);
}}
4

2 回答 2

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;
    } 
}
于 2013-05-17T21:08:24.457 回答
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); 
}});

---------- 新编辑 -----------------

好的,您只需向父活动添加一个点击处理程序怎么样,您应该很高兴:

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); 
于 2013-05-17T21:01:54.950 回答