I have three spinners in a fragment. They are populated from JSON received from a remote server.
I can see that all three spinners have items.
The spinners are spinner_categoria, spinner_marca and spinner_modelo.
The issue is that only spinner_marca and spinner_modelo are showing their items when touched on them.
spinner_categoria shows the first item, but when touched on it, nothing happens, and I know it has 3 other items.
This is my code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.nuevo_disp_1, container, false);
spinner_modelo = (Spinner) v.findViewById(R.id.spinner_modelos);
layout_modelos = (LinearLayout) v.findViewById(R.id.layout_modelos);
//CATEGORIAS
txt_categoria = (TextView) v.findViewById(R.id.txt_categoria);
spinner_categoria = (Spinner) v.findViewById(R.id.spinner_categorias);
layout_categorias = (LinearLayout) v.findViewById(R.id.layout_categorias);
adapterCategoria = new AdapterCategorias(getActivity(), listCategorias);
spinner_categoria.setAdapter(adapterCategoria);
spinner_categoria.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
txt_categoria.setText("Categoría seleccionada : " + listCategorias.get(position).getNombre());
layout_marcas.setVisibility(View.VISIBLE);
callDataMarcas();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
//MARCAS
spinner_marca = (Spinner) v.findViewById(R.id.spinner_marcas);
layout_marcas = (LinearLayout) v.findViewById(R.id.layout_marcas);
txt_marca = (TextView) v.findViewById(R.id.txt_marca);
spinner_marca = (Spinner) v.findViewById(R.id.spinner_marcas);
layout_marcas.setVisibility(View.GONE);
adapterMarca = new AdapterMarcas(getActivity(), listMarcas);
spinner_marca.setAdapter(adapterMarca);
spinner_marca.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
txt_marca.setText("Marca seleccionada : " + listMarcas.get(position).getNombre());
layout_modelos.setVisibility(View.VISIBLE);
callDataModelos();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
//MODELOS
txt_modelo = (TextView) v.findViewById(R.id.txt_modelo);
spinner_modelo = (Spinner) v.findViewById(R.id.spinner_modelos);
adapterModelo = new AdapterModelos(getActivity(), listModelos);
spinner_modelo.setAdapter(adapterModelo);
spinner_modelo.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
txt_modelo.setText("Modelo seleccionado : " + listModelos.get(position).getNombre());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
callDataCategorias();
return v;
}
What is wrong on it?
EDIT
XML from the fragment:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="#+id/marcas"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/layout_categorias"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtbusqueda"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Elige categoría:"
android:textSize="20dp"
android:textStyle="bold" />
<Spinner
android:id="#+id/spinner_categorias"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/txt_categoria"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Categoría seleccionada:"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout_marcas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/layout_categorias"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Elige marca:"
android:textSize="20dp"
android:textStyle="bold" />
<Spinner
android:id="#+id/spinner_marcas"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/txt_marca"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Marca seleccionada:"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout_modelos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/layout_marcas"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Elige modelo:"
android:textSize="20dp"
android:textStyle="bold" />
<Spinner
android:id="#+id/spinner_modelos"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/txt_modelo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Modelo seleccionado:"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
Related
I am new here so please bear with my naivety. I am working on a music player that will display a menu on clicking 'dots' buttton(given in code below) in listview item. Inside the menu there will be a button to delete. The listview is getting populated by a custom adapter. Here is the code of adapter...
private class hashmapAdapter extends BaseAdapter implements Filterable {
HashMap<Integer, NowPlayingActivity.Details> NewDetails = new HashMap<>();
private hashmapAdapter(HashMap<Integer, NowPlayingActivity.Details> hashMap) {
if(NewDetails.size()==0) {
NewDetails.putAll(hashMap);
}
}
#Override
public int getCount() {
return NewDetails.size();
}
#Override
public Object getItem(int position) {
return NewDetails.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final View result;
if(convertView == null) {
result = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_item,parent,false);
} else {
result = convertView;
}
((TextView)result.findViewById(R.id.Name)).setText(NewDetails.get(position).getName());
((TextView)result.findViewById(R.id.artist)).setText(NewDetails.get(position).getArtist());
((TextView)result.findViewById(R.id.duration)).setText(NewDetails.get(position).getDuration()+" || ");
dots = (Button)result.findViewById(R.id.dots);
dots.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
name = (TextView)result.findViewById(R.id.Name);
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View popup_view = inflater.inflate(R.layout.option_layout,null);
RelativeLayout item = (RelativeLayout) popup_view.findViewById(R.id.popup);
final float width= (getResources().getDimension(R.dimen.width_entry_in_dp));
final float height= getResources().getDimension(R.dimen.height_entry_in_dp);
final PopupWindow popupWindow = new PopupWindow(popup_view,Math.round(width),Math.round(height),true);
popupWindow.showAtLocation(item, Gravity.END, 0, 0);
popupWindow.showAsDropDown(dots);
play_next_btn = (Button)popup_view.findViewById(R.id.play_next);
add_to_playlist = (Button)popup_view.findViewById(R.id.add_to_playlist);
share = (Button)popup_view.findViewById(R.id.share);
delete = (Button)popup_view.findViewById(R.id.delete);
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(int i=0;i<NewDetails.size();i++) {
if(NewDetails.get(i).getName().equals(name.getText().toString())) {
for(int j=0;j<hashMap.size();j++) {
if(hashMap.get(j).getName().equals(name.getText().toString())) {
if (true) {
NewDetails.remove(i);
hashMap.remove(j);
}
}
}
}
}
populate1(NewDetails);
popupWindow.dismiss();
}
});
}
});
return result;}
Now the problem is that after deleting element when i call populate1(NewDetails), it gives a null pointer exception on NewDetails.get(position).getName(). So on repopulating listview i am getting NPE error. Following is the populate1() function code...
public void populate1(HashMap<Integer,NowPlayingActivity.Details> hashMap) {
adapter = new hashmapAdapter(hashMap);
library.setAdapter(adapter);
adapter.notifyDataSetChanged();
library.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
name = (TextView)view.findViewById(R.id.Name);
String SongName = name.getText().toString();
Intent intent = new Intent(getApplicationContext(),NowPlayingActivity.class);
intent.putExtra("SongName",SongName);
setResult(555,intent);
finish();
}
});
}
I have already tried notifydatasetchanged() and listview.invalidateviews(), both didnt work. File is getting deleted but the adapter is not refreshing the list to remove element. Here is the xml layout of single_item for listview....`
`<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="70dp"
android:paddingRight="8dp"
android:paddingEnd="8dp"
android:background="#drawable/element_shape_list"
android:descendantFocusability="blocksDescendants">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/v1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:foreground="?android:attr/selectableItemBackground">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/v11"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="#+id/art_work"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#mipmap/ic_launcher"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"/>
<TextView
android:id="#+id/Name"
android:textSize="17sp"
android:textColor="#000"
android:textStyle="bold"
android:gravity="bottom"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginRight="30dp"
android:layout_marginEnd="30dp"
android:layout_toRightOf="#id/art_work"
android:layout_toEndOf="#+id/art_work"
android:singleLine="true"
/>
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginRight="30dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="10dp"
android:layout_toRightOf="#+id/art_work"
android:layout_toEndOf="#id/art_work"
android:layout_below="#id/Name">
<TextView
android:id="#+id/duration"
android:textSize="12sp"
android:textColor="#000"
android:layout_width="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_height="20dp"
android:singleLine="true"
android:gravity="clip_horizontal"
android:ellipsize="end"/>
<TextView
android:id="#+id/artist"
android:textSize="12sp"
android:textColor="#000"
android:layout_toRightOf="#+id/duration"
android:layout_toEndOf="#+id/duration"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:gravity="fill_horizontal"
android:layout_height="20dp"
android:singleLine="true" />
</RelativeLayout>
</RelativeLayout>
</FrameLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:background="#android:color/transparent">
<Button
android:id="#+id/dots"
android:layout_width="40dp"
android:textStyle="bold"
android:background="?android:attr/selectableItemBackground"
android:layout_height="70dp"
android:text="#string/vertical_ellipsis"
android:textSize="25sp" />
</FrameLayout>
</RelativeLayout>
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
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);
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'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"