0

我为 listview 制作了一个简单的 android 程序,用于学习目的,因为我使用了带有文本和图像的 alistview。现在我想要的是当我单击列表中的项目时,它应该使用该特定图像和文本进入下一个活动..我已经实现了如下那不起作用请帮助我:

主要的

package com.example.androidlistview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {
     String[] smily = new String[]  {"Angry","Basic","Cool","Cry","Err","Evil","Kiss","Laugh","Shame"
                ,"Toungh","Wink","Wonder"};

        int[] pic =new int[]{
                R.drawable.ic_action_emo_angry,
                R.drawable.ic_action_emo_basic,
                R.drawable.ic_action_emo_cool,
                R.drawable.ic_action_emo_cry,
                R.drawable.ic_action_emo_err,
                R.drawable.ic_action_emo_evil,
                R.drawable.ic_action_emo_kiss,
                R.drawable.ic_action_emo_laugh,
                R.drawable.ic_action_emo_shame,
                R.drawable.ic_action_emo_tongue,
                R.drawable.ic_action_emo_wink,
                R.drawable.ic_action_emo_wonder

        };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

            for(int i=0;i<10;i++){
                HashMap<String, String> hm = new HashMap<String,String>();
                hm.put("sml", " " + smily[i]);

                hm.put("pic", Integer.toString(pic[i]) );
                aList.add(hm);
            }
            String[] from = { "pic","sml" };


            int[] to = { R.id.smily,R.id.txt};


            SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.list_layout, from, to);

            final ListView listView = ( ListView ) findViewById(R.id.listView1);

            listView.setAdapter(adapter);
            listView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub
                     Intent intent = new Intent(MainActivity.this,Descripsn.class);
                        String smiley=(String)listView.getSelectedItem();
                        Bundle b =new Bundle();
                        intent.putExtra("smily", smiley);
                        intent.putExtra("image", smily[arg2]);
                        startActivity(intent);
                }

            });


    }



}

描述java

package com.example.androidlistview;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;

public class Descripsn extends Activity {
TextView tv;
ImageView im;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_descripsn);
        tv=(TextView)findViewById(R.id.textView1);
        im=(ImageView)findViewById(R.id.imagedesc);

        Intent i =getIntent();
        Bundle b=getIntent().getExtras();
        String smily = b.getString("smiley");
        int img = b.getInt(smily);
        tv.setText(smily);
    }


}

日志

06-21 09:44:38.263: I/Choreographer(4029): Skipped 53 frames!  The application may be doing too much work on its main thread.
06-21 09:44:39.523: I/Choreographer(4029): Skipped 58 frames!  The application may be doing too much work on its main thread.
06-21 09:44:48.243: I/Choreographer(4029): Skipped 74 frames!  The application may be doing too much work on its main thread.
06-21 09:44:48.633: I/Choreographer(4029): Skipped 200 frames!  The application may be doing too much work on its main thread.
06-21 09:44:48.853: I/Choreographer(4029): Skipped 46 frames!  The application may be doing too much work on its main thread.
06-21 09:44:49.685: I/Choreographer(4029): Skipped 43 frames!  The application may be doing too much work on its main thread.
06-21 09:44:54.079: I/Choreographer(4029): Skipped 44 frames!  The application may be doing too much work on its main thread.
06-21 09:44:54.733: I/Choreographer(4029): Skipped 46 frames!  The application may be doing too much work on its main thread.
4

4 回答 4

1

试试这个

 @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {

                 Intent intent = new Intent(MainActivity.this,Descripsn.class);

                    intent.putExtra("smily", smily[arg2].toString());
                    intent.putExtra("image", pic[arg2]);
                    intent.putExtra("pos", arg2);
                    startActivity(intent);
            }

并参加其他活动

Bundle b=getIntent().getExtras();
    String smily = b.getString("smily");
    int img = b.getInt("image");
    int imgpos = b.getInt("pos");
于 2013-06-21T10:02:58.383 回答
0

在您的 Descriptsn 类中,您必须对 getExtras. 使用相同的标识符。

试试这样。。

Intent i =getIntent();
        Bundle b=getIntent().getExtras();
        String smily = b.getString("smily");
        String img = b.getString("image");
        tv.setText(smily);

编辑:- 您将根据需要获得传递的文本..但是对于图像,您可以将选定的列表位置作为标识符发送到您的下一个意图,如下所示..

 @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub
                     Intent intent = new Intent(MainActivity.this,Descripsn.class);
                        String smiley=(String)listView.getSelectedItem();
                        Bundle b =new Bundle();
                        intent.putExtra("smily", smiley);
                        intent.putExtra("image", smily[arg2]);
                        intent.putExtra("pos", arg2);
                        startActivity(intent);
                }

然后得到这样的额外,,

Intent i =getIntent();
            Bundle b=getIntent().getExtras();
            String smily = b.getString("smily");
            String img = b.getString("image");
            int imgpos = b.getInt("pos");
            tv.setText(smily);
于 2013-06-21T09:35:11.970 回答
0
String smily = b.getString("smiley");

错误的))

试试这个:

String smily = b.getString("smily");
于 2013-06-21T09:52:29.997 回答
0

main.java
包 com.example.androidlistview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MainActivity extends Activity {
     String[] smily = new String[]  {"Angry","Basic","Cool","Cry","Err","Evil","Kiss","Laugh","Shame"
                ,"Toungh","Wink","Wonder"};

        public int[] pic =new int[]{
                R.drawable.ic_action_emo_angry,
                R.drawable.ic_action_emo_basic,
                R.drawable.ic_action_emo_cool,
                R.drawable.ic_action_emo_cry,
                R.drawable.ic_action_emo_err,
                R.drawable.ic_action_emo_evil,
                R.drawable.ic_action_emo_kiss,
                R.drawable.ic_action_emo_laugh,
                R.drawable.ic_action_emo_shame,
                R.drawable.ic_action_emo_tongue,
                R.drawable.ic_action_emo_wink,
                R.drawable.ic_action_emo_wonder

        };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

            for(int i=0;i<10;i++){
                HashMap<String, String> hm = new HashMap<String,String>();
                hm.put("sml", " " + smily[i]);

                hm.put("pic", Integer.toString(pic[i]) );
                aList.add(hm);
            }
            String[] from = { "pic","sml" };


            int[] to = { R.id.smily,R.id.txt};


            SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.list_layout, from, to);

            final ListView listView = ( ListView ) findViewById(R.id.listView1);

            listView.setAdapter(adapter);
            listView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub
                    Intent intent = new Intent(MainActivity.this,Descripsn.class);
                    Bundle b =new Bundle();
                    intent.putExtra("smily", smily[arg2]);
                    intent.putExtra("image", pic[arg2]);
                    startActivity(intent);
                }

            });


    }



}

desc.jav

package com.example.androidlistview;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;

public class Descripsn extends Activity {
TextView tv;
ImageView im;
private MainActivity main = new MainActivity();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_descripsn);
        tv=(TextView)findViewById(R.id.textView1);
        im=(ImageView)findViewById(R.id.imagedesc);

        Intent i =getIntent();
        Bundle b=  getIntent().getExtras();
        String smily = b.getString("smily");
       int img = b.getInt("image");
        String imgpos = b.getString("pos");

        System.out.println("Image value"+img);

        //System.out.println("======Posi::::::"+smily);

       tv.setText(smily);
       im.setBackgroundResource(img);
    }


}
于 2013-06-21T10:40:56.600 回答