gpt4 book ai didi

java - setOnItemClickListener 不起作用

转载 作者:行者123 更新时间:2023-12-01 09:13:45 25 4
gpt4 key购买 nike

我的Listview应用程序从自定义适配器ListAdapter.class获取itemview的数据和背景颜色。我还需要在下面的textview中设置当前选定的列表项值listview,但是MAinActivity中的setOnItemClickListener没有执行。请帮忙。

这是我的 ListView 应用程序:

Layout image

MainActivity.java

public class MainActivity extends Activity {
private static ListAdapterclass adapter;
ListView lv;
TextView tv2;
private final String android_versions[]={
"Donut",
"Eclair",
"Froyo",
"Gingerbread",
"Honeycomb",
"Ice Cream Sandwich",
"Jelly Bean",
"KitKat",
"Lollipop",
"Marshmallow"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
lv = (ListView) findViewById(R.id.listView1);
tv2 = (TextView) findViewById(R.id.selected);

adapter = new ListAdapterclass(getApplicationContext(), android_versions);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(), "hiiiiiiiii", Toast.LENGTH_SHORT).show();
System.out.println("********************** INSIDE ONITEMCLICKLISTNER IN MAIN ACTIVITY ******************");
String ver_name = (lv.getItemAtPosition(i)).toString();


tv2 = (TextView) findViewById(R.id.selected);
tv2.setText(ver_name);
}
});
}
}

ListAdapter.class

public class ListAdapterclass extends ArrayAdapter implements AdapterView.OnItemClickListener{

private String android_versionNames[];
Context mContext;
public int row_index=-1;

@Override
public void onItemClick(AdapterView<?> adapterView, View v, int i, long l) {
int position=(Integer)v.getTag();
String ver_name=getItem(position).toString();
}

private static class ViewHolder{
TextView tv;
LinearLayout LL;
TextView tv2;
}



public ListAdapterclass(Context context,String android_versionnames[]) {
super(context, R.layout.list_item,android_versionnames);
this.android_versionNames=android_versionnames;
this.mContext=context;

System.out.println(" ???????????????????????? Inside dataadapter,Android names : ?????????????????????????????\n ");
for(int i=0;i<android_versionnames.length;i++){
System.out.println("\n"+android_versionnames[i]);
}

}
private int lastPosition=-1;

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

String ver_name=getItem(position).toString();

final ViewHolder viewHolder;
final View result;

if(convertView==null){
viewHolder=new ViewHolder();
LayoutInflater inflater=LayoutInflater.from(getContext());
convertView=inflater.inflate(R.layout.list_item,parent,false);
viewHolder.tv=(TextView)convertView.findViewById(R.id.label);
viewHolder.LL=(LinearLayout) convertView.findViewById(R.id.linearLayout_1);
viewHolder.tv2=(TextView)convertView.findViewById(R.id.selected);
result=convertView;
convertView.setTag(viewHolder);
}else{
viewHolder=(ViewHolder) convertView.getTag();
result=convertView;
}
lastPosition=position;

viewHolder.tv.setText(ver_name);

viewHolder.LL.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
row_index=position;
notifyDataSetChanged();
}
});
if(row_index==position){
viewHolder.LL.setBackgroundColor(Color.parseColor("#409de1"));
viewHolder.tv.setTextColor(Color.parseColor("#ffffff"));
}
else
{
viewHolder.LL.setBackgroundColor(Color.parseColor("#ffffff"));
viewHolder.tv.setTextColor(Color.parseColor("#000000"));

}
return convertView;
}
}

ActivityMain.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.cybraum.test.listviewcolorchange.MainActivity"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:clickable="true"
android:layout_weight="1"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView1"
>
</ListView>


</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".2"
android:id="@+id/linearLayout_2"
android:orientation="horizontal"

>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selected : "
android:textStyle="bold"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="text"
android:id="@+id/selected"
android:layout_gravity="center"/>

</LinearLayout>

</LinearLayout>

listitem.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/linearLayout_1"
android:padding="10dp">
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"
android:textColor="#000000"
android:gravity="center">
</TextView>
</LinearLayout>

问题是什么?

最佳答案

从适配器中删除 viewHolder.LL.setOnClickListener 监听器并在您的适配器中添加一个方法来更新 row_index:

public void changeIndex(int rowIndex){
this.row_index = rowIndex;
notifyDataSetChanged();
}

从 onItemClickListener 事件调用此方法:

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
adapter.changeIndex(i);//This will give you the same result of viewHolder.LL.setOnClickListener as you are doing
//Do whatever you are doing previously
}
});

关于java - setOnItemClickListener 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40735357/

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