I want to create a custom list view in app...
but I don't understand why my code have problem to run it
when I run my app ...Shows Message as "app is stopped" ..
I'm confused..
Help me to solve this
thank you
java code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list=(ListView) findViewById(R.id.listView1);
Myadapter adapter=new Myadapter(getApplicationContext());
list.setAdapter(adapter);
}
public class Myadapter extends BaseAdapter
{
LayoutInflater myInflater;
public Myadapter(Context context) {
myInflater=LayoutInflater.from(context);
// TODO Auto-generated constructor stub
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return (4);
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder Holder;
if(convertView==null)
{
Holder=new ViewHolder();
convertView=myInflater.inflate(R.layout.item_layout, null);
Holder.star=(ImageButton) convertView.findViewById(R.id.imageButton_star);
Holder.vorod=(Button) convertView.findViewById(R.id.button_voro);
Holder.url=(TextView) convertView.findViewById(R.id.text_addres);
Holder.vorod=(Button)convertView.findViewById(R.id.imageButton_star);
Holder.rank=(ImageButton) convertView.findViewById(R.id.imageButton_rank);
Holder.speed=(ImageButton) convertView.findViewById(R.id.imageView_speed);
Holder.delete=(ImageButton) convertView.findViewById(R.id.imageButton_delete);
convertView.setTag(Holder);
}
else
Holder=(ViewHolder) convertView.getTag();
// TODO Auto-generated method stub
return convertView;
}
}
public class ViewHolder
{
ImageButton star;
Button vorod;
TextView url;
ImageButton rank;
ImageButton speed;
ImageButton detail;
ImageButton delete;
}
}
Xml code:(Item list view)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="30dp"
android:background="#drawable/image001"
android:orientation="vertical" >
<ImageButton
android:id="#+id/imageButton_star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:background="#drawable/round"
android:src="#drawable/star1"
/>
<Button
android:id="#+id/button_voro"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/text_addres"
android:background="#drawable/corner_round"
android:text="button" />
<TextView
android:id="#+id/text_addres"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="9dp"
android:layout_margin="30dp"
android:layout_centerInParent="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="30dp"
android:background="#drawable/olgo"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/imageView_speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="#drawable/speed" />
<ImageButton
android:id="#+id/imageButton_dataile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="#drawable/dataile" />
<ImageButton
android:id="#+id/imageButton_rank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="#drawable/ranke" />
<ImageButton
android:id="#+id/imageButton_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="#drawable/delete" />
</LinearLayout>
and main Xml: There is a listView
you have this in code:
Holder.star=(ImageButton) convertView.findViewById(R.id.imageButton_star);
but you have not a Button with id imageButton_star in your xml file.
You are inilizating Holder.vorod twice
Remove
Holder.vorod=(Button)convertView.findViewById(R.id.imageButton_star);
Related
I am new to android,I am currently making a listview with custom list items,I want to do an action on Listview's item click event,I have searched so many similar threads with no luck,Can anyone help me to fix this.my code is as below,
<?xml version="1.0" encoding="utf-8"?>
<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:background="#fff">
<TextView
android:id="#+id/tv_hdr"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:background="#color/orange"
android:gravity="center"
android:text="SELECT YOUR CITY"
android:textColor="#color/white"
android:textSize="22dp" />
<EditText
android:id="#+id/et_search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_hdr"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="#drawable/et_selector"
android:drawableLeft="#drawable/search"
android:hint="City Or Postal Code"
android:padding="10dp"
android:textSize="16dp" />
<TextView
android:id="#+id/tv_loc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/et_search"
android:background="#color/grey_light"
android:drawableLeft="#drawable/ic_loc"
android:gravity="center_vertical"
android:padding="10dp"
android:paddingLeft="5dp"
android:text=" My Location"
android:textColor="#color/black"
android:textSize="16dp"
android:textStyle="bold" />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scr_location"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/tv_loc"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_populcar_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:gravity="center_vertical"
android:padding="10dp"
android:paddingLeft="5dp"
android:text=" Popular Cities"
android:textColor="#color/orange"
android:textSize="16dp"
android:textStyle="bold" />
<ListView
android:id="#+id/lv_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_populcar_city" />
<TextView
android:id="#+id/tv_states"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/lv_city"
android:background="#color/white"
android:gravity="center_vertical"
android:padding="10dp"
android:paddingLeft="5dp"
android:text="States"
android:textColor="#color/orange"
android:textSize="16dp"
android:textStyle="bold" />
<ListView
android:id="#+id/lv_state"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_states" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
adapter
public class CityAdapter extends BaseAdapter {
private Context mContext;
private final ArrayList<City> city;
Integer selected_position = -1;
public CityAdapter(Context c, ArrayList<City> city) {
mContext = c;
this.city = city;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return city.size();
}
#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 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) { // if it's not recycled, initialize some attributes
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.raw_city, parent, false);
} else {
v = (View) convertView;
}
TextView tv_city = (TextView) v.findViewById(R.id.tv_city);
tv_city.setText(city.get(position).getCity());
return v;
}
}
activity
lv_city.setOnItemClickListener( new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Pref.setValue( SelectCity.this, Const.PREF_CITY_ID, cityList.get( position ).getCity_id() );
finish();//finishing activity
}
} );
You are wrong class in listview item click
lv_city.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Pref.setValue( SelectCity.this, Const.PREF_CITY_ID,
cityList.get( position ).getCity_id() );
finish();//finishing activity
}
});
#quick learner's was correct, But i put some idea for your ref..
If you use custom adapter class, In my suggestion use setOnClickListener in custom adapter class this is better way to do this type of case!!
For example,
mViewHolder.MyUI.setOnClickListener
problem with settingText for each and individual edittext in listview.Edittext focus missing..Check my image above which is haveing custom keypad and i need to settext when keypad buttons is pressed.Below is my code. i tried but the focus is going for some other edittext.Please help me out.
private LayoutInflater mInflater;
public ArrayList myItems = new ArrayList();
List < HashMap < String, String >> painItems;
public MyAdapet()
{
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
painItems = new ArrayList < HashMap < String, String >> ();
HashMap < String, String > map = new HashMap < String, String > ();
for(int i=1;i<10;i++)
{
map.put("row_2", "Location Content");
map.put("row_3","Content");
map.put("row_4", "50");
map.put("row_5", "");
painItems.add(map);
}
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return painItems.size();
}
#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 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_xml, null);
} else {
}
returnText = (EditText) convertView
.findViewById(R.id.returnedit);
returnText.setInputType(InputType.TYPE_NULL);
//Fill EditText with the value you have in <span id="r774jm31_1" class="r774jm31">data source</span>
//we need to update adapter once we finish with editing
returnText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
listview.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
returnText.requestFocus();
}
});
return convertView;
}
My Listxml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="250dp" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:descendantFocusability="afterDescendants"
android:focusable="false"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
My custom List:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#cfcfcf"
android:gravity="center">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/seq_bg"
android:layout_centerInParent="true"
/>
<TextView
android:id="#+id/sqlno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="left"
android:text="1"
android:textColor="#000000"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Small"
android:textStyle="bold" />
</RelativeLayout>
<TextView
android:id="#+id/lcontent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="Location"
android:textColor="#ffffff"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Small"
android:textStyle="bold" />
<TextView
android:id="#+id/addcontent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="Add Content"
android:textColor="#ffffff"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Small"
android:textStyle="bold" />
<TextView
android:id="#+id/drop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="50"
android:textColor="#ffffff"
android:gravity="center"
android:textStyle="bold"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Small"
android:layout_weight="1"
/>
<EditText
android:id="#+id/returnedit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="67"
android:textColor="#000000"
android:textStyle="bold"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Small"
android:layout_weight="1"
/>
<Button
android:id="#+id/viewnotesButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="view Notes"
android:textColor="#000000"
android:textStyle="bold"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Small"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
My clicklisteners for custom kepad is like this:
button0.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
returnText.setText(returnText.getText().toString()+"0");
MyAdapet.notifyDataSetChanged();
}
});
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
returnText.setText(returnText.getText().toString()+"1");
MyAdapet.notifyDataSetChanged();
}
});
try this code -
returnText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
listview.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
returnText.requestFocus();
notifyDataSetChanged(); // for update adapter
}
});
Hope this code helps you !!!
if its not working please let me know i will try to help more.
I have a model class. Now from my activity i want to set the values in model class & show them in gridview using my custom adapter. After that i need to store the object in a variable(class type) from gridview's onItemClick. I have done the below codes. But its not working. Where did i go wrong?
activity_country.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=".CountryActivity" >
<TextView
android:id="#+id/nTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="21dp"
android:layout_marginTop="26dp"
android:text="Name" />
<TextView
android:id="#+id/aTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/nTextView"
android:layout_below="#+id/nTextView"
android:layout_marginTop="24dp"
android:text="About" />
<EditText
android:id="#+id/CountryNameEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/nTextView"
android:layout_alignBottom="#+id/nTextView"
android:layout_marginLeft="48dp"
android:layout_toRightOf="#+id/nTextView"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/CountryAboutEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/CountryNameEditText"
android:layout_alignTop="#+id/aTextView"
android:ems="10" />
<Button
android:id="#+id/saveCountryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/CountryAboutEditText"
android:layout_below="#+id/CountryAboutEditText"
android:layout_marginLeft="16dp"
android:layout_marginTop="22dp"
android:text="Save"
android:onClick="saveCountry"
/>
<GridView
android:id="#+id/countryGridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/saveCountryButton"
android:layout_centerHorizontal="true"
android:numColumns="2">
</GridView>
</RelativeLayout>
CountryActivity.java
public class CountryActivity extends Activity
{
ArrayList<Country> countries = new ArrayList<Country>();
Country aCountry;
EditText CountryNameTxtBox;
EditText CountryAboutTxtBox;
GridView countrygGridView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_country);
// Show the Up button in the action bar.
setupActionBar();
CountryNameTxtBox = (EditText)findViewById(R.id.CountryNameEditText);
CountryAboutTxtBox = (EditText)findViewById(R.id.CountryAboutEditText);
countrygGridView = (GridView)findViewById(R.id.countryGridView);
}
public void saveCountry(View view)
{
String txtName = CountryNameTxtBox.getText().toString();
String txtAbout = CountryAboutTxtBox.getText().toString();
showGrid();
}
public void showGrid()
{
/*
ArrayAdapter<Country> adapter = new ArrayAdapter<Country>(this,android.R.layout.simple_list_item_1, countries);
countrygGridView.setAdapter(adapter);
*/
countrygGridView.setAdapter(new myAdapter(this,countries));
countrygGridView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View v,int position, long id)
{
for(Country mCountry: countries)
{
if(countries.contains(aCountry))
{
Toast.makeText(CountryActivity.this,countries.get(position).getName(), 2000).show();
}
}
}
});
}
countrygrid.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/label1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:layout_marginTop="15sp"
android:textSize="15sp" >
</TextView>
<TextView
android:id="#+id/label2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:layout_marginTop="15sp"
android:textSize="15sp" >
</TextView>
</LinearLayout>
myAdapter.java
public class myAdapter extends BaseAdapter
{
Context mycontext;
ArrayList<Country> countries;
public myAdapter(Context c, ArrayList<Country> obj)
{
this.mycontext=c;
this.countries = obj;
}
public int getCount() {
// TODO Auto-generated method stub
return countries.size();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return countries.get(arg0);
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertview, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) mycontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if(convertview==null)
{
gridView =new View(mycontext);
gridView =inflater.inflate(R.layout.countrygrid, null);
TextView txtvw1 = (TextView) gridView.findViewById(R.id.label1);
txtvw1.setText(countries.get(position).getName());
TextView txtvw2 = (TextView) gridView.findViewById(R.id.label2);
txtvw2.setText(countries.get(position).getAbout());
}
else
{
gridView = (View) convertview;
}
return gridView;
}
}
*Its showing error in countryActivity.java- when i am trying to set the adapter inside showGrid() *
countrygGridView.setAdapter(new myAdapter(this,countries));
public class myAdapter
{
does not extend anything
It should be
public class myAdapter extends BaseAdapter //orArrayAdapter
{
If you still have a problem post the stacktrace for further help
I'm trying to implement a custom ExpandableListView inside a SherlockFragment. I followed this sample to extend BaseExpandableListAdapter and create my custom adapter.
Here is my problem, when my expandable list is displayed on the screen I can see all the group item, but when I click on one of them, the child item which are supposed to be displayed below it don't show up.
I tried to debug it, the onGroupExpandListener I put in the adapter is never called, and actually I put breakpoints in the different methods of my adapter, there are not called ever when I click on one of the group item.
I tried to modify my xml files which define the group item and the child item, to make them clickable or focus-able, nothing changed. I also tried to remove the editText, Button, and CheckBox I put in there, thinking it was may be creating a conflict ...
I thought the problem was may be due to some incompatibility issues using ExpandableListView with SherlockFragment, but according to the developer's forum its not.
So I really don't know where to look now, it's may be simply a rookie mistake I did in my adapter...
Any help would be great,
Thanks in advance!
here is my code:
public class BuildingExpandalbeListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private ExpandableListView mExpandableListView;
private SideEntity[] mSidesCollection;
private int[] groupStatus;
public BuildingExpandalbeListAdapter(Context pContext,
ExpandableListView pExpandableListView,
SideEntity[] pSidesCollection) {
mContext = pContext;
mSidesCollection = pSidesCollection;
mExpandableListView = pExpandableListView;
groupStatus = new int[mSidesCollection.length];
mExpandableListView.setClickable(true);
setListEvent();
}
private void setListEvent() {
mExpandableListView
.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int arg0) {
// TODO Auto-generated method stub
groupStatus[arg0] = 1;
}
});
mExpandableListView
.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int arg0) {
// TODO Auto-generated method stub
groupStatus[arg0] = 0;
}
});
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return mSidesCollection[groupPosition].getSegmentEntity(childPosition)
.getName();
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return mSidesCollection[groupPosition].getSegmentEntity(childPosition)
.getId();
}
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ChildHolder childHolder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(
R.layout.building_list_item, null);
childHolder = new ChildHolder();
childHolder.editText1 = (EditText) convertView
.findViewById(R.id.editText1);
childHolder.checkBox1 = (CheckBox) convertView
.findViewById(R.id.checkBox1);
convertView.setTag(childHolder);
} else {
childHolder = (ChildHolder) convertView.getTag();
}
childHolder.editText1.setText(mSidesCollection[groupPosition]
.getSegmentEntity(childPosition).getName());
childHolder.checkBox1.setChecked(mSidesCollection[groupPosition]
.getSegmentEntity(childPosition).hasDoor());
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return mSidesCollection[groupPosition].getSegmentsCollection().size();
}
#Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return mSidesCollection[groupPosition];
}
#Override
public int getGroupCount() {
// TODO Auto-generated method stub
return mSidesCollection.length;
}
#Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
GroupHolder groupHolder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(
R.layout.building_list_group, null);
groupHolder = new GroupHolder();
groupHolder.editText1 = (EditText) convertView
.findViewById(R.id.editText1);
groupHolder.editText2 = (EditText) convertView
.findViewById(R.id.editText2);
convertView.setTag(groupHolder);
} else {
groupHolder = (GroupHolder) convertView.getTag();
}
groupHolder.editText1
.setText(mSidesCollection[groupPosition].getName());
groupHolder.editText2.setText(Integer
.toString(mSidesCollection[groupPosition]
.getSegmentsCollection().size()));
return convertView;
}
class GroupHolder {
EditText editText1;
EditText editText2;
}
class ChildHolder {
EditText editText1;
CheckBox checkBox1;
}
#Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
Here is my SherlockFragment using this adapter :
public class BuildingFragment extends SherlockFragment {
private ViewGroup myViewGroup;
private View v;
private SideEntity[] mSideCollection;
private BuildingsDbAdapter buildingDataBase;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
myViewGroup = container;
myViewGroup.removeAllViews();
v = inflater.inflate(R.layout.building_data_layout, container, false);
buildingDataBase = new BuildingsDbAdapter(getSherlockActivity());
return v;
}
#Override
public void onResume() {
super.onResume();
buildingDataBase.open();
mSideCollection = BuildingsDbAdapter
.fetchSideMatchingBuildingId(CustomTabFragmentActivity.mBuildingId);
for (int i = 0; i < mSideCollection.length; i++) {
BuildingsDbAdapter.fetchSegmentMatchingSideId(
mSideCollection[i].getId(), mSideCollection[i]);
}
ExpandableListView mExpandableListView = (ExpandableListView) v
.findViewById(R.id.expandableListView1);
BuildingExpandalbeListAdapter mAdapter = new BuildingExpandalbeListAdapter(
v.getContext().getApplicationContext(), mExpandableListView,
mSideCollection);
mExpandableListView.setAdapter(mAdapter);
buildingDataBase.close();
}
}
and here is my xml files :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="10dip"
android:layout_weight="3"
android:singleLine="true"
android:text="Building name: "
android:textColor="#android:color/black"
android:textSize="13dip" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="10dip"
android:layout_weight="4"
android:singleLine="true"
android:text="Columbia Tower"
android:textColor="#android:color/black"
android:textSize="15dip"
android:textStyle="italic" />
<!-- <Button
android:id="#+id/button1"
android:layout_width="0dip"
android:layout_height="30dp"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="10dip"
android:layout_weight="1"
android:background="#color/honeycombish_blue"
android:drawableTop="#drawable/edit_query"
android:gravity="center_vertical|center_horizontal" /> -->
</LinearLayout>
<ExpandableListView
android:id="#+id/expandableListView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:divider="#android:color/black"
android:clipChildren="false"
android:focusable="true" >
</ExpandableListView>
</LinearLayout>
the group_item :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="center_vertical"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/RelativeLayout"
android:layout_marginTop="6dip"
android:layout_toLeftOf="#+id/button1"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="5dip"
android:layout_weight="2"
android:singleLine="true"
android:text="Side: "
android:textColor="#android:color/black"
android:textSize="13dip" />
<EditText
android:id="#+id/editText1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="5dip"
android:layout_weight="2"
android:singleLine="true"
android:textColor="#android:color/black"
android:textSize="15dip"
android:textStyle="italic" />
<TextView
android:id="#+id/textView3"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="5dip"
android:layout_weight="5"
android:singleLine="true"
android:text="Number of Segment:"
android:textColor="#android:color/black"
android:textSize="13dip" />
<EditText
android:id="#+id/editText2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="5dip"
android:layout_weight="2"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="13dip" />
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignBottom="#+id/linearLayout1"
android:layout_alignParentRight="true"
android:layout_marginLeft="2dip"
android:background="#android:color/transparent"
android:drawableBottom="#drawable/ic_edit_shape"
android:gravity="bottom|center_horizontal" />
</RelativeLayout>
the list_item :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignBottom="#+id/linearLayout1"
android:layout_alignParentLeft="true"
android:layout_marginLeft="2dip"
android:background="#android:color/transparent"
android:drawableBottom="#drawable/ic_edit_shape"
android:gravity="bottom|center_horizontal" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/RelativeLayout"
android:layout_marginTop="6dip"
android:layout_toRightOf="#+id/button1"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="5dip"
android:layout_weight="2"
android:singleLine="true"
android:text="Name: "
android:textColor="#android:color/black"
android:textSize="13dip" />
<EditText
android:id="#+id/editText1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="5dip"
android:layout_weight="2"
android:singleLine="true"
android:text="Shop1"
android:textColor="#android:color/black"
android:textSize="15dip"
android:textStyle="italic" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="0dip"
android:layout_height="30dip"
android:layout_weight="1"
android:hint="Door" />
</LinearLayout>
</RelativeLayout>
Ok I find my mistake, the problem was in fact coming from both EditText and the Button I have been using in my group items. When I set this widgets to non clickable and non focusable the click on my group items perform normally.
I should have detected this conflict sooner, I tried before to remove both my Button and my EditText, but i forgot that at some point I had try to add android:clickable="true" to my RelativeLayout (thinking it would allowed the click on my group items) but instead it has also created a conflict :/
Anyway, if someone has a similar problem, keep in mind to check if any of your view inside your group item is clickabble or focusable and set them to android:clickable="false" android:focusable="false"
i am trying to display image using GridView. This is the first time i using GridView, so i using the example from here and implement it to mine (i have tried the example that contained there, and it's works).
But, i have checked it many time, there's no error comes from LogCat, no Force Closed, the image didn't show. i have no idea where's the wrong part.
Here's my code:
choosepic.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/bg_inner">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/book_inner"
android:layout_marginTop="50dp"
/>
<ImageButton
android:id="#+id/homeBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/home_btn"
android:background="#null"
/>
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#drawable/bg_arrow_btn"
android:layout_alignParentRight="true"
/>
<ImageButton
android:id="#+id/nextBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/right_arrow"
android:background="#null"
android:layout_alignParentRight="true"
android:layout_marginTop="5dp"
android:layout_marginRight="7dp"
android:layout_marginLeft="7dp"
/>
<ImageButton
android:id="#+id/prevBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/left_arrow"
android:background="#null"
android:layout_toLeftOf="#+id/nextBtn"
android:layout_marginTop="5dp"
/>
<GridView
android:id="#+id/gridView1"
android:numColumns="3"
android:gravity="center"
android:columnWidth="30dp"
android:stretchMode="columnWidth"
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="70dp"
>
</GridView>
</RelativeLayout>
animalbutton.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</ImageView>
<TextView
android:text="TextView"
android:layout_height="wrap_content"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="18sp"
android:visibility="gone">
</TextView>
ImageAdapter.java
public class ImageAdapter extends BaseAdapter{
private Activity activity;
private ArrayList<String> listNm;
private ArrayList<Integer> listAnim;
public ImageAdapter(Activity activity,ArrayList<String> listName, ArrayList<Integer> listImage) {
super();
this.listNm = listName;
this.listAnim = listImage;
this.activity = activity;
}
public static class ViewHolder
{
public ImageView imgViewAnim;
public TextView txtViewAnim;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(convertView==null)
{
view = new ViewHolder();
convertView = inflator.inflate(R.layout.animalbutton, null);
view.txtViewAnim = (TextView) convertView.findViewById(R.id.textView1);
view.imgViewAnim = (ImageView) convertView.findViewById(R.id.grid_item_image);
convertView.setTag(view);
}
else
{
view = (ViewHolder) convertView.getTag();
}
view.txtViewAnim.setText(listNm.get(position));
view.imgViewAnim.setImageResource(listAnim.get(position));
return convertView;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
#Override
public String getItem(int position) {
// TODO Auto-generated method stub
return listNm.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
choosepic.java
public class choosepic extends Activity {
/** Called when the activity is first created. */
ImageAdapter mAdapter;
GridView gridView;
static final String[] animal = new String[] {
"cat", "cow","croc", "duck", "elephant", "giraffe", "lion", "moose", "mouse"};
private ArrayList<String> listNm;
private ArrayList<Integer> listAnim;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.choosepic);
gridView = (GridView) findViewById(R.id.gridView1);
prepare_list1();
mAdapter = new ImageAdapter(this,listNm, listAnim);
gridView.setAdapter(mAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
Toast.makeText(getApplicationContext(), mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
}
});
}
public void prepare_list1(){
listNm = new ArrayList<String>();
listAnim = new ArrayList<Integer>();
for (int i = 0; i < animal.length; i++) {
listNm.add(animal[i]);
listAnim.add(getResources().getIdentifier("anim_"+animal[i], "drawable", getPackageName()));
}
}
}
i need some help. i appreciate any help. thank you in advance!
i think the problem is in your getCount() that returns 0 elements
instead of that make it like this
#Override
public int getCount() {
// TODO Auto-generated method stub
return listNm.size();
}