I'm pretty new in the Android programming world. I'm using the code from this site and I want to click on the images to open a new activity. Can anyone help me with this?
This is the xml:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:dividerHeight="0px"
tools:context=".StreamActivity" />
And the first part of the Java code:
public class StreamActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stream);
StreamAdapter adapter = new StreamAdapter(this);
((ListView) findViewById(R.id.main_list)).setAdapter(adapter);
adapter.add(new StreamItem(this, R.drawable.photo1, "Option1", "Click to open"));
adapter.add(new StreamItem(this, R.drawable.photo2, "Option2", "Click to open"));
adapter.add(new StreamItem(this, R.drawable.photo3, "Option3", "Click to open"));
adapter.add(new StreamItem(this, R.drawable.photo4, "Option4", "Click to open"));
adapter.add(new StreamItem(this, R.drawable.photo5, "Option5", "Click to open"));
adapter.add(new StreamItem(this, R.drawable.photo6, "Option6", "Click to open"));
adapter.add(new StreamItem(this, R.drawable.photo7, "Option7", "Click to open"));
ListView.setOnItemClickListener
. You can have your Activity implement the interface and then call listView.setOnItemClickListener(this);
inside of onCreate()
.
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// create an Intent to open the next Activity.
// If you need information from the selected item, use
StreamItem item = adapter.getItem(position);
}
Add listener for clicks on your list
yourList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(context, YourTargetClass.class);
startActivity(intent);
}
});
Check out the ListView guide if you haven't: Android developer ListView guide
That example extends ListActivity and implements onListItemClick. In that method, create an Intent and call startActivity.
Here's another example with lots of information and a different way to do the listener: ListView tutorial
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.