我为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);
}
});
}
}
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.
答案 0 :(得分: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");
答案 1 :(得分: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);
答案 2 :(得分:0)
String smily = b.getString("smiley");
错))
试试这个:
String smily = b.getString("smily");
答案 3 :(得分:0)
<强> main.java 强>
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;
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);
}
}