你好,我正在做一个应用程序,我有一个listview与imageView和textView如何打开一个新的活动项目点击列表视图项目这里是我的代码,
public class Chapters extends Activity {
// Array of strings for ListView Title
String[] listviewTitle = new String[]{
"ListView Title 1", "ListView Title 2", "ListView Title 3", "ListView Title 4",
"ListView Title 5", "ListView Title 6", "ListView Title 7", "ListView Title 8",
};
int[] listviewImage = new int[]{
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
};
String[] listviewShortDescription = new String[]{
"Android ListView Short Description", "Android ListView Short Description", "Android ListView Short Description", "Android ListView Short Description",
"Android ListView Short Description", "Android ListView Short Description", "Android ListView Short Description", "Android ListView Short Description",
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chapters);
final List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 8; i++) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("listview_title", listviewTitle[i]);
hm.put("listview_discription", listviewShortDescription[i]);
hm.put("listview_image", Integer.toString(listviewImage[i]));
aList.add(hm);
}
String[] from = {"listview_image", "listview_title", "listview_discription"};
int[] to = {R.id.listview_image, R.id.listview_item_title, R.id.listview_item_short_description};
SimpleAdapter simpleAdapter = new SimpleAdapter(getBaseContext(), aList, R.layout.chapters_image_text, from, to);
final ListView androidListView = (ListView) findViewById(R.id.list_view);
androidListView.setAdapter(simpleAdapter);
androidListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//what code do i insert here to open new activity Toast toast = Toast.makeText(getApplicationContext(),
}
});
}
}
答案 0 :(得分:2)
在onItemClick
方法中使用该代码显示Toast:
Toast.makeText(getApplicationContext(), "Message", Toast.LENGTH_SHORT).show();
如果你想打开另一个Activity,请使用:
Intent intent= new Intent(MyActivity.this, ResultActivity.class);
startActivity(intent);
答案 1 :(得分:0)
尝试这样的事情
Intent intent= new Intent(Intent.ACTION_VIEW, Uri.parse("http://stackoverflow.com"));
startActivity(intent);
答案 2 :(得分:0)
只是一个建议,您可以像这样使用ArrayAdapter类:
1-创建一个arrayAdapter类:
private class YourArrayAdapter extends ArrayAdapter<String>{
public Context context;
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.your_item_layout, parent, false);
//get your views which exist in a one item ex:
TextView textView =(TextView)view.findViewById(R.id.text_view);
Button button=(Button)view.findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//you can use position paramater to know which item was clicked
Intent intent= new Intent(YourActivity.this, ActivityYouWantToLaunch.class);
startActivity(intent);
}
});
return view;
}
public YourArrayAdapter(Context context, int resource,int textViewResourceId) {
super(context, resource, textViewResourceId,yourArrayList);
this.context=context;
}
}
2- show list在activity上的项目。在你放入所有arrayList的项目之后,像这样传递你的arrayAdapter:
YourArrayAdapter yourArrayAdapter= new YourArrayAdapter(this,R.layout.your_item_layout, android.R.id.text1);
yourListView.setAdapter(yourArrayAdapter);
我希望这会对你有所帮助