我试图将一个监听器放到我的listview中,这位于我的片段中。我尝试了几种方法,但仍然无法正常工作。我无法创建另一个视图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>
这是我的片段:
@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();
}
答案 0 :(得分:1)
您可以尝试使用此代码。
lv.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
// do your work
}
});
答案 1 :(得分:0)
看来我选择了错误的方法。它应该是OnItemClickListener(),而不是我选择OnItemSelectedListener()