0

私は学習目的でリストビュー用のシンプルなアンドロイドプログラムを作成しました.テキストと画像で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);
                }

            });


    }



}

desc.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

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 に答える
0

Descripsn クラス内では、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 に答える