Intent in ListView

我与影子孤独终老i 提交于 2019-12-13 08:40:14

问题


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

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>

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


回答1:


try

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

}



回答2:


set on itemclick listener to your listview you can explore more about listview here http://developer.android.com/guide/topics/ui/layout/listview.html if you want use intent on click of listview just put your code in listview click listener




回答3:


You can use an OnItemClickListener to determine which list item gets clicked.

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


来源:https://stackoverflow.com/questions/32827787/intent-in-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!