ListView 中的意图

Intent in ListView

如何单击 ListView。我的意思是如何对其进行意图。

JAVA

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。

尝试

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
        }
    });

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

@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
        }
    });

}

为您的列表视图设置 itemclick 侦听器 您可以在此处探索有关列表视图的更多信息 http://developer.android.com/guide/topics/ui/layout/listview.html 如果你想在点击列表视图时使用意图,只需将你的代码放在列表视图点击监听器中

您可以使用 OnItemClickListener 来确定哪个列表项被点击。

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String itemChosen = (String) parent.getItemAtPosition(position);
        Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
        intent.putExtra("groceryItem", itemChosen);
        startActivity(intent);
    }
}