0

我对 Android 开发有点陌生。您能否告诉我,如何使用onListItemClick. 我有 200 个项目ListView。如果我从 中单击一个项目ListView,它必须导航到另一个屏幕,该屏幕应显示所单击项目的详细信息。

4

2 回答 2

0

我假设您正在从数据库中获取演员的列表和详细信息。我使用相同的演练在我的应用程序中显示所选食谱的食谱详细信息。

    public class ActorList extends Activity {

       ListView myActorList=null;

public final static String selectedActor_ID="will Keep Actor ID";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

        setContentView(R.layout.layout_actor_list);     

        //initialize controls

            myActorList.setAdapter(adapter);

            myActorList.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View v,
                        int position, long id) {
                    // TODO Auto-generated method stub


                    Intent i= new Intent(ActorList.this,ActorDetail.class);
                    // you have to pass the actor id to next activity
                    // you can get this actor id from argument of type "long"
                    i.putExtra(selectedActor_ID, String.valueOf(id));
                    startActivity(i);

                }           

            });

    }



}


//Other Activity To show Detail..

//get the ID of Selected Actor on other activity say "ActorDetail"

public class ActorDetail extends Activity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_actor_detail);

        ActorID=getIntent().getStringExtra(ActorList.selectedActor_ID);
// now as you have the id here for that particular actor
//fetch the detail of that selected actor thru id and bind it to your layout.       


    }



}       

您还需要像这样在清单文件中注册其他活动

<activity 
            android:name=".otherscreenActivity" 

        </activity>
于 2013-04-01T09:05:22.353 回答
0

为了得到你想要的,你可以做这样的事情......

1>使用某些控件创建另一个活动,您可以在其中放置您的值...

2>在listview的itemclick()上... a>创建意图并设置值以传递给新活动b>以此意图开始新活动。

3>在新活动的oncreate()中a>从意图中检索值b>使用这些值填充您的控件...

于 2013-04-02T07:14:11.193 回答