Don't Miss

Opens an activity on item click of listview android

By on January 13, 2018

Activity.java

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ExampleActivity extends AppCompatActivity {

private ListView listView;
private ArrayAdapter arrayAdapter;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

String[ ] citiesname = getResources().getStringArray(R.array.cities_name);
listView = (ListView) findViewById(R.id.list_item);
ArrayAdapter adapter = new ArrayAdapter(this,
R.layout.listitems,R.id.textView2,citiesname);
listView.setAdapter(adapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
startActivity(new Intent(ExampleActivity.this,ExampleActvity2.class));
}
});
}

}

activity_main.xml

add string array to res/values style.xml 

ISLAMABAD
KARACHI
PESHAWAR
Queeta
ISLAMABAD
KARACHI
PESHAWAR
Queeta
ISLAMABAD
KARACHI
PESHAWAR
Queeta
ISLAMABAD
KARACHI
PESHAWAR
Queeta

  <ListView android:id=”@+id/list_item”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_alignParentTop=”true” />

About CodingBazz

Leave a Reply

Your email address will not be published. Required fields are marked *