gpt4 book ai didi

android - ListView setOnItemClickListener 不工作

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

我有一个项目 ListView ,每个项目都有一些 ImageView 和 TextView 。我在谷歌上搜索了很多解决方案,但没有一个对我有帮助。我是 android 开发的新手,我想我失去了一些明显的东西。我还假设也许我尝试了太多的解决方案,并且它们以某种方式相互阻塞。请帮帮我。这是我的代码:

public class MyActivity extends Activity {
private List<Lesson> lessons = new ArrayList<Lesson>();

private int loop_count = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.my_layout);

FillLessonList();
FillListView();
}
private void FillListView()
{
ArrayAdapter<Lesson> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.lessonsList);
list.setAdapter(adapter);

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i("debug", "item click: " + view.findViewById(R.id.lesson_text_lesson_name));

Toast.makeText(getApplicationContext(), "Your toast message.",
Toast.LENGTH_SHORT).show();

Intent intent = new Intent(view.getContext(), LessonActivity.class);
startActivity(intent);
}
});
}

private class MyListAdapter extends ArrayAdapter<Lesson>
{
MyListAdapter()
{
super(MyActivity.this, R.layout.lesson_item, lessons);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View lessonView = convertView;
if(lessonView == null)
{
lessonView = getLayoutInflater().inflate(R.layout.lesson_item, parent, false);
}

Lesson currentLesson = lessons.get(position);

//here I add all the needed info into the item.
//This part works fine and displays everything I need properly.

return lessonView;
}

每次点击真正记录的唯一内容是:

05-18 17:10:57.158 29101-29101/h2d.drawteam.howtodraw D/AbsListView: Touch down: touch mode = 0,mScrollY = 0,y = 122.7923,mFirstPosition = 0,mActivePointerId = 0,mDataChanged = false,adatper size = 15,this = android.widget.ListView{32614ed4 VFED.VC. .F...... 60,0-1020,1599 #7f0d0076 app:id/lessonsList}
05-18 17:10:57.258 29101-29101/h2d.drawteam.howtodraw D/AbsListView: CheckForTap:mFirstPosition = 0,mMotionPosition = 0,child = android.widget.RelativeLayout{3506a91f V.E..... ........ 0,0-960,225}
05-18 17:10:57.293 29101-29101/h2d.drawteam.howtodraw D/AbsListView: Touch up: touch mode = 1,mScrollY = 0,mLastY = -2147483648,mMotionPosition = 0,mFirstPosition = 0,mDataChanged = false,adatper size = 15,this = android.widget.ListView{32614ed4 VFED.VC. .F...... 60,0-1020,1599 #7f0d0076 app:id/lessonsList}

这是我的主要布局 xml 文件:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_clear"
android:orientation="vertical"
android:paddingTop="10dp"
android:paddingBottom="10dp">

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/container_button_back"
android:paddingRight="25dp"
android:paddingLeft="25dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:weightSum="1">

<Button
android:layout_width="64dp"
android:layout_height="31dp"
android:id="@+id/button_back"
android:background="@drawable/back_button_fit"
android:maxHeight="50dp"
android:maxWidth="80dp"
android:adjustViewBounds="true"
android:longClickable="false"
android:enabled="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:singleLine="false" />

</LinearLayout>

<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/separator_1" />

<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/container_list"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingBottom="5dp">

<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/lessonsList"
android:dividerHeight="0dp"
android:divider="@null"
android:descendantFocusability="blocksDescendants"/>
</LinearLayout>

这是我的列表项 xml 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:clickable="false"
android:paddingLeft="0dp"
android:paddingRight="10dp">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lesson_icon"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="@drawable/abc_btn_check_material"
android:minHeight="75dp"
android:minWidth="75dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"/>



<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/default_lesson_name"
android:id="@+id/lesson_text_lesson_name"
android:layout_marginTop="17dp"
android:layout_alignParentTop="true"
android:textSize="18sp"
android:maxLines="1"
android:layout_toLeftOf="@+id/lesson_rate"
android:layout_toRightOf="@+id/lesson_icon"
android:layout_toEndOf="@+id/lesson_icon"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/default_steps"
android:id="@+id/lesson_text_steps"
android:layout_below="@+id/lesson_text_lesson_name"
android:layout_toRightOf="@+id/lesson_icon"
android:layout_toEndOf="@+id/lesson_icon"
android:textSize="12sp"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"/>

<ImageButton
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/lesson_rate"
android:src="@drawable/rate1"
android:baselineAlignBottom="false"
android:background="#00000000"
android:layout_above="@+id/lesson_text_steps"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="false"
android:paddingRight="10dp"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:scaleType="centerInside"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"/>

<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/lesson_separator"
android:src="@drawable/separator"
android:background="#00000000"
android:adjustViewBounds="true"
android:layout_alignBottom="@+id/lesson_icon"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"/>

最佳答案

试试这个

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

Log.i("debug", "item click: " + view.findViewById(R.id.lesson_text_lesson_name));

Toast.makeText(getApplicationContext(), "Your toast message.",
Toast.LENGTH_SHORT).show();

Intent intent = new Intent(view.getContext(), LessonActivity.class);
startActivity(intent);

}
});

list.setOnItemClickListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.i("debug", "item click: " + view.findViewById(R.id.lesson_text_lesson_name));

Toast.makeText(getApplicationContext(), "Your toast message.",
Toast.LENGTH_SHORT).show();

Intent intent = new Intent(view.getContext(), LessonActivity.class);
startActivity(intent);

}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});

替代方法

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View lessonView = convertView;
if(lessonView == null)
{
lessonView = getLayoutInflater().inflate(R.layout.lesson_item, parent, false);
}

Lesson currentLesson = lessons.get(position);

//here I add all the needed info into the item.
//This part works fine and displays everything I need properly.

lessonView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(""testing123,"testing");
Toast.makeText(MyActivity.this, "Your toast message.",
Toast.LENGTH_SHORT).show();

Intent intent = new Intent(MyActivity.this, LessonActivity.class);
startActivity(intent);
}
});

return lessonView;
}

关于android - ListView setOnItemClickListener 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37302369/

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