Android - How to create clickable listview?
In fact it is quite easy:
This is your Activity with the ListView, it implements an OnItemClickListener:
public class MainActivity extends Activity implements OnItemClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
//* *EDIT* *
ListView listview = (ListView) findViewById(R.id.listView1);
listview.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
Log.i("HelloListView", "You clicked Item: " + id + " at position:" + position);
// Then you start a new Activity via Intent
Intent intent = new Intent();
intent.setClass(this, ListItemDetail.class);
intent.putExtra("position", position);
// Or / And
intent.putExtra("id", id);
startActivity(intent);
}
Edit
The above code would be placed in your MainActivity.java. I changed the name of the class to MainActivity
and the contentView to setContentView(R.layout.activity_main)
- The names are those of a freshly created Android Project in Eclipse.
Please see also the 2 new lines under //* Edit * - those will set the Listener for clicks on items in the list.
Your activity_main.xml should look like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:entries="@array/sections" >
</ListView>
</RelativeLayout>
The array.xml (not string.xml) in your `res/values/` folder looks like this
<resources>
<string-array name="sections">
<item >Pro Constructive</item>
<item >Con Constructive</item>
<item >1st Speaker Cross</item>
<item >Pro Rebbutal</item>
<item >Con Rebuttal</item>
<item >2nd Speaker Cross</item>
<item >Pro Summary</item>
<item >Con Summary</item>
<item >Grand Cross</item>
<item >Pro Final Focus</item>
<item >Con Final Focus</item>
</string-array>
</resources>
N.B.: If you copy & paste this code it should work. But you will get an error by clicking on an Item because you haven't created the ListItemDetail.class
yet.
Here is an example of how this could look:
Your ListItemDetail.java:
public class ListItemDetail extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listitem);
Intent intent = getIntent();
int position = intent.getIntExtra("position", 0);
// Here we turn your string.xml in an array
String[] myKeys = getResources().getStringArray(R.array.sections);
TextView myTextView = (TextView) findViewById(R.id.my_textview);
myTextView.setText(myKeys[position]);
}
}
And its activity_listitem.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/my_textview"/>
</LinearLayout>
If you copy past this code it will work.
How to make items clickable in list view?
Edited Answer
Apply This Sample Please This Works For You I have Tested This Code
//ListviewActivity.java
package com.anl.lk;
public class ListviewActivity extends ListActivity {
static final String[] COUNTRIES = new String[] {
"Afghanistan", "Albania", "Algeria", "American Samoa",
"Andorra", "Angola", "Anguilla", "Antarctica",
"Antigua and Barbuda", "Argentina", "Armenia", "Aruba",
"Australia", "Austria", "Azerbaijan", "Bahrain",
"Bangladesh", "Barbados", "Belarus", "Belgium", "Belize",
"Benin", "Bermuda", "Bhutan", "Bolivia",
"Bosnia and Herzegovina", "Botswana", "Bouvet Island",
"Brazil", "British Indian Ocean Territory"
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter < String > (this,
android.R.layout.simple_list_item_1, COUNTRIES));
getListView().setTextFilterEnabled(true);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
new AlertDialog.Builder(this)
.setTitle("Hello")
.setMessage("from " + getListView().getItemAtPosition(position))
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {}
})
.show();
Toast.makeText(ListviewActivity.this,
"ListView: " + l.toString() + "\n" +
"View: " + v.toString() + "\n" +
"position: " + String.valueOf(position) + "\n" +
"id: " + String.valueOf(id),
Toast.LENGTH_LONG).show();
}
}
//FirstPage
package com.anl.lk;
public class FirstPage extends Activity {
@override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent sam = new Intent(FirstPage.this, ListviewActivity.class);
startActivity(sam);
}
});
}
}
//Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.anl.lk"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".FirstPage" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ListviewActivity" android:theme="@android:style/Theme.Dialog"></activity>
</application>
Please Use This Code it have your Solution
Clickable items in listview - Android Studio
Luiz's answer is correct. But you do not especially need to implement the fonction. You can make a new instance instead :
myListView.setOnItemClickListener(new OnItemClickListener(){
@Override
Public void onItemClick(AdapterView<?> p1, View p2, int i, long p4)
{
// Your stuff here
}
});
Where p1 is your listView, p2 is the clicked view (useful for finding text and changing it inside), p3 is the position in the listView, p4 the unique id.
How do I make the ListView clickable and show some text once it gets clicked?
I think you need good code working samples and a tutorial. A good link is [Using an ArrayAdapter with ListView][1]
[1]: https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView .
You asked to populate the Listview, look at the code extending the ArrayAdapter.
Since you want to write text on the bottom, you can create a TextView outside the LinearLayout in the res/layout/item_user.xml. That is one simple way. Try some code and see what you get. You'll have to participate half way since you can learn a lot here.
How to make ListView items clickable?
Use listview's OnItemClickListener
lv.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
// based on the item clicked go to the relevant activity
String clickedItem = (String)adapter.getItemAtPosition(position);
}
});
Make a whole ListView row clickable
Probably the best idea is extending an ArrayAdapter
and override the getView()
method. The second parameter of this method is a View
(usually called convertView
, but not necessarily), vaguely speaking, is the representation of a row, so this method will be called for each row. Simply declare your onClickListener()
over it.
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
...
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
...
}
});
...
}
Related Topics
Sending Http Post Request with Android
How to Show Ellipses on My Textview If It Is Greater Than the 1 Line
Android Calculate Days Between Two Dates
Internet Listener Android Example
Broadcastreceiver for Action_Media_Button Not Working
Google Cloud Messaging: Don't Receive Alerts When iOS App Is in Background
Convert from Enum Ordinal to Enum Type
Setting a Timeout for Socket Operations
Android Resource Not Found Exception
Passing Function as a Parameter in Java
How to Draw an Arrowhead (In Android)
How to Create JSON Object Using String
How Get Value from Linkedhashmap Based on Index Not on Key
Theme.Appcompat.Light.Darkactionbar - No Resource Found
Aes Gcm Implementation with Authentication Tag in Java
Integration Testing Frameworks for Testing a Distributed System