我为 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.