Search did not display result properly - java

I have trouble getting data from an array then search it in my search box. It only shows one result which is the first item.
I think this is the code being called:
ViewOrders.this.orderArrayAdapter.getFilter().filter(s);
Other than that is there other option to get a search function?
Here is my code:
Class A
private OrdersList orderArrayAdapter;
private void setupAdapterOrdersList() {
orderArrayAdapter = new OrdersList(getApplicationContext(), R.layout.row_item_view_orders, ordersArray);
if (orderListView != null) {
orderListView.setAdapter(orderArrayAdapter);
}
}
private void searchFromListView() {
etSearch = findViewById(R.id.search_order);
/**
* Enabling Search Filter
* */
etSearch.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// When user changed the Text
ViewOrders.this.orderArrayAdapter.getFilter().filter(s);
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
Class B
package com.example.android.ontrack.adapters;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.example.android.ontrack.R;
import com.example.android.ontrack.models.Order;
public class OrdersList extends ArrayAdapter<Order> {
Context mContext;
int mLayoutResourceId;
Order mData[] = null;
public OrdersList(Context context, int resource, Order[] data) {
super(context, resource, data);
this.mContext = context;
this.mLayoutResourceId = resource;
this.mData = data;
}
#Override
public Order getItem(int position) {
return super.getItem(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
LayoutInflater inflater = LayoutInflater.from(mContext);
row = inflater.inflate(mLayoutResourceId, parent, false);
TextView nameOfSchool = row.findViewById(R.id.school_name_orders);
TextView nameOfAgent = row.findViewById(R.id.agent_name);
TextView orderId = row.findViewById(R.id.order_id);
TextView netRevenue = row.findViewById(R.id.net_revenue);
TextView orderQuantity = row.findViewById(R.id.total_quantity);
TextView date = row.findViewById(R.id.date);
Order orders = mData[position];
nameOfSchool.setText(orders.nameOfSchool);
nameOfAgent.setText(String.valueOf(orders.nameOfAgent));
orderId.setText(String.valueOf(orders.orderId));
netRevenue.setText(String.valueOf(orders.netRevenue));
orderQuantity.setText(String.valueOf(orders.totalQuantity));
date.setText(String.valueOf(orders.date));
return row;
}
}

Related

How to change GroupView background color on Expand in ExpandableListView?

I Have problem to change background color of GroupView on position..
According to My Code
when i press 1nd number GroupView then the color change of 1st number GroupView.
AND
when i press 2nd number GroupView then 1st number GroupView is color is Changed.
I Want to change GroupView Color on Postion Expand and Collapse.
ExpandableListViewAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.NumberPicker;
import android.widget.TextView;
import com.acase.clouds.cloudstailor.Models.ExpandableModel;
import com.acase.clouds.cloudstailor.R;
import java.util.List;
public class ExpandableListViewAdapter extends BaseExpandableListAdapter {
private Context context;
private LayoutInflater inflater;
private List<ExpandableModel> listDataGroup;
public ExpandableListViewAdapter(Context context, List<ExpandableModel> listDataGroup) {
this.context = context;
this.listDataGroup = listDataGroup;
this.inflater = LayoutInflater.from(context);
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return listDataGroup.get(groupPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return 1;
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ExpandableModel model = listDataGroup.get(groupPosition);
if (convertView == null) {
convertView = inflater.inflate(model.getLayoutId(), null);
}
switch (model.getType()) {
case AGE:
setNumberPicker(convertView, model);
break;
case STATE:
setNumberPicker(convertView, model);
break;
}
return convertView;
}
private void setNumberPicker(View convertView, final ExpandableModel model) {
NumberPicker numberPicker = convertView.findViewById(R.id.numberPicker);
numberPicker.setMinValue(0);
numberPicker.setMaxValue(99);
if (!model.getValue().isEmpty()) {
numberPicker.setValue(Integer.parseInt(model.getValue()));
}
// to change formate of number in numberpicker
numberPicker.setFormatter(new NumberPicker.Formatter() {
#Override
public String format(int i) {
return String.format("%02d", i);
}
});
numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker numberPicker, int i, int i1) {
model.setValue(i1 + "");
}
});
}
#Override
public int getChildrenCount(int groupPosition) {
return 1;
}
#Override
public Object getGroup(int groupPosition) {
return this.listDataGroup.get(groupPosition);
}
#Override
public int getGroupCount() {
return this.listDataGroup.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
// String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_row_group, null);
}
ExpandableModel model = listDataGroup.get(groupPosition);
TextView tv_title = convertView.findViewById(R.id.tv_title);
TextView tv_value = convertView.findViewById(R.id.tv_value);
tv_title.setText(model.getName());
tv_value.setText(model.getValue());
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
EditProfileActivity
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.acase.clouds.cloudstailor.Adapter.ExpandableListViewAdapter;
import com.acase.clouds.cloudstailor.Models.ExpandableModel;
import com.bumptech.glide.Glide;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class EditProfileActivity extends AppCompatActivity {
Toolbar toolbar;
ImageView profileimage;
TextView changephototext, textView;
RelativeLayout item;
private static final int PICK_IMAGE_REQUEST = 1;
public Uri path;
private ExpandableListView expandableListView;
private ExpandableListViewAdapter expandableListViewAdapter;
List<ExpandableModel> list = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_profile);
toolbar = findViewById(R.id.tool).findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Edit Profile");
profileimage = findViewById(R.id.Profile_Img);
profileimage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
fileChooser();
}
});
changephototext = findViewById(R.id.Change_Photo_Button);
changephototext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
fileChooser();
}
});
// initializing the views
initViews();
// initializing the listeners
initListeners();
// initializing the objects
initObjects();
}
private void initViews() {
expandableListView = findViewById(R.id.expandableListView);
}
private void initListeners() {
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
ExpandableModel model = list.get(groupPosition);
Toast.makeText(EditProfileActivity.this, model.getName() + " clicked at " + childPosition, Toast.LENGTH_SHORT).show();
return false;
}
});
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
ExpandableModel model = list.get(groupPosition);
Toast.makeText(EditProfileActivity.this, model.getName() + " expanded", Toast.LENGTH_SHORT).show();
item = findViewById(R.id.Group);
item.setBackgroundResource(R.drawable.selectedlistback);
textView = findViewById(R.id.tv_title);
textView.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
}
});
// ExpandableListView Group collapsed listener
expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
ExpandableModel model = list.get(groupPosition);
Toast.makeText(EditProfileActivity.this, model.getName() + " collapsed", Toast.LENGTH_SHORT).show();
item = findViewById(R.id.Group);
item.setBackgroundResource(R.drawable.listback);
textView = findViewById(R.id.tv_title);
textView.setTextColor(getResources().getColor(R.color.colorAccent));
}
});
}
private void initObjects() {
ExpandableModel age = new ExpandableModel("AGE", "", ExpandableModel.ExpandableType.AGE, R.layout.list_row_child);
list.add(age);
ExpandableModel state = new ExpandableModel("STATE", "", ExpandableModel.ExpandableType.STATE, R.layout.list_row_child);
list.add(state);
expandableListViewAdapter = new ExpandableListViewAdapter(this, list);
expandableListView.setAdapter(expandableListViewAdapter);
}
private void fileChooser()
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICK_IMAGE_REQUEST);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data.getData() != null)
{
path = data.getData();
Glide.with(this).load(path).into(profileimage);
path = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),path);
profileimage.setImageBitmap(bitmap);
}catch (IOException e){
e.printStackTrace();
}
}
}
}
Try add the following code inside getGroupView of your adapter:
if(isExpanded) {
convertView.setBackgroundResource(R.drawable.selectedlistback);
tv_title.setTextColor(context.getResources().getColor(R.color.colorPrimaryDark));
}else{
convertView.setBackgroundResource(R.drawable.listback);
tv_title.setTextColor(context.getResources().getColor(R.color.colorAccent));
}
and remove the listeners. Hope that helps!

How to getCount multi private int

i'm new android java programming. i'm using ListView multiple image. but i have a issue about public int getCount(). i want to know how to do multiple int image not error. i try to find information. can't find the information i want to know
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class Main2Adapter extends BaseAdapter {
private Context context;
private int[] intspic;
private int[] rawspic;
private String[] StringsName;
private String[] StringsDetail;
public Main2Adapter(Context context,
int[] intspic,
int[] rawspic,
String[] stringsName,
String[] stringsDetail) {
this.context = context;
this.intspic = intspic;
this.rawspic = rawspic;
StringsName = stringsName;
StringsDetail = stringsDetail;
}
#Override
public int i getCount() {
return rawspic,intspic.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view2 = layoutInflater.inflate(R.layout.listview_layout_ii, viewGroup, false);
ImageView imageViews = (ImageView) view2.findViewById(R.id.imageViewii);
ImageView rawii = (ImageView) view2.findViewById(R.id.imageRawii);
TextView Names = view2.findViewById(R.id.textView11);
TextView Details = view2.findViewById(R.id.textView22);
rawii.setImageResource(rawspic[i]);
imageViews.setImageResource(intspic[i]);
Names.setText(StringsName[i]);
Details.setText(StringsDetail[i]);
return view2;
}
}
I guess all of these arrays have the same length:
private int[] intspic;
private int[] rawspic;
private String[] StringsName;
private String[] StringsDetail;
so getCount() may return the length of one of them:
#Override
public int getCount() {
return rawspic.length;
}
Edit rereturn intspic.length; to rereturn intspic.length;

recyclerview list not showing data passed through arraylist

My code was worked perfectly just before i added header image, after that listview not showing the given datas.
These are the main pages.
Can anyone help please.Any help will be appreciated.
MyRecyclerViewAdapter.java
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class MyRecyclerViewAdapter extends
RecyclerView.Adapter<MyRecyclerViewAdapter.DataObjectHolder> {
private static String LOG_TAG = "MyRecyclerViewAdapter";
private ArrayList<DataObject> mDataset;
private static MyClickListener myClickListener;
Context context;
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
String[] data;
public MyRecyclerViewAdapter(String[] data) {
this.data = data;
}
public static class DataObjectHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView desc;
ImageView image,head_img;
public DataObjectHolder(View itemView) {
super(itemView);
desc = (TextView) itemView.findViewById(R.id.description);
image = (ImageView) itemView.findViewById(R.id.image);
head_img=(ImageView) itemView.findViewById(R.id.header_img);
Log.i(LOG_TAG, "Adding Listener");
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
myClickListener.onItemClick(getPosition(), v);
}
}
public void setOnItemClickListener(MyClickListener myClickListener) {
this.myClickListener = myClickListener;
}
public MyRecyclerViewAdapter(ArrayList<DataObject> myDataset) {
this.mDataset = myDataset;
this.context=context;
}
#Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
if(viewType==TYPE_ITEM) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview, parent, false);
//DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return new DataObjectHolder(view);
}
else if (viewType==TYPE_HEADER){
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false);
//DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return new DataObjectHolder(view);
}
throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}
#Override
public void onBindViewHolder(DataObjectHolder holder, int position) {
if(holder instanceof VHItem) {
holder.desc.setText(mDataset.get(position ).getDescription());
// holder.image.setText(mDataset.get(position).getImage_url());
Picasso.with(holder.image.getContext()).load(mDataset.get(position ).getImage_url()).into(holder.image);
}
else if(holder instanceof VHHeader){
holder.head_img.setImageResource(R.drawable.cam);
}
}
public void addItem(DataObject dataObj, int index) {
mDataset.add(dataObj);
notifyItemInserted(index);
}
public void deleteItem(int index) {
mDataset.remove(index);
notifyItemRemoved(index);
}
#Override
public int getItemCount() {
return (null != mDataset ? mDataset.size() + 1 : 0);
}
public interface MyClickListener {
public void onItemClick(int position, View v);
}
private class VHItem extends DataObjectHolder {
public VHItem(View itemView) {
super(itemView);
}
}
private class VHHeader extends DataObjectHolder {
public VHHeader(View itemView) {
super(itemView);
}
}
#Override
public int getItemViewType(int position) {
if (isPositionHeader(position))
return TYPE_HEADER;
return TYPE_ITEM;
}
private boolean isPositionHeader(int position) {
return position == 0;
}
private String getItem(int position) {
return data[position - 1];
}
}
Profile2Fragment.java
import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
public class Profile2Fragment extends Fragment {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private static String LOG_TAG = "RecyclerViewActivity";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.recycler_view, container, false);
mRecyclerView = (RecyclerView) rootview.findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new MyRecyclerViewAdapter(getDataSet());
mRecyclerView.setAdapter(mAdapter);
RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL);
mRecyclerView.addItemDecoration(itemDecoration);
return rootview;
}
private ArrayList<DataObject> getDataSet() {
ArrayList<DataObject> results = new ArrayList<DataObject>();
for (int index = 0; index < 1; index++) {
DataObject obj = new DataObject("Lorem Ipsum is simply\n dummy text of the", "https://mediastream.jumeirah.com/webimage/heroactual//globalassets/global/hotels-and-resorts/dubai/madinat-jumeirah/restaurants/pierchic/madinat-jumeirah-pierchic-inside-restaurant-02-hero.jpg");
results.add(obj);
obj = new DataObject("Lorem Ipsum is simply\n dummy text of the", "https://photos.smugmug.com/Portfolio/i-HBk894k/0/db7f0344/L/The%20Black%20Couches-L.jpg");
results.add(obj);
obj = new DataObject("Lorem Ipsum is simply\n dummy text of the", "http://thesmartlocal.com/images/easyblog_images/82/Pollen.jpg");
results.add(obj);
}
return results;
}
}
DataObject.java
public class DataObject {
private String description;
private String image_url;
DataObject (String text1, String text2){
description = text1;
image_url = text2;
}
public String getImage_url() {
return image_url;
}
public void setImage_url(String image_url) {
this.image_url = image_url;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
try to change return statement
`return new DataObjectHolder(view);
to your specific Class like return
new VHHeader(view);
and
return new VHItem(view);
in onCreateViewHolder..

Android ExpandableList onChildClickListener in Fragment does not work

I have code about onChildClickListener like the snippet code below
OnChildClickListener occl = new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "Jalan", Toast.LENGTH_LONG).show();
return true;
}
};
lv2.setOnChildClickListener(occl);
But the Toast does'nt appear, why it can be occurring?
Depending 2 Files,
(Sorry for long post, i want it to be clear)
The below snipped Adapter code:
package com.em.crayonpediamoodlesiswa;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.content.Entity;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;
ArrayList<Entity> objects;
View rootView;
ExpandableListAdapterChild la;
ExpandableListView lv;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
public ExpandableListAdapter(Context context, List<String> expandableListTitle,
HashMap<String, List<String>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
#Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}
#Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
#Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition, expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item, null);
}
TextView expandedListTextView = (TextView) convertView
.findViewById(R.id.expandedListItem);
expandedListTextView.setText(expandedListText);
/*ExpandableListView elv = new ExpandableListView(context);
elv.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT, AbsListView.LayoutParams.WRAP_CONTENT));
elv.setAdapter(new ExpandableListAdapter(context,null,null));
((ViewGroup)convertView).addView(elv);*/
return convertView;
}
#Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.size();
}
#Override
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}
#Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
#Override
public long getGroupId(int listPosition) {
return listPosition;
}
#Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_group, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.listTitle);
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(listTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
}
The below snippet Activity Code using Fragment:
package com.em.crayonpediamoodlesiswa;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;
public class MyCourses extends Fragment{
private static final String url = "jdbc:mysql://192.168.43.166:3306/moodle";
private static final String user = "emil";
private static final String pass = "emilhamep";
private int amount;
ExpandableListAdapter la;
ExpandableListView lv2;
List<String> expandableListTitle;
HashMap<String, List<String>> expandableListDetail;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.my_courses, container, false);
lv2=(ExpandableListView)rootView.findViewById(R.id.expandableListViewChild);
prepareListDataMyCourse();
la=new ExpandableListAdapter(getActivity(), expandableListTitle, expandableListDetail);
lv2.setAdapter(la);
//Menentukan aksi ketika diklik untuk setiap hasil fetch array
try{
//Toast.makeText(getActivity(), "Jalan-jalan", Toast.LENGTH_LONG).show();
int user_id = 5;
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(url,user,pass);
Statement st = con.createStatement();
ResultSet rschild=st.executeQuery("SELECT co.fullname FROM course co JOIN course_display codi WHERE codi.userid=" + user_id + " AND co.id= codi.course");
final String[] a = null;
int counter=0;
while(rschild.next()) {
a[counter]=rschild.getString(1);
//my_course.add(rschild.getString(1));
counter++;
}
/* lv2.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
int childPosition, long id) {
Toast.makeText(getActivity(), "Jalan", Toast.LENGTH_LONG).show();
for(int b=0;b<a.length;b++){
if(childPosition == b){
Toast.makeText(getActivity(), a[b], Toast.LENGTH_LONG).show();
}
Intent intent = new Intent(getActivity(),Classroom.class);
String logged_user=a[b].toString();
intent.putExtra("username", logged_user);
startActivity(intent);
}
return false;
}
});*/
OnChildClickListener occl = new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "Jalan", Toast.LENGTH_LONG).show();
return true;
}
};
lv2.setOnChildClickListener(occl);
}
catch(Exception e){
e.printStackTrace();
Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_LONG);
}
return rootView;
}
private void prepareListDataMyCourse(){
expandableListTitle = new ArrayList<String>();
expandableListDetail = new HashMap<String, List<String>>();
try{
int user_id = 5;
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(url,user,pass);
Statement st = con.createStatement();
//Query header expandable
List<String> my_course = new ArrayList<String>();
expandableListTitle.add("My Course");
//Query dapet list my course
ResultSet rschild=st.executeQuery("SELECT co.fullname FROM course co JOIN course_display codi WHERE codi.userid=" + user_id + " AND co.id= codi.course");
while(rschild.next()) {
my_course.add(rschild.getString(1));
}
expandableListDetail.put(expandableListTitle.get(0), my_course);
}catch(Exception e){
e.printStackTrace();
Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_LONG);
}
}
}
check your adapter isChildSelectable returns true or false.return it true
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
Apply this one
OnChildClickListener occl = new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "Jalan", Toast.LENGTH_LONG).show();
return false;
}
};
lv2.setOnChildClickListener(occl);
if this doesn't work then refer this tutorial http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/

SetOnItem Click Listener event is not getting effected?

this is my code to display listview in customized format .but my requirement is when ever i clicked on any of the item of listview its further details need to be displayed but when am clicking on it ....its not at all getting effected i mean no setonitemclicklictener event is performed ....
i request please have a look on my code which is given above ....thanks to all
package com.hands;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Passengers extends Activity implements OnClickListener{
ListView lview3;
Button btn;
int textlength = 0;
ArrayList<String> text_sort = new ArrayList<String>();
ArrayList<Integer> image_sort = new ArrayList<Integer>();
ArrayList<String> text_sort2 = new ArrayList<String>();
EditText edittext;
private static String month[] = {"kareem","Saleem","Imran","Anwar","Shahid",
"Raheem","Afzal","Nazeer","Ahmed"};
private static String desc[] = {"ssagi123,Indian","ssagi1234,Indian","ssagi1235,Indian",
"ssagi1236,Indian","ssagi1237,Indian","ssagi1238,Indian","ssagi1239,Indian",
"ssagi12310,Indian","ssagi123411,Indian","Month - 10"};
int[] image = { R.drawable.user2, R.drawable.user2, R.drawable.user2,
R.drawable.user2, R.drawable.user2, R.drawable.user2, R.drawable.user2,
R.drawable.user2, R.drawable.user2, R.drawable.user2 };
/* private static String bstatus[] = {"no status","no status","no status","no status","no status",
"no status","no status","no status","no status"};
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.passengers);
System.out.println("****1");
btn=(Button)findViewById(R.id.psnbutt1);
btn.setOnClickListener(this);
edittext = (EditText) findViewById(R.id.search_mycontact);
lview3 = (ListView) findViewById(R.id.listView3);
System.out.println("****2");
lview3.setAdapter(new ListViewCustomAdapter(image, month, desc));
edittext.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after)
{
}
public void onTextChanged(CharSequence s, int start,
int before, int count)
{
textlength = edittext.getText().length();
text_sort.clear();
image_sort.clear();
text_sort2.clear();
for (int i = 0; i < month.length; i++)
{
if (textlength <= month[i].length())
{
if (edittext.getText().toString().
equalsIgnoreCase((String) month[i].subSequence(0, textlength)))
{
text_sort.add(month[i]);
image_sort.add(image[i]);
text_sort2.add(desc[i]);
}
}
}
System.out.println("****3");
lview3.setAdapter(new ListViewCustomAdapter(image_sort, text_sort, text_sort2));
}
});
System.out.println("****4");
lview3.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println("****5");
// Object listItem = lview3.getItemAtPosition(position);
//String keyword=listItem.toString();
// Toast.makeText(getApplicationContext(), "you are selected"+keyword, Toast.LENGTH_LONG).show();
}
});
}
class ListViewCustomAdapter extends BaseAdapter
{
String[] title;
String[] description;
int[] number;
Activity context;
LayoutInflater inflater;
ListViewCustomAdapter()
{
}
ListViewCustomAdapter(int[] image, String[] month, String[] desc)
{
title = month;
description=desc;
number=image;
}
ListViewCustomAdapter(ArrayList<Integer> image,ArrayList<String> month, ArrayList<String> desc)
{
title = new String[month.size()];
description=new String[desc.size()];
number = new int[image.size()];
for(int i=0;i<month.size();i++)
{
title[i] = month.get(i);
description[i]=desc.get(i);
number[i] = image.get(i);
}
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return title.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row=convertView;
row = inflater.inflate(R.layout.listitem_row2, parent, false);
TextView textview = (TextView) row.findViewById(R.id.txtViewTitle);
TextView textview1 = (TextView) row.findViewById(R.id.txtViewDescription);
System.out.println("before list row statement");
//final ListView lview3=(ListView) row.findViewById(R.id.listView3);
// final ListView lview3=(ListView)findViewById(R.id.listView3);
System.out.println("after list row statement");
ImageView imageview = (ImageView) row.findViewById(R.id.imgViewLogo);
textview.setText(title[position]);
textview1.setText(description[position]);
imageview.setImageResource(number[position]);
/*lview3.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
Object listItem = lview3.getItemAtPosition(position);
String keyword=listItem.toString();
Toast.makeText(getApplicationContext(), "you are selected"+keyword, Toast.LENGTH_LONG).show();
}
});
*/
return (row);
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.psnbutt1:
Intent i = new Intent(this, NewhapploginActivity.class);
startActivity(i);
break;
}
}
}
You need to add
android:focusable="false"
android:focusableInTouchMode="false"
to all the View's of your row.xml.Because in Custom ListView as the View over it are having the focus so ListView is not able to get its focus working.

Categories