ListView中的意图 [英] Intent in ListView

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

问题描述

如何单击ListView.我的意思是说如何做到这一点.

How do I click a ListView. I mean how to make intent on it.

public class Login extends ActionBarActivity {
    ListView listView;
    ArrayAdapter<String> adapter;
    String[] grocery_categories = {"Beverages", "Bakery", "Canned Goods", "Condiments", "Dairy", "Snacks", "Frozen Foods",
                                    "Meat", "Produce", "Cleaners", "Paper Goods", "Personal Care", "Others"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        listView = (ListView) findViewById(R.id.list_view);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, grocery_categories);
        listView.setAdapter(adapter);    
    }
}

XMS

<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"
tools:context="com.example.admin.mobilegroceryapp.Login"
android:id="@+id/rl_login">

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/login_bckgrnd"
    android:src="@drawable/login_bckgrnd"
    android:scaleType="centerCrop"
    />

<ListView
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">



</ListView>

我是一名学生,正在为自己的论文探索Android Studio.

I'm an student by the way and exploring Android Studio for my thesis.

推荐答案

尝试

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
            String grocery = (String) listView.getAdapter().getItem(position);
            Intent intent = new Intent(listView.getContext(),/*Your activity*/);
            listView.getContext().startActivity(intent);
            //or create other intents here
        }
    });

现在您的代码应如下所示:

Now your code should look like:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
listView = (ListView) findViewById(R.id.list_view);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, grocery_categories);
listView.setAdapter(adapter);
  listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
            String grocery = (String) listView.getAdapter().getItem(position);
            Intent intent = new Intent(listView.getContext(),/*Your activity*/);
            listView.getContext().startActivity(intent);
            //or create other intents here
        }
    });

}

这篇关于ListView中的意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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