gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:19:08 25 4
gpt4 key购买 nike

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

}

最佳答案

使用此代码点击 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;

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com