gpt4 book ai didi

android - ListView OnClickListener 在 Fragment 中不起作用

转载 作者:行者123 更新时间:2023-11-30 02:42:12 25 4
gpt4 key购买 nike

我正在尝试为我的 fragment 中的 ListView 设置一个监听器。我已经尝试了几种方法来做到这一点,但仍然无法正常工作。我无法创建另一个 View focusable="false" 因为 ListView 上方有一个 EditText。每次我单击该项目时,它都会调用我创建的 JSONAdapter 中的 getView。这是我的 XML:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>

<EditText
android:id="@+id/searchquery"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="Search"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/list">

</ListView>

</LinearLayout>

这是我的 fragment :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

mJSONAdapter = new JSONAdapter(getActivity(), inflater);

View v = inflater.inflate(R.layout.header, container, false);
return v;
}
@Override
public void onViewCreated(View view,Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

ListView lv = (ListView) getView().findViewById(android.R.id.list);
// Set the ListView to use the ArrayAdapter
lv.setAdapter(mJSONAdapter);//isi datany pk json adapter
lv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(),Integer.toString(position),Toast.LENGTH_SHORT).show();
JSONObject jsonObject = (JSONObject) mJSONAdapter.getItem(position);//ambil item yg diclick
String raceid = jsonObject.optString("id","");//ambil cover id yg diklik, argumen kedua itu klo null defaultny apa

// create an Intent to take you over to a new DetailActivity
Intent detailIntent = new Intent(getActivity(), DetailActivity.class);

// pack away the data about the cover
// into your Intent before you head out
detailIntent.putExtra("raceid", raceid);
// start the next Activity using your prepared Intent
startActivity(detailIntent);
}

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

}
});

new RetrieveFeedTask().execute();

}

最佳答案

你可以试试这段代码。

lv.setOnItemClickListener(new OnItemClickListener() 
{
@Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{

// do your work
}
});

关于android - ListView OnClickListener 在 Fragment 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25607184/

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