android - 带图片的自定义 ListView。我无法点击某个项目并将我转至另一个 Activity

标签 android listview

点击该项目后,没有任何反应。提示我哪里没有正确实现方法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;
    }

}

最佳答案

使用此代码单击列表项并转到下一个扩展..

 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;

关于android - 带图片的自定义 ListView。我无法点击某个项目并将我转至另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24134907/

相关文章:

android - Android 移动框架 odoo v1 的 ListView 出现故障

android - 获取点击的项目及其在 RecyclerView 中的位置

c++ - ListView 控件忽略扩展样式

android - 错误 : "app_name" is not translated in af

android - 如何检测用户点击了插页式广告?

android - 如何创建自定义子菜单,如 Google Chrome 应用程序

android - 在 ListView 顶部包含 float 操作按钮

c# - 为什么 View 设置为 Details 的 C# ListView 控件仍然为空?

java - 如何将 AndroidAnnotations SharedPref 与 PreferenceActivity 一起使用?

android - XmlPullParserException 二进制 XML 文件行 #17<vector> 标签需要 viewportWidth > 0