gpt4 book ai didi

java - 如何设置 onItemClickListener

转载 作者:行者123 更新时间:2023-12-02 05:36:44 28 4
gpt4 key购买 nike

我在 Android Studio 中设置了一个 Listview,但需要帮助编写 OnItemClickListner。

我已经尝试过该代码,但似乎不起作用。

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.list_view);

ArrayList<Object> list = new ArrayList<>();
list.add(new LTCItem("30.06 Sign Violations","Submit A Complaint To Texas Attorney General",R.drawable.gavel));
list.add(new LTCItem("U.S. & Texas LawShield","Legal Defense For Self Defense",R.drawable.lawshield));

listView.setAdapter(new LTCAdapter(this, list));

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

}
});

}
}

下面是我的 list_view 文件。按照建议,文件中的哪个位置会阻止 DescendantFocusability?我把它放在listView下面吗?抱歉,我正在学习。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alwaysDrawnWithCache="true"
android:background="#000000"
android:padding="8dp">

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent" />

<ImageView
android:id="@+id/itemListViewImgIcon"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="@+id/itemListViewImgIcon"
android:src="@mipmap/ic_launcher" />

<TextView
android:id="@+id/itemListViewTxtTopicName"
android:layout_width="match_parent"
android:layout_height="wrap_content"

<TextView
android:id="@+id/itemListViewTxtTopicSubtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/itemListViewTxtTopicName"

</RelativeLayout>

好的,我添加了适配器代码,它是一个 Java 类项目。我在哪里添加代码?

public class LTCAdapter extends BaseAdapter {
ArrayList<Object> list;
private static final int LTC_Item = 0;
private static final int HEADER = 1;
private LayoutInflater inflater;


public LTCAdapter(Context context, ArrayList<Object> list) {
this.list = list;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getItemViewType(int position) {
if (list.get(position) instanceof LTCItem) {
return LTC_Item;
} else {
return HEADER;
}
}

@Override
public int getViewTypeCount() {
return 2;
}

@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int i) {
return list.get(i);
}

@Override
public long getItemId(int i) {
return 1;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
switch (getItemViewType(i)) {
case LTC_Item:
view = inflater.inflate(R.layout.item_list_view, null);
break;
case HEADER:
view = inflater.inflate(R.layout.item_listview_header, null);
break;
}
}

switch (getItemViewType(i)) {
case LTC_Item:
ImageView image = (ImageView) view.findViewById(R.id.itemListViewImgIcon);
TextView name = (TextView) view.findViewById(R.id.itemListViewTxtTopicName);
TextView subtitle = (TextView) view.findViewById(R.id.itemListViewTxtTopicSubtitle);

image.setImageResource(((LTCItem) list.get(i)).getImage());
name.setText(((LTCItem) list.get(i)).getName());
subtitle.setText(((LTCItem) list.get(i)).getSubtitle());
break;
case HEADER:
TextView title = (TextView) view.findViewById(R.id.itemListViewHeader);
title.setText(((String) list.get(i)));
break;
}
return view;
}
}

最佳答案

您的 ListView 元素没有 ID,您应该在 XML< 中向其添加 android:id="@+id/list_view"/strong> 文件。

这是一个例子:

<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />

关于java - 如何设置 onItemClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56160023/

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