0

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.

Smern
  • 18,746
  • 21
  • 72
  • 90
Lawrence
  • 63
  • 1
  • 9
  • 2
    duplicate of http://stackoverflow.com/questions/9097723/adding-an-onclicklistener-to-listview-android – dex Sep 28 '15 at 16:41

3 Answers3

1

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);
    }
}
Andrew Brooke
  • 12,073
  • 8
  • 39
  • 55
0

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

Pavan
  • 5,016
  • 1
  • 26
  • 30
-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
        }
    });

}
subhash
  • 2,689
  • 18
  • 13
  • Sir I just want to have to make clickable all array I've been listed. Is this possible? – Lawrence Sep 28 '15 at 16:43
  • this is what it does. put above code below line listView.setAdapter(adapter); – subhash Sep 28 '15 at 16:44
  • listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> listView, View view, int position, long id) { String grocery = listView.getAdapter().getItem(position); Intent intent = new Intent(listView.getContext(),Login.class); listView.getContext().startActivity(intent); //or create other intents here } }); – Lawrence Sep 28 '15 at 16:45
  • There is something wrong sir to the line "String grocery = listView.getAdapter().getItem(position);" in your suggestion – Lawrence Sep 28 '15 at 16:47
  • its String grocery = (String) listView.getAdapter().getItem(position); – subhash Sep 28 '15 at 16:50