如何在数组适配器中实现onitemclicklistener

时间:2013-12-03 08:49:02

标签: android android-listview

我很困惑,我想在我的适配器中添加一个onitemclicklistener 我不知道如何在我的代码中集成以下代码。 任何帮助,将不胜感激。 这是适配器

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);
    }
}
});

这是我的java文件

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));

}

}

5 个答案:

答案 0 :(得分:0)

onclicklistener设为listview而非adapter

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

  // do whatever you want
    }
});

答案 1 :(得分:0)

尝试

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));

答案 2 :(得分:0)

试试如下:

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);
      }
    }
});

答案 3 :(得分:0)

你需要这样做

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);
}

答案 4 :(得分:0)

我正在编辑你的代码。请用此

替换您的JAVA文件
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);
    }
}
});
}

}

不要在您的适配器类中包含 ONITEMCLICKLISTENER 。将它包含在主java类