将onClickListener添加到ListView中的特定项目 [英] Adding onClickListener to specific items in ListView

查看:75
本文介绍了将onClickListener添加到ListView中的特定项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请尝试增加对如何在后续代码中添加onItemClickListener的理解,以便在单击智能手机计划"时开始其活动,依此类推.我在StackOverflow上看到了与此问题相关的其他问题,但不了解如何解决.我已经添加了onItemClickListener,但不了解如何将其设置为特定的列表项.这是代码

please I'm trying to add understand how to add onItemClickListener to the followng code such that when "Smartphone Plans" is clicked, its activity starts and so on. I've seen other questions on StackOverflow relating to this question but do not understand how to go about them. I've already added an onItemClickListener but do not understand how to set it to specific list items. here is the code

package devchuks.com.rechargeit;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.app.ListActivity;

import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class EtisalatData extends AppCompatActivity {
    ListView listView;

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

        listView = (ListView) findViewById(R.id.list);


        String[] values = new String[] {
                "Smartphone Plans",
                "Internet Bundles",
                "Weekend Plans",

        };



        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, values);



        listView.setAdapter(adapter);


        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {


            }

        });
    }

}

推荐答案

您可以像这样定义您的 listView.setOnItemClickListener 进入不同的活动,以单击不同的元素.

You can define your listView.setOnItemClickListener like this to go to different activity for clicking different element.

listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
         String item = listView.getItemAtPosition(position);
         Toast.makeText(this,"You selected : " + item,Toast.LENGTH_SHORT).show();
         if(position==0) {
            // Do your code for clicking "Smartphone Plans" example
            // startActivity(new Intent(getApplicationContext(),SmartphonePlans.class));
         }
        else if(position==1) { 
           // Do your code for clicking "Internet Bundles". example
          // startActivity(new Intent(getApplicationContext(),InternetBundles.class));
         }
        else if(position==2) { 
           // Do your code for clicking "Weekend Plans". example 
          //startActivity(new Intent(getApplicationContext(),WeekendPlans.class));*/
    }

});

这篇关于将onClickListener添加到ListView中的特定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆