OnListIemClick set visibility for each row - android

I am trying to show a list that when I pressed it in that position I would like to show an image that is gone and to change the typeface of that row. So first I have a xml file with a simple list and then another xml for each row, this one:
<ImageView
android:id="#+id/icono"
android:layout_gravity="center"
android:layout_marginRight="5dip"/>
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:text="TextView" />
<ImageView
android:id="#+id/image_tick_ingred"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="false"
android:layout_alignParentLeft="false"
android:layout_alignParentRight="true"
android:layout_alignParentTop="false"
android:layout_alignRight="#id/textView1"
android:layout_centerVertical="true"
android:src="#android:drawable/presence_online" />
</RelativeLayout>
So that is the code to each row, contains an image on the left then a text and finally another image on the right of the row. And the java code:
public class TabIngred extends ListActivity {
ImageView img;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rellenas_ingr); //the list xml layout
Bundle extras = getIntent().getExtras(); //get data
if (extras != null) {
String ingredientesLista[] = extras.getStringArray("ingredientesLista");
int[] ingredientesImagen=extras.getIntArray("ingredientesImagen");
setListAdapter (new IngredienteAdapter(this, R.layout.rellenas_ingr_fila, ingredientesLista, ingredientesImagen));
getListView().setChoiceMode(2);
}
}
private class IngredienteAdapter extends ArrayAdapter<String>{
private String listaIngred[];
private int[] listaImagenes;
public IngredienteAdapter(Context context, int textViewResourceId, String ingredientesLista[], int[] ingredientesImagen) {
super(context, textViewResourceId, ingredientesLista);
this.listaIngred = ingredientesLista;
this.listaImagenes=ingredientesImagen;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// XML de la vista de la fila
v = vi.inflate(R.layout.rellenas_ingr_fila, null); //the row for the listview
}
String ingred = listaIngred[position];
int imagen=listaImagenes[position];
int tickImagen = R.drawable.tick_ingred;//set the image to a tick
if (ingred != null) {
TextView ttitulo = (TextView) v.findViewById(R.id.textView1);
if (ttitulo != null) {
ttitulo.setText(ingred);
}
ImageView timagen = (ImageView) v.findViewById(R.id.icono);
if (timagen != null) {
timagen.setImageResource(imagen);
}
ImageView ttick = (ImageView) v.findViewById(R.id.image_tick_ingred);
if (ttick != null) {
ttick.setImageResource(tickImagen);
ttick.setVisibility(View.GONE);
}
}
return v;
}
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
img=(ImageView)v.findViewById(R.id.image_tick_ingred);
TextView t= (TextView)v.findViewById(R.id.textView1);
if(img.getVisibility()==View.GONE){
t.setTypeface(null, Typeface.BOLD);
img.setVisibility(View.VISIBLE);
}else {
t.setTypeface(null, Typeface.NORMAL);
img.setVisibility(View.GONE);
}
}
}
So, I define the getview(), I put the image on gone (thats works), but then the OnlistItemClick() doesn´t work. First when I selected an Item the image isn´t visible and apart for that indefferently the position I pressed the first row is which change the typeface, not the item what I pressed.
Thanks :)

in you onListItemClick you need to use the View
for example:
img=(ImageView)findViewById(R.id.image_tick_ingred);
TextView t= (TextView)findViewById(R.id.textView1);
should be
img=(ImageView)v.findViewById(R.id.image_tick_ingred);
TextView t= (TextView)v.findViewById(R.id.textView1);
notice the v that is the view of the row clicked

The problem was that I define getListView().setChoiceMode(2); and that was because it doesn´t work, I delete the line and works perfect!!
Thanks after all!

Related

In Android, how to update GridView cell element control when selected?

I am trying to implement a GridView in Android to display a list of Products as mentioned in below image:
With Custom Button and Grid List I am implemented this.
I want to know, how can I make this Product Button Red when I select it. Or in other words, I want to get the selected cell item object and change the background color to red, TextViews text color to white. Plus, at the same time, I want to make all remaining cell items to default white background and text color to purple.
I am new to android, any help would be great support. Thanks in advance. Here is my code:
GridView in Fragment
<GridView
android:id="#+id/grid_Products"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:horizontalSpacing="10dp"
android:gravity="center"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp />
In ProductFragment class in onCreateView() method, I am binding the productModels to gridView
List<ProductModel> productModels;
GridView gdGridView=(GridView)(view.findViewById(R.id.grid_Products));
adapter = new ProductButtonAdaptor(view.getContext(), productModels);
gdGridView.setAdapter(adapter);
product_button.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btn_product_red"
android:id="#+id/pnl_ProudctButton"
android:orientation="vertical">
<LinearLayout
android:paddingTop="10dp"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="5"
android:gravity="center"
android:layout_gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/lbl_ProductName"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="bottom"
android:text="5"
android:layout_marginRight="2dp"
android:textColor="#color/purple"
android:textAlignment="center"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="#+id/lbl_ProductCurrency"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:gravity="bottom"
android:text="QAR"
android:textAlignment="center"
android:textColor="#color/purple"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<View
android:id="#+id/lbl_ProductSeparator"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_alignParentBottom="true"
android:layout_margin="4dp"
android:background="#color/purple" />
<TextView
android:id="#+id/lbl_ProductCategory"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:gravity="top"
android:text="International"
android:textAlignment="center"
android:textColor="#color/purple"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="16dp" />
</LinearLayout>
ProductButtonAdpater class:
public class ProductButtonAdaptor extends ArrayAdapter<ProductModel> implements AdapterView.OnItemClickListener
{
private Context context;
private final List<ProductModel> productModels;
private int selected = -1;
public ProductButtonAdaptor(Context context, List<ProductModel> productValues)
{
super(context, R.layout.button_product, productValues);
this.context = context;
this.productModels = productValues;
}
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null)
{
gridView = new View(context);
// get layout from button_product.xml
gridView = inflater.inflate(R.layout.button_product, null);
// set value into textview
TextView lbl_ProductName = (TextView)gridView.findViewById(R.id.lbl_ProductName);
TextView lbl_ProductCurrency = (TextView)gridView.findViewById(R.id.lbl_ProductCurrency);
TextView lbl_ProductCategory = (TextView)gridView.findViewById(R.id.lbl_ProductCategory);
lbl_ProductName.setText(productModels.get(position).getCode());
lbl_ProductCurrency.setText(productModels.get(position).getCurrency());
lbl_ProductCategory.setText(productModels.get(position).getCategoryName());
}
else
{
gridView = (View)convertView;
}
if (selected == position)
{
TextView lbl_ProductName = (TextView)gridView.findViewById(R.id.lbl_ProductName);
TextView lbl_ProductCurrency = (TextView)gridView.findViewById(R.id.lbl_ProductCurrency);
TextView lbl_ProductCategory = (TextView)gridView.findViewById(R.id.lbl_ProductCategory);
View lbl_ProductSeperator = (View)gridView.findViewById(R.id.lbl_ProductSeparator);
LinearLayout pnl_ProductButton = (LinearLayout)gridView.findViewById(R.id.pnl_ProudctButton);
lbl_ProductName.setTextColor(ContextCompat.getColor(context, R.color.vodafone_white));
lbl_ProductCurrency.setTextColor(ContextCompat.getColor(context, R.color.vodafone_white));
lbl_ProductCategory.setTextColor(ContextCompat.getColor(context, R.color.vodafone_white));
lbl_ProductSeperator.setBackgroundColor(ContextCompat.getColor(context, R.color.vodafone_white));
pnl_ProductButton.setBackground(ResourcesCompat.getDrawable(context.getResources(), R.drawable.btn_product_red, null));
}
else
{
//setup the other cells
TextView lbl_ProductName = (TextView)gridView.findViewById(R.id.lbl_ProductName);
TextView lbl_ProductCurrency = (TextView)gridView.findViewById(R.id.lbl_ProductCurrency);
TextView lbl_ProductCategory = (TextView)gridView.findViewById(R.id.lbl_ProductCategory);
View lbl_ProductSeperator = (View)gridView.findViewById(R.id.lbl_ProductSeparator);
LinearLayout pnl_ProductButton = (LinearLayout)gridView.findViewById(R.id.pnl_ProudctButton);
lbl_ProductName.setTextColor(ContextCompat.getColor(context, R.color.vodafone_purple));
lbl_ProductCurrency.setTextColor(ContextCompat.getColor(context, R.color.vodafone_purple));
lbl_ProductCategory.setTextColor(ContextCompat.getColor(context, R.color.vodafone_purple));
lbl_ProductSeperator.setBackgroundColor(ContextCompat.getColor(context, R.color.vodafone_purple));
pnl_ProductButton.setBackground(ResourcesCompat.getDrawable(context.getResources(), R.drawable.btn_product_white, null));
}
return gridView;
}
#Override
public int getCount()
{
return productModels.size();
}
#Override
public ProductModel getItem(int position)
{
return productModels.get(position);
}
#Override
public long getItemId(int position)
{
return 0;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
selected = position;
this.notifyDataSetChanged();
//adapter.notifyDataChanged();
//parent.invalidate();
//view.invalidate();
}
}
this is the updated code
I think you want to do it programatically (not styles).
So,
You have to declare a int selected; variable which stores index of a selected cell (or -1 if none is selected). Then, you have to implement onClickListener on each cell and change selected value when any element is tapped and redraw all data grid cells using notify... method.
After that, do not forget to change the setup block of colors and other parameters of each sell in the correspond method of the adapter.
inside onCreate() method:
gridview.setOnItemClickListener(adapter);
your class:
public class ProductButtonAdaptor extends BaseAdapter implemets onItemClickListener {
private Context context;
private final ProductModel[] productModels;
private int selected = -1;
public ProductButtonAdaptor(Context context, ProductModel[] productValues) {
this.context = context;
this.productModels = productValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from button_product.xml
gridView = inflater.inflate(R.layout.button_product, null);
// set value into textview
TextView lbl_ProductName = (TextView) gridView.findViewById(R.id.lbl_ProductName);
TextView lbl_ProductCurrency = (TextView) gridView.findViewById(R.id.lbl_ProductCurrency);
TextView lbl_ProductCategory = (TextView) gridView.findViewById(R.id.lbl_ProductCategory);
lbl_ProductName.setText(productModels[position].Name);
lbl_ProductCurrency.setText(productModels[position].Currency);
lbl_ProductCategory.setText(productModels[position].CategoryName);
} else {
gridView = (View) convertView;
}
if (selected == position) {
//setup selected cell
//for example
gridView.setBackgroundColor(Color.red);
} else {
//setup the other cells
gridView.setBackgroundColor(Color.white);
}
return gridView;
}
#Override
public int getCount() {
return productModels.length;
}
#Override
public Object getItem(int position) {
return productModels[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selected = position;
adapter.notifyDataChanged();
//you can pass the grid as a paramater of constructor if you need it
grid.invalidateViews();
}

The TextViews populated in my GridView appear blank

I'm using a Gridview to show a list of skills. The code runs fine, even populates exact number of items according to my array. However, the TextView that should display the item names is blank.
This is the code for my gridview adapter
SkillsAdapter.java
public class SkillsAdapter extends BaseAdapter {
private Context context;
private final String[] skillValues;
public SkillsAdapter(Context context, String[] skillValues) {
this.context = context;
this.skillValues = skillValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.skills_single_item, null);
// set value into textview
textView = (TextView) gridView.findViewById(R.id.single_label);
textView.setText(skillValues[position]);
} else {
gridView = (View) convertView;
}
return gridView;
}
#Override
public int getCount() {
return skillValues.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
This is the code for my layout file skills_single_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
android:background="#drawable/rounded_corners">
<TextView
android:id="#+id/single_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_gravity="center"/>
</RelativeLayout>
The array is not null. The error occurs somewhere around here
// set value into textview
textView = (TextView) gridView.findViewById(R.id.single_label);
textView.setText(skillValues[position]);
Change your layout Item XML with this your I used your Adapter class its work fine at my side
it may help you
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="horizontal"
android:padding="5dp">
<TextView
android:id="#+id/single_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="8dp"
android:textColor="#000000" />
</RelativeLayout>
Set test in your text view out of if condition like:
if (convertView == null) {
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.skills_single_item, null);
// set value into textview
textView = (TextView) gridView.findViewById(R.id.single_label);
} else {
gridView = (View) convertView;
}
textView.setText(skillValues[position]);
Move following line out of if-else blocks
textView.setText(skillValues[position]);
Like
if (convertView == null) {
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.skills_single_item, null);
textView = (TextView) gridView.findViewById(R.id.single_label);
} else {
gridView = (View) convertView;
}
// set value into textview
textView.setText(skillValues[position]);
I recommend you to read android ViewHolder pattern.

Toggling a populated list view between visible and invisible

I have created a simple list view with two different text views (shown below). I want to have a series of rows that will display more information when clicked--one text view hidden that can be toggled by clicking the other text view. I think my problem might be the linear layout encapsulating the two text views, but I'm not sure. The slide_up and slide_down animations were basic ones taken from a website.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:textSize="20sp"
android:clickable="true"
android:onClick="toggle_contents"/>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="example"
android:textSize="20sp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"/>
</LinearLayout>
The list view is populated from a content provider as follows:
cursor.moveToFirst();
ArrayList<String> listItems = new ArrayList<String>();
ArrayList<String> listItems2 = new ArrayList<String>();
// listItems = new ArrayList<ListItem>();
ListView mylist=(ListView) findViewById(R.id.list);
do{
String id=cursor.getString(cursor.getColumnIndex("id"));
String name=cursor.getString(cursor.getColumnIndex("name"));
// String name_str = id +" : "+ name;
listItems2.add(id);
listItems.add(name);
cursor.moveToNext();
}while(!cursor.isAfterLast());
CustomListAdapter adapter=new CustomListAdapter(this, listItems2, listItems);
mylist.setAdapter(adapter);
I want to toggle between visible and invisible with the slide up and slide down animations:
public void toggleContents(View v){
if(txt_hidden.isShown()){
slide_up(this, txt_hidden);
txt_hidden.setVisibility(View.GONE);
}
else{
txt_hidden.setVisibility(View.VISIBLE);
slide_down(this, txt_hidden);
}
//txt_hidden.setVisibility(txt_hidden.isShown()? View.GONE : View.VISIBLE);
}
#Override
public void onDestroy() {
super.onDestroy();
}
public static void slide_down(Context ctx, View v){
Animation a = AnimationUtils.loadAnimation(ctx, R.anim.slide_down);
if(a != null){
a.reset();
if(v != null){
v.clearAnimation();
v.startAnimation(a);
}
}
}
public static void slide_up(Context ctx, View v){
Animation a = AnimationUtils.loadAnimation(ctx, R.anim.slide_up);
if(a != null){
a.reset();
if(v != null){
v.clearAnimation();
v.startAnimation(a);
}
}
}
How do you set the txt_hidden variable? I believe it might be pointing to a view that is different than the one you are actually trying to change the visibility for.
I would suggest that you don't set the click listener in the xml file, but rather in getView() of your adapter when you are building your list item view:
private class ViewHolder
{
TextView mTextView1;
TextView mTextView2
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_view_item_1, parent, false);
viewHolder = new ViewHolder();
viewHolder.mTextView1 = (TextView)convertView.findViewById(R.id.text1);
viewHolder.mTextView2 = (TextView)convertView.findViewById(R.id.text2);
viewHolder.mTextView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do your animation here
}
});
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder)convertView.getTag();
}
viewHolder.mTextView1.setText(mElements1.get(position));
viewHolder.mTextView2.setText(mElements2.get(position));
return convertView;
}
add tag to the view that you set OnClickListener() to, for using animations on another view
...
view.setOnClickListener(this);
view.setTag(textViewToShowOnClick);
...
public void onClick(View v){
...
TexView textViewToShowOnClick = (TextView) v.getTag();
v.slideUp(...);
...
}

custom android listview item not selectable on the whole area

I am using a ListView with a custom adapter for displaying items as pairs of title/subtitle TextViews.
Unfortunately, I can select an item just by clicking on its upper half, the one occupied by the title
Here is the code I am using:
journal_list.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="false"
android:background="#fff"
android:cacheColorHint="#fff"
android:paddingBottom="6dp"
/>
journal_list_item.xml for the list item's layout
<?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="wrap_content"
android:descendantFocusability="blocksDescendants"
>
<TextView
android:id="#+id/journal_entry_date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#000"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
/>
<TextView
android:id="#+id/journal_content"
android:paddingLeft="28dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="16sp"
android:textColor="#555"
android:inputType="textMultiLine"
android:maxLines="2"
android:minLines="1"
android:layout_below="#id/journal_entry_date"
android:layout_alignParentLeft="true"
/>
Also the code for the adapter:
private class JournalAdapter extends ArrayAdapter<JournalEntry> {
Context ctxt;
public JournalAdapter(Context ctxt) {
super(ctxt, R.layout.journal_list_item);
this.ctxt = ctxt;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
JournalHolder holder;
if (row == null) {
row = ((LayoutInflater)ctxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).
inflate(R.layout.journal_list_item, parent, false);
holder = new JournalHolder(row);
row.setTag(holder);
} else {
holder = (JournalHolder) row.getTag();
}
JournalEntry crt = getItem(position);
holder.getDate().setText(crt.dateWritten);
holder.getContent().setText(crt.content);
return row;
}
}
private static class JournalHolder {
private TextView date;
private TextView content;
private View base;
public JournalHolder(View base) {
this.base = base;
}
public TextView getDate() {
if (date == null)
date = (TextView) base.findViewById(R.id.journal_entry_date);
return date;
}
public TextView getContent() {
if (content == null)
content = (TextView) base.findViewById(R.id.journal_content);
return content;
}
}
Code from the ListActivity's onCreate() method:
private JournalAdapter adapter;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.journal_list);
adapter = new JournalAdapter(this);
setListAdapter(adapter);
registerForContextMenu(getListView());
}
Also, I am calling a method called updateList() in onResume()
private void updateList() {
adapter.clear();
Cursor cursor = helper.listJournal(start, end);
cursor.moveToFirst();
JournalEntry crt;
while (!cursor.isAfterLast()) {
crt = new JournalEntry();
crt.dateWritten = cursor.getString(cursor.getColumnIndex("date_written"));
crt.content = cursor.getString(cursor.getColumnIndex("content"));
crt.id = cursor.getInt(cursor.getColumnIndex("entry_id"));
adapter.add(crt);
cursor.moveToNext();
}
cursor.close();
adapter.notifyDataSetChanged();
}
List item clicking is handled in the onListItemClick of my ListActivity, as follows:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = new Intent(this, JournalEditor.class);
intent.setAction(Intent.ACTION_EDIT);
intent.putExtra("entry_id", adapter.getItem(position).id);
startActivity(intent);
}
Apparently, I am forgetting something important, and that's why this whole weird thing happens.
I have found a similar discussion here: http://groups.google.com/group/android-developers/browse_thread/thread/5636c8ea74033657 but it wasn't very helpful.
The problem may be from the clicking of the textviews. When you set the views of the listview items as clickable, they override the click of the listview item and the click will only work when the specific view of the item is clicked.
If this is the case, do the following for your two textviews in the getView() function:
TextView tv;
tv.setFocusable(false);
tv.setClickable(false);
Then set the listview to clickable: listView1.setClickable(true);

Android ListView Custom Adapter ImageButton

This may not be the correct approach, if there is a better way pleas tell me.
I've created a class of Custom Adapter & in my getView method I inflate the view I want to use
public View getView(int position, View convertView, ViewGroup parent)
{
View v = mInflater.inflate(R.layout.wherelayout, null);
if (convertView != null)
{
v = convertView;
}
HashMap<String, Object> whereHash = (HashMap<String, Object>) this.getItem(position);
if (whereHash != null)
{
TextView whereId = (TextView) v.findViewById(R.id.tvWhere);
TextView whereDetails = (TextView) v.findViewById(R.id.tvWhereDetails);
ImageButton ibDelWhere = (ImageButton) v.findViewById(R.id.ibDelWhere);
whereId.setText((CharSequence) whereHash.get("where"));
whereDetails.setText((CharSequence) whereHash.get("details"));
if (ibDelWhere != null)
{
ibDelWhere.setId(position);
ibDelWhere.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
//do stuff when clicked
}
}
);
}
}
return v;
}
The view consists of 2 TextView aligned to the left & an ImageButton aligned to the right, I want to be able to delete the item from the ListView when the button is clicked. the layout is like this -
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal" android:clickable="true">
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="25sp" android:id="#+id/tvWhere" android:textColor="#00FF00" android:text="TextView" android:gravity="top|left" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"></TextView>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/tvWhereDetails" android:textColor="#0000FF" android:text="TextView" android:textSize="18sp" android:layout_below="#+id/tvWhere" android:gravity="bottom|left" android:layout_alignParentLeft="true"></TextView>
<ImageButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="#drawable/eraser" android:id="#+id/ibDelWhere" android:layout_alignParentRight="true" android:layout_alignParentTop="true"></ImageButton>
</RelativeLayout>
The problem is that when the ImageButton is in the layout, I can click it & the onClick() fires as expected, but I can't click the actual list item itself, i.e. click on the TextView items to fire the ListView.onItemClick that was assigned to it already. If I remove the ImageButton from the layout, then the ListView.onItemClick event fires when I click the item. Is there any way I can enable clicking both the ListView item & the button within the layout ?
Thanks guys & gals.
You have to set the imagebutton as non focusable and non focusableInTouchMode (clickable is ok).
Please note, as opposed as other views, you can't do that in xml because the android:focusable gets overwritten in ImageButton's constructor.
To be more precise, that's one of the few differences between ImageView and ImageButton. See for yourself, this is the complete source of ImageButton.
#RemoteView
public class ImageButton extends ImageView {
public ImageButton(Context context) {
this(context, null);
}
public ImageButton(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.imageButtonStyle);
}
public ImageButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setFocusable(true);
}
#Override
protected boolean onSetAlpha(int alpha) {
return false;
}
}
To solve, just call setFocusable(false) from java. Or use an ImageView :)
myImageButton.setFocusable(false);
Hope it helps.
You can make both clickable, but it's not really supported and Romain Guy will yell at you. Also, you won't be able to focus/press the button with the trackball. With that said, you can add the following properties to the button, which should make both clickable:
android:focusable="false"
android:focusableInTouchMode="false"
Just make sure you can live with the consequences.
Try to set
android:clickable="false" on the relative Layout.
I had the same problem with a LinearLayout inside another Linearlayout.
The outer LinearLayout was clickable=true, result:
The ListView.OnItemClickListener does not fire.
After setting it to clickable=false it works.
i.e. click on the TextView items to fire the ListView.onItemClick that was assigned to it already.
What happens when you click on the TextView while the ImageButton is there? Does it register a click on the button? or do nothing at all?
How much space in the row does the ImageButton take up? It could be that it is large enough that you can't click in the row outside of it.
I had this same problem. My solution was to set the onClick method for the view inside the adapter instead of using onItemClick.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null){
LayoutInflater inflater = context.getLayoutInflater();
v = inflater.inflate(R.layout.list_item, parent, false);
}
v.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//This should replace the onListItemClick method
}
});
v.findViewById(R.id.someinnerview).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Write one of these for each of your inner views
}
});
return v;
}
Hope that helps! Depending on the rest of your program, this might force you into handing more data to the adapter (which I had to do) but it works.
here is example of the custom adapter in list view.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="5dip">
<ImageView
android:layout_width="50dip"
android:layout_height="50dip"
android:id="#+id/imgViewLogo"
android:src="#drawable/icon"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:scaleType="center">
</ImageView>
<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_width="wrap_content"
android:id="#+id/txtViewTitle"
android:layout_toRightOf="#+id/imgViewLogo"
android:layout_marginLeft="2dip">
</TextView>
<TextView
android:layout_height="wrap_content"
android:text="TextView"
android:layout_width="wrap_content"
android:id="#+id/txtViewDescription"
android:layout_toRightOf="#+id/imgViewLogo"
android:layout_below="#+id/txtViewTitle"
android:layout_marginLeft="2dip">
</TextView>
<TextView
android:layout_height="wrap_content"
android:text="TextView"
android:layout_width="wrap_content"
android:id="#+id/txtViewMobile"
android:layout_toRightOf="#+id/imgViewLogo"
android:layout_below="#+id/txtViewDescription"
android:layout_marginLeft="2dip">
</TextView>
and make java file On The BaseAdapter
public class ListViewCustomAdapter extends BaseAdapter {
Context context;
String[] mobile;
String[] month;
String[] number;
public LayoutInflater inflater;
public ListViewCustomAdapter(Context context,String[] month, String[] number, String[] mobile) {
// TODO Auto-generated constructor stub
super();
this.context = context;
this.month=month;
this.number=number;
this.mobile=mobile;
Log.i("88888888888888888","*******333********");
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
//ADC71C 95AA1F
public int getCount() {
// TODO Auto-generated method stub
return month.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
private class ViewHolder {
TextView txtViewTitle;
ImageView imgViewLogo;
TextView txtViewDescription;
TextView txtViewMobile;
}
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
Log.i("88888888888888888","*******444********");
ViewHolder holder;
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
if (convertView == null)
{
Log.i("88888888888888888","*******555********");
convertView = inflater.inflate(R.layout.listitem_row, null);
holder = new ViewHolder();
holder.imgViewLogo = (ImageView) convertView.findViewById(R.id.imgViewLogo);
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.txtViewTitle);
holder.txtViewDescription = (TextView) convertView.findViewById(R.id.txtViewDescription);
holder.txtViewMobile = (TextView) convertView.findViewById(R.id.txtViewMobile);
convertView.setTag(holder);
}
else
{
Log.i("88888888888888888","*******666********");
holder = (ViewHolder) convertView.getTag();
}
Log.i("888888888888","Display the value of the textbox like(9856321584)other wise (TextView)");
holder.txtViewTitle.setText(month[position]);
holder.txtViewDescription.setText(number[position]);
holder.txtViewMobile.setText(mobile[position]);
return convertView;
}
}

Categories

Resources