我有一个应用程序将项目加载到FrameLayout内的ListView中。该部分工作正常但click
功能似乎不起作用,当我将鼠标悬停在他们没有可点击的悬停外观的项目上时,点击时什么都不做。
我已经用Google搜索过,并且在xml中提到了一些焦点和可点击的类型,但尝试添加它们似乎没有任何区别。
我使用的布局结构有问题吗?或者我可以添加一些东西以确保ListView元素是可点击的吗?
LansFragment
Map<String,String> myMap = new HashMap<>();
JSONArray lanArray = data; //json arrray, blank for example
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, container, false);
ArrayList<String> itemArray = new ArrayList<String>();
try {
JSONArray jsonMainArr = jsonObj.getJSONArray(lanArray);
for (int i = 0; i < jsonMainArr.length(); i++) {
JSONObject json_data = jsonMainArr.getJSONObject(i);
String name = json_data.getString("lanName");
String key = json_data.getString("lanKey");
itemArray.add(name);
myMap.put(name, key);
}
} catch (Exception e) {
// catch here
}
ArrayAdapter adapter = new ArrayAdapter<String>(view.getContext(), R.layout.activity_listview, itemArray);
ListView listView = (ListView) getActivity().findViewById(R.id.listview);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
String lan = l.getItemAtPosition(position).toString();
String key = myMap.get(lan);
// Not currently working due to no click registering
}
});
return view;
}
activity_main
<?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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="app.stats.LansFragment"
android:orientation="horizontal">
<FrameLayout
android:id="@+id/fragment_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listview" />
</FrameLayout>
</LinearLayout>
activity_listview
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>
答案 0 :(得分:2)
您致电片段内的ListView
ID来调用:
ListView listView = (ListView) getActivity().findViewById(R.id.listview);
但是在片段中你应该引用片段视图。它应该是:
ListView listView = (ListView) view.findViewById(R.id.listview);