Ashu Kumar Ashu Kumar - 4 years ago 180
Android Question

how to get Index and String from ListView on ArrayAdapter class

i m creating webservice application. it has ArrayAdapter class that is present the web data to Listview. and i want to get index of selected Listview Item and send the index value to next activity to do other task. but i m not able to get Index from ListView. ListView id is default Android API id

I used almost everything like setOnItemSelectedListener and setOnClickListener OnItemSelectedListener but did not work

protected void onCreate(Bundle savedInstanceState) {


private void initView() {
// show progress dialog
dialog =, "", "Loading...");

String url = "xxxxxxxx";
// json class doing background data processing
FetchDataTask task = new FetchDataTask(this);

public void onFetchComplete(List<Application> data) {
// dismiss the progress dialog
if(dialog != null) dialog.dismiss();
// create new adapter
ApplicationAdapter adapter = new ApplicationAdapter(this, data);
// set the adapter to list


THIS IS ArrayAdapter class

public class ApplicationAdapter extends ArrayAdapter<Application> implements OnItemClickListener{
private List<Application> items;
ListView listview;
public ApplicationAdapter(Context context, List<Application> items) {
super(context, R.layout.app_custom_list, items);
this.items = items; AdapterView.OnItemSelectedListener()
public void onItemSelected(AdapterView<?> parentView, View childView, int position, long id)
String text = ((TextView)childView.getTag()).toString();
public void onNothingSelected(AdapterView<?> parentView)

public int getCount() {
return items.size();

public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;

if(v == null) {
LayoutInflater li = LayoutInflater.from(getContext());
v = li.inflate(R.layout.app_custom_list, null);

Application app = items.get(position);

if(app != null) {
ImageView icon = (ImageView)v.findViewById(;

for(int i=1; i<=5; i++) {
ImageView iv = new ImageView(getContext());

if(i <= app.getRating()) {
else {


return v;

public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String[] lv_arr = null;
// TODO Auto-generated method stub
String itemname = lv_arr[position];
Log.d("string from ListView",itemname);


Please help me to get Index of Selected Item of ListView. if anyone can suggest me what should i use , i will be grateful to them

thanks in Advance

Answer Source

at the bottom of protected void onCreate(Bundle savedInstanceState) { add

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

      //get position index of item here. 

       String indexid = String.valueOf(position);  

       //and do whatever afterwards.                              



and remove public void onItemClick(AdapterView<?> parent, View view, int position, ....... in your adapter

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download