Reputation: 25
I am having some difficulties in managing onItemClickListener
in my Fragment view. When I press on items in list, nothing happens. Please check my code and let me know if I am doing it right or is there anything wrong with my code.
public class People extends BaseFragment implements OnClickListener {
static String name;
ListView listview;
TextView et;
static public List<SuccessStoreyItem> success_list3 = new ArrayList<SuccessStoreyItem>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view= inflater.inflate(R.layout.home,null);
ParserResult pr = new ParserResult();
Button b=(Button) view.findViewById(R.id.home1);
listview = (ListView)view.findViewById(R.id.lv_home);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "ullu ka patha", Toast.LENGTH_LONG).show();
/*Profile frg3 = new Profile();
Bundle b3 = new Bundle();
String username = success_list3.get(position).getStorey_id();
b3.putString("username", username);
frg3.setArguments(b3);
((MainActivity)getActivity()).launchNewFragment(frg3, R.id.tab2);
Toast.makeText(getActivity(), "Name :"+name ,1).show();*/
}
});
et=(TextView) view.findViewById(R.id.editText1);
b.setOnClickListener(this);
if(ParserResult.success_list2.size() != 0){
success_list3 = ParserResult.success_list2;
}
listview.setAdapter(new Adapters(success_list3 ,MainActivity.mainactivity_context));
ParserResult.success_list2 = new ArrayList<SuccessStoreyItem>();
return view;
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
setTitle(R.string.Home);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
name=et.getText().toString();
Bundle bundle = new Bundle();
bundle.putString("Name", name);
Toast.makeText(getActivity(), "Name :"+name ,1).show();
//((MainActivity)getActivity()).launchNewFragment(new Home2(),R.id.tab1);
}
}
UPDATES: List view structure added
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/view_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/view_tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/view_imageview"
android:layout_alignLeft="@+id/view_tv_age"
android:layout_marginBottom="5sp"
android:text="TextView" />
<TextView
android:id="@+id/view_tv_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/view_tv_username"
android:layout_marginLeft="26dp"
android:layout_toRightOf="@+id/view_imageview"
android:text="TextView" />
<ImageButton
android:id="@+id/ib_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="29dp"
android:layout_marginTop="18dp"
android:background="#fff"
android:src="@drawable/chevron" />
</RelativeLayout>
FRAGMENT LAYOUT
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="10sp" >
<TextView
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="sp"
android:gravity="center"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="New Machtes"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/home1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/iconnew"
android:textColor="#ffffff"
android:text="16" />
</RelativeLayout>
<ListView
android:id="@+id/lv_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/relativeLayout1" >
</ListView>
</RelativeLayout>
Upvotes: 1
Views: 320
Reputation: 10471
Add android:descendantFocusability="blocksDescendants"
to your <RelativeLayout..
in the ListView item layout.
Looking into your listview item layout, I see you have an ImageButton
. It happens that ImageButton
steals the focus from the view group (the list row) it is in. This means that when you click the list row, it is always the ImageButton
that captures the event.
By adding android:descendantFocusability="blocksDescendants"
we block the list row descendants from stealing the focusability.
You can read more about it here.
Upvotes: 3
Reputation: 29
i think your listview.setadapter() line and the data associated with it, should come before you set onclickitemlistener() to that listview..
Upvotes: 0