onListItemClick不起作用。我试图存储列表视图中选择的值并将其传递给mainactivity。
如何获取所选项目位置并将其传递给主要活动?
请建议并感谢您的帮助。
这是我的代码:
public static class ListViewFragment extends Fragment {
public static final String ARG_PLANET_NUMBER = "planet_number";
//FragmentActivity listener;
private ListView myListView;
private EditText editsearch;
private CheckBox selectAll;
private ArrayList<String> SelectedFilter1;
private ArrayList<String> CheckedFitlers;
ArrayAdapter<String> adapter_list;
public ListViewFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
SelectedFilter1=getArguments().getStringArrayList("ArrayList");
adapter_list = new ArrayAdapter<String>(this.getActivity(),
android.R.layout.simple_list_item_multiple_choice,SelectedFilter1);
adapter_list.notifyDataSetChanged();
myListView.setAdapter(adapter_list);
myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
myListView.setTextFilterEnabled(true);
editsearch.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
adapter_list.getFilter().filter(text);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
});
selectAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
Log.i("CheckBOX","Is Checked:"+isChecked);
if(isChecked)
{
for ( int i=0; i< myListView.getCount(); i++ )
myListView.setItemChecked(i, true);
}else
{
for ( int i=0; i< myListView.getCount(); i++ )
myListView.setItemChecked(i, false);
}
}
}
);
}
public void onListItemClick(ListView l, View v, int position, long id) {
//showDetails(position);
Log.i("Item Clicked in the Listview","Item Clicked in the listview: "+position);
//Toast.makeText(getActivity(), "Item Clicked in the listview: "+position, 3000).show();
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_list_view, container, false);
myListView=(ListView) rootView.findViewById(R.id.listView_brand);
editsearch = (EditText) rootView.findViewById(R.id.search);
selectAll=(CheckBox) rootView.findViewById(R.id.checkBox1);
//myListView.setFocusable(false);
// myListView.setClickable(false);
selectAll.setFocusable(false);
// rootView.setFocusable(false)
return rootView;
}
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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="test.dsi.decathnavigation.Brand_list" >
<ListView
android:id="@+id/listView_brand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/search"
android:fastScrollAlwaysVisible="false"
android:fastScrollEnabled="false"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollingCache="true"
android:smoothScrollbar="false" >
</ListView>
<EditText
android:id="@+id/search"
android:layout_width="450dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/listView_brand"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="Type to search" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/search"
android:text="Select All" />
</RelativeLayout>
答案 0 :(得分:0)
onListItemClick无法在片段
中工作
因为您没有在ListViewFragment Fragment中实现OnItemClickListener
并覆盖onListItemClick
的{{1}}。
这样做:
OnItemClickListener
答案 1 :(得分:0)
您需要为OnItemClickListener
添加ListView
:
myListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//your code over here
}
});
希望这有帮助。
答案 2 :(得分:0)
我会尽力帮助使用我的例子:
在包含ListView的Activity中声明ListView:
public class RankingActivity extends Activity {
ListView bestPlayersList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ranking);
mContext=this;
bestPlayersList = (ListView) findViewById(R.id.bestPlayersListView);
}
bestPlayersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final CheckedTextView item = (CheckedTextView) view;
if (item.isChecked())
{
//..do some action..for example get item text: item.getText()
}
}
});
}
对我有用;)