当前位置: 动力学知识库 > 问答 > 编程问答 >

android - How to implement onitemclicklistener in array adapter

问题描述:

i am confused, i want to add an onitemclicklistener in my adapter

i have no idea how do i integrate the following code in my code.

any help would be appreciated.

here is the adapter

listView.setOnItemClickListener(new OnItemClickListener() {

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

if (position == ITEM_POSITION_HERE)

{

Intent myIntent = new Intent(getApplicationContext(), ActivityName.class);

startActivity(myIntent);

}

}

});

and here is my java file

import android.app.Activity;

import android.os.Bundle;

import android.widget.ArrayAdapter;

import android.widget.ListView;

public class MainActivity extends Activity {

private ListView list1;

private String array[] = { "Iphone", "Tutorials", "Gallery", "Android",

"item 1", "item 2", "item3", "item 4" };

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

list1 = (ListView) findViewById(R.id.ListView01);

// By using setAdpater method in listview we an add string array in

// list.

list1.setAdapter(new ArrayAdapter(this,

android.R.layout.simple_list_item_1, array));

}

}

网友答案:

Try out as below:

list1.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
         if (position ==items[ITEM_POSITION_HERE])
        {
          Intent myIntent = new Intent(getApplicationContext(),NextActivity.class);
          startActivity(myIntent);
      }
    }
});
网友答案:

Try

list1 = (ListView) findViewById(R.id.ListView01);
list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
    if (position == ITEM_POSITION_HERE)
    {
        Intent myIntent = new Intent(getApplicationContext(),ActivityName.class);
        startActivity(myIntent);
    }
}
});
list1.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, array));
网友答案:

Set onclicklistener to listview not adapter

list1.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

  // do whatever you want
    }
});
网友答案:

You Need to do that

list1.setOnItemClickListener(your listener);

 onListItemClick(ListView l, View v, int position, long id) {

    super.onListItemClick(l, v, position, id);      

    Intent i = new Intent(this, yourClass.class);

    startActivity(i);
}
网友答案:

I am editing your code. Please replace your JAVA file with this

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

private ListView list1;

private String array[] = { "Iphone", "Tutorials", "Gallery", "Android",

"item 1", "item 2", "item3", "item 4" };

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

list1 = (ListView) findViewById(R.id.ListView01);

// By using setAdpater method in listview we an add string array in

// list.

list1.setAdapter(new ArrayAdapter(this,

android.R.layout.simple_list_item_1, array));
    list1 .setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
    if (position == ITEM_POSITION_HERE)
    {
        Intent myIntent = new Intent(getApplicationContext(),         ActivityName.class);
        startActivity(myIntent);
    }
}
});
}

}

Do not incluse ONITEMCLICKLISTENER with your adapter class. Include it in you main java class

分享给朋友:
您可能感兴趣的文章:
随机阅读: