点击该项目,没有任何反应。提示我没有正确实现方法OnItemClickListener。
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import com.lessons.Lesson1;
public class Lesson extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lesson);
ListView lista = (ListView) findViewById(R.id.itemlist);
ArrayList<Manager> arraydir = new ArrayList<Manager>();
Manager manager;
Resources res = getResources();
// Вводим данные
manager = new Manager(res.getDrawable(R.drawable.lesson1), res.getString(R.string.lesson1), res.getString(R.string.lessonname1));
arraydir.add(manager);
manager = new Manager(res.getDrawable(R.drawable.lesson2), res.getString(R.string.lesson2), res.getString(R.string.lessonname2));
arraydir.add(manager);
AdapterLesson adapter = new AdapterLesson(this, arraydir);
lista.setAdapter(adapter);
lista.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) {
TextView textView = (TextView) itemClicked.findViewById(R.id.lessonname);
String strText = textView.getText().toString();
if (strText.equalsIgnoreCase(getResources().getString(R.string.lesson1))) {
// Launch the lesson1 Activity
startActivity(new Intent(Lesson.this, Lesson1.class));
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
适配器
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class AdapterLesson extends BaseAdapter{
protected Activity activity;
protected ArrayList<Manager> items;
public AdapterLesson(Activity activity, ArrayList<Manager> items) {
this.activity = activity;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int arg0) {
return items.get(arg0);
}
@Override
public long getItemId(int position) {
return items.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Создаем convertView для эффективности
View v = convertView;
//Связываем формат списка, который мы создали
if(convertView == null){
LayoutInflater inf = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inf.inflate(R.layout.itemlist, null);
}
// Создаем объект директивы
Manager dir = items.get(position);
//Вводим фото
ImageView foto = (ImageView) v.findViewById(R.id.foto);
foto.setImageDrawable(dir.getFoto());
//Вводим номер урока
TextView lessonnumber = (TextView) v.findViewById(R.id.lessonnumber);
lessonnumber.setText(dir.getLessonnumber());
//Вводим название урока
TextView lessonname = (TextView) v.findViewById(R.id.lessonname);
lessonname.setText(dir.getLessonname());
// Возвращаем
return v;
}
}
管理器
import android.graphics.drawable.Drawable;
public class Manager {
protected Drawable foto;
protected String lessonnumber;
protected String lessonname;
protected long id;
public Manager(Drawable foto, String lessonnumber, String lessonname) {
super();
this.foto = foto;
this.lessonnumber = lessonnumber;
this.lessonname = lessonname;
}
public Manager(Drawable foto, String lessonnumber, String lessonname, long id) {
super();
this.foto = foto;
this.lessonnumber = lessonnumber;
this.lessonname = lessonname;
this.id = id;
}
public Drawable getFoto() {
return foto;
}
public void setFoto(Drawable foto) {
this.foto = foto;
}
public String getLessonnumber() {
return lessonnumber;
}
public void setLessonnumber(String lessonnumber) {
this.lessonnumber = lessonnumber;
}
public String getLessonname() {
return lessonname;
}
public void setLessonname(String lessonname) {
this.lessonname = lessonname;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
答案 0 :(得分:1)
使用此代码点击listitem并转到下一个extivity ..
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch(position){
case 0:
Intent firstIntent = new Intent(yourclass.this, firstitem.class);
startActivity(firstIntent);
break;
case 1:
Intent secondintent = new Intent(yourclass.this, seconditem.class);
startActivity(secondintent);
break;
答案 1 :(得分:1)
Try This code only change get view method part
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Создаем convertView для эффективности
View v = convertView;
//Связываем формат списка, который мы создали
if(convertView == null){
LayoutInflater inf = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inf.inflate(R.layout.itemlist, null);
}
// Создаем объект директивы
Manager dir = items.get(position);
//Вводим фото
ImageView foto = (ImageView) v.findViewById(R.id.foto);
foto.setImageDrawable(dir.getFoto());
//Вводим номер урока
TextView lessonnumber = (TextView) v.findViewById(R.id.lessonnumber);
lessonnumber.setText(dir.getLessonnumber());
//Вводим название урока
TextView lessonname = (TextView) v.findViewById(R.id.lessonname);
lessonname.setText(dir.getLessonname());
v.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {
Intent firstIntent = new Intent(yourclass.this, firstitem.class);
startActivity(firstIntent);
} });
// Возвращаем
return v;
}