我创建了Listview,现在想知道在单击列表视图项时我们如何启动另一个活动?我真的不知道如何进一步编码。请指导我,帮助我。我的编码是,
MainActivity.Java
package com.topitideas.newlistview;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity{
ListView list;
String[] itemname ={
"Safari",
"Camera",
"Global",
"FireFox",
"UC Browser",
"Android Folder",
"VLC Player",
"Cold War"
};
Integer[] imgid={
R.drawable.pic1,
R.drawable.pic2,
R.drawable.pic3,
R.drawable.pic4,
R.drawable.pic5,
R.drawable.pic6,
R.drawable.pic7,
R.drawable.pic8,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
list=(ListView)findViewById(R.id.list);
list.setAdapter((ListAdapter) adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Slecteditem= itemname[+position];
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
CustomListAdapter.java
> package com.topitideas.newlistview;
>
>
> import android.app.Activity;
> import android.view.LayoutInflater;
> import android.view.View;
> import android.view.ViewGroup;
> import android.widget.ArrayAdapter;
> import android.widget.ImageView;
> import android.widget.TextView;
>
> public class CustomListAdapter extends ArrayAdapter<String> {
>
> private final Activity context;
> private final String[] itemname;
> private final Integer[] imgid;
>
> public CustomListAdapter(MainActivity context, String[] itemname, Integer[] imgid) {
> super(context, R.layout.mylist, itemname);
> // TODO Auto-generated constructor stub
>
> this.context=context;
> this.itemname=itemname;
> this.imgid=imgid;
> }
>
> public View getView(int position,View view,ViewGroup parent) {
> LayoutInflater inflater=context.getLayoutInflater();
> View rowView=inflater.inflate(R.layout.mylist, null,true);
>
> TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
> ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
> TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);
>
> txtTitle.setText(itemname[position]);
> imageView.setImageResource(imgid[position]);
> extratxt.setText("Description "+itemname[position]);
> return rowView;
>
> }; }
mylist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/icon"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#33CC33" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_marginLeft="10dp"/>
</LinearLayout>
</LinearLayout>
答案 0 :(得分:1)
只需使用Intent并启动它
Intent intent = new Intent(MainActivity.this, NextActivity.class);
intent.putExtra("key", "value");
startActivity(intent);
将它放在你的onItemClick
中在NextActivity中:
Bundle extras = getIntent().getExtras();
if (extras != null)
{
String myParam = extras.getString("key");
}