简体   繁体   English

来自其他活动的信息

[英]Information from another activity

I've been searching my problem but i can't find anything that helps my case. 我一直在寻找问题,但找不到任何可以帮助我解决这个问题的方法。 I have an activity with a ListView where you can see names of people in a Database and when you click in one of the name it should go to another activity to show the details but it crashes when i click the name. 我有一个带有ListView的活动,您可以在其中看到数据库中的人员名称,当单击其中一个名称时,应该转到另一个活动以显示详细信息,但是当我单击该名称时会崩溃。 Code above: 上面的代码:

First Activity: Code that shows all the names in ListView is working 第一个活动:显示ListView中所有名称的代码正在运行

myview.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //Toast.makeText(getApplicationContext(), "clicked", 3000).show();
            setContentView(R.layout.fragment_vertodos);
            lv=(ListView) findViewById(R.id.listone);
            Cursor cursor=mydb.rawQuery("SELECT * FROM teste;", null);

            if(cursor.moveToFirst()){
                Toast.makeText(getApplicationContext(), "Nome:", 3000).show();
                data.clear();
                do{
                    data.add(cursor.getString(cursor.getColumnIndex("name")));
                }while (cursor.moveToNext());   
                ArrayAdapter <String> adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,data);
                lv.setAdapter(adapter);
            }
                else{
                    Toast.makeText(getApplicationContext(), "DATA NOT AVAILABLE", 3000).show();
                }
            cursor.close();}
    });


lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> l, View v, int position,long id) {
                    // TODO Auto-generated method stub
                    String name = (String) l.getItemAtPosition(position);
                    List<String> emailLists = new ArrayList<String>();
                    emailLists.add(cursor.getString(cursor.getColumnIndex("email")));
                    String email = emailLists.get(position);

                    Intent intent = new Intent(MainActivity.this, Detalhes.class);
                    intent.putExtra("name",name);
                    intent.putExtra("email",email);
                    startActivity(intent);  
                }
            });

My question is now. 我的问题是现在。 i created a second activity that get's the information but it's giving an error: 我创建了第二个活动,该活动获得了信息,但出现了错误:

Second Activity: 第二项活动:

txname = (TextView)findViewById(R.id.txtnome);
    txmail = (TextView)findViewById(R.id.txtmail);


    Bundle b = getIntent().getExtras();

    if(b!=null){
        String n =(String) b.get("name");
        String m =(String) b.get("email");
        txname.setText(n);
        txmail.setText(m);

    }

The first error is: java.lang.ClassCastException: java.lang.String cannot be cast to android.database.Cursor 第一个错误是:java.lang.ClassCastException:无法将java.lang.String强制转换为android.database.Cursor

我怀疑您正在将ArrayAdapter<String>设置为ListView ,因此在以下行中, l.getItemAtPosition(position)返回String ,而不是Cursor ,并且l.getItemAtPosition(position)失败。

Cursor detalhes = (Cursor) l.getItemAtPosition(position);
 The first error is: java.lang.ClassCastException: java.lang.String cannot be cast to android.database.Cursor

Issue is your trying to type cast the String to Cursor at this line 问题是您尝试在此行将String类型转换为Cursor

 Cursor detalhes = (Cursor) l.getItemAtPosition(position);

getItemAtPosition will return the data associated with the specified position in the list. getItemAtPosition将返回与列表中指定位置关联的数据。

So, in your case your have passed List<String> data as the data for the list items. 因此,在您的情况下,您已经传递了List<String> data作为列表项的数据。 so each item will have as string object 所以每个项目都有一个字符串对象

String name = (String) l.getItemAtPosition(position);

If you need to pass the email of that items. 如果您需要通过该项目的电子邮件。 You can create a ArrayList for email and add it from database like this. 您可以为电子邮件创建一个ArrayList,然后从数据库中添加它,如下所示。

  List<String> emailLists = new ArrayList<String>();

And, add the emails into the list 并且,将电子邮件添加到列表中

 data.add(cursor.getString(cursor.getColumnIndex("email"))); 

Now, When the item selected, get the email from the ArrayList 现在,当选择该项目时,从ArrayList获取电子邮件

String email = emailLists.get(position);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM