I have to put a list of names queried from database into a list (not listview). I am able to do that by inflating a linearlayout with a custom view. I have a created a new class in which there is a method which inflates the layout with the custom view.
In that method I have implemented a onclicklistener, but it is not working.
Code:
public static void showPeopleInvitedList(Context context, View view, final LinearLayout layout) {
dbAdapter = new DatabaseHandler(context);
String[] ENTRIES = dbAdapter.getItems("user_friend_name");
for (int i = 0; i < 5; i++ ) {
LayoutInflater inflator = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflator.inflate(R.layout.people, layout, false);
view.setId(layout.getChildCount() + 1);
params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(params);
view.setOnClickListener(myClickListener);
layout.addView(view, 0, params);
}
myClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("click", "clicked");
layout.removeView(v);
}
};
}
But onclicklistener is not working. I am calling this method in the main activity. This method is inside a class which extends LinearLayout
Any suggestions?
You have not yet instantiated myClickListener, so you are setting the view’s
onClickListenertonull. Move its initialization to before yourforloop.}