Capturing clicks in AdapterView - how to get them?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Capturing clicks in AdapterView - how to get them?

Postby floda » Thu Sep 17, 2009 8:31 pm

Hi,

i have an AdapterView consisting of a button, a text and edittext in one row on the screen and i wonder what i need to do to capture clicks on them. I tried the following with setOnItemClickListener but my Log remains empty. Do i need to do something in the Adapterclass (see below)?

Any feedback is welcome.

Thanks in advance.


AdapterTest.java

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. package org.rice;
  5.  
  6.  
  7.  
  8. import android.app.Activity;
  9.  
  10. import android.view.View;
  11.  
  12. import android.widget.*;
  13.  
  14. import android.os.Bundle;
  15.  
  16. import java.util.*;
  17.  
  18. import android.widget.AdapterView.OnItemClickListener;
  19.  
  20. import android.util.*;
  21.  
  22.  
  23.  
  24. public class AdapterTest extends Activity {
  25.  
  26.         private ArrayList<FuItem> fItems;
  27.  
  28.         private ArrayAdapter<FuItem> aa;
  29.  
  30.         private ListView myListView;
  31.  
  32.        
  33.  
  34.         private void fillit(){
  35.  
  36.                 fItems = new ArrayList<FuItem>();
  37.  
  38.                
  39.  
  40.                 for(int i = 0; i < 5; i++){
  41.  
  42.                         FuItem fi = new FuItem(String.valueOf(i), (float)i);
  43.  
  44.                         fItems.add(fi);
  45.  
  46.                 }
  47.  
  48.         }
  49.  
  50.     /** Called when the activity is first created. */
  51.  
  52.     @Override
  53.  
  54.     public void onCreate(Bundle savedInstanceState) {
  55.  
  56.         super.onCreate(savedInstanceState);
  57.  
  58.         setContentView(R.layout.main);
  59.  
  60.        
  61.  
  62.         myListView = (ListView)findViewById(R.id.liste);
  63.  
  64.        
  65.  
  66.         fillit();
  67.  
  68.        
  69.  
  70.         int resId = R.layout.row;
  71.  
  72.         aa = new FuItemAdapter(this, resId, fItems);
  73.  
  74.        
  75.  
  76.         myListView.setAdapter(aa);
  77.  
  78.        
  79.  
  80.         myListView.setOnItemClickListener(new OnItemClickListener() {
  81.  
  82.  
  83.  
  84.                         @Override
  85.  
  86.                         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
  87.  
  88.                                         long arg3) {
  89.  
  90.                                 // TODO Auto-generated method stub
  91.  
  92.                                 Log.i("adaptertest", "onItemClick fired");
  93.  
  94.                         }
  95.  
  96.                
  97.  
  98.         });
  99.  
  100.     }
  101.  
  102. }
  103.  
  104.  
  105.  
  106.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


The Adapterclass is here:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package org.rice;
  3.  
  4.  
  5.  
  6. import android.view.*;
  7.  
  8. import android.widget.*;
  9.  
  10. import android.content.Context;
  11.  
  12. import java.util.*;
  13.  
  14.  
  15.  
  16. public class FuItemAdapter extends ArrayAdapter<FuItem>{
  17.  
  18.  
  19.  
  20.         int resource;
  21.  
  22.        
  23.  
  24.         public FuItemAdapter(Context _context, int _resource, List<FuItem> _items) {
  25.  
  26.                 super(_context, _resource, _items);
  27.  
  28.                 resource = _resource;
  29.  
  30.         }
  31.  
  32.        
  33.  
  34.         @Override
  35.  
  36.         public View getView(int position, View convertView, ViewGroup parent){
  37.  
  38.                 LinearLayout fuitemView;
  39.  
  40.                
  41.  
  42.                 FuItem item = getItem(position);
  43.  
  44.                
  45.  
  46.                 String productString = item.getProduct();
  47.  
  48.                 String priceString = String.valueOf(item.getPrice());
  49.  
  50.                
  51.  
  52.                 if(convertView == null){
  53.  
  54.                         fuitemView = new LinearLayout(getContext());
  55.  
  56.                         String inflater = Context.LAYOUT_INFLATER_SERVICE;
  57.  
  58.                         LayoutInflater vi;
  59.  
  60.                         vi = (LayoutInflater)getContext().getSystemService(inflater);
  61.  
  62.                         vi.inflate(resource, fuitemView, true);
  63.  
  64.                 }
  65.  
  66.                 else {
  67.  
  68.                         fuitemView = (LinearLayout) convertView;
  69.  
  70.                 }
  71.  
  72.                
  73.  
  74.                 TextView productView = (TextView)fuitemView.findViewById(R.id.product);
  75.  
  76.                 EditText priceView = (EditText)fuitemView.findViewById(R.id.price);
  77.  
  78.                
  79.  
  80.                 productView.setText(productString);
  81.  
  82.                 priceView.setText(priceString);
  83.  
  84.                
  85.  
  86.                 return fuitemView;
  87.  
  88.         }
  89.  
  90. }
  91.  
  92.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
floda
Freshman
Freshman
 
Posts: 5
Joined: Thu Sep 17, 2009 8:20 pm

Top

Re: Capturing clicks in AdapterView - how to get them?

Postby flyer » Sat Sep 19, 2009 2:37 am

i got same problem , i remebered i just change the Button focusabled
yourBtn.setFoucsable(false) or yourBtn.setFouce(false);
you can do a try !
your button still can get the onclick listener even if the button didn't get the fouces
flyer
Freshman
Freshman
 
Posts: 8
Joined: Mon Jan 14, 2008 7:07 am

Re: Capturing clicks in AdapterView - how to get them?

Postby floda » Sat Sep 19, 2009 9:12 pm

Hi and thanks for the reply. Unfortunately onItemClickListener does not fire. A workaround is the registration of onClickListeners for the Views in the AdapterView. But i don't like to put app-logic in there.

What a waste of time. Once i figure it out i'll write a piece of documentation.

flyer wrote:i got same problem , i remebered i just change the Button focusabled
yourBtn.setFoucsable(false) or yourBtn.setFouce(false);
you can do a try !
your button still can get the onclick listener even if the button didn't get the fouces
floda
Freshman
Freshman
 
Posts: 5
Joined: Thu Sep 17, 2009 8:20 pm

Re: Capturing clicks in AdapterView - how to get them?

Postby flyer » Mon Sep 21, 2009 5:09 am

public View getView(int position, View convertView, ViewGroup parent){
LinearLayout fuitemView;

FuItem item = getItem(position);

String productString = item.getProduct();
String priceString = String.valueOf(item.getPrice());

if(convertView == null){
fuitemView = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater vi;
vi = (LayoutInflater)getContext().getSystemService(inflater);
vi.inflate(resource, fuitemView, true);
}
else {
fuitemView = (LinearLayout) convertView;
}

TextView productView = (TextView)fuitemView.findViewById(R.id.product);
EditText priceView = (EditText)fuitemView.findViewById(R.id.price);

productView.setText(productString);
priceView.setText(priceString);

return fuitemView;
}


is your button set setOnClickListener????

TextView productView = (TextView)fuitemView.findViewById(R.id.product);
EditText priceView = (EditText)fuitemView.findViewById(R.id.price);


is your button set setOnClickListener????
Button btn=(Button)fuitemView.findViewById(R.id.btn);
btn.setFocusable(false);
btn.setOnClickListener( new YourListener() );




productView.setText(productString);
priceView.setText(priceString);
flyer
Freshman
Freshman
 
Posts: 8
Joined: Mon Jan 14, 2008 7:07 am

Re: Capturing clicks in AdapterView - how to get them?

Postby floda » Mon Sep 21, 2009 8:00 am

Hi flyer,

in the meantime i have Listeners for the row items (button, edittext) registered in the FuItemAdapter. They fire, but on the OnItemClickListener it is very quiet.

The code with these listeners is there:
http://groups.google.com/group/android- ... a83259ecaa

Thanks again and best,

floda

flyer wrote:public View getView(int position, View convertView, ViewGroup parent){
LinearLayout fuitemView;

FuItem item = getItem(position);

String productString = item.getProduct();
String priceString = String.valueOf(item.getPrice());

if(convertView == null){
fuitemView = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater vi;
vi = (LayoutInflater)getContext().getSystemService(inflater);
vi.inflate(resource, fuitemView, true);
}
else {
fuitemView = (LinearLayout) convertView;
}

TextView productView = (TextView)fuitemView.findViewById(R.id.product);
EditText priceView = (EditText)fuitemView.findViewById(R.id.price);

productView.setText(productString);
priceView.setText(priceString);

return fuitemView;
}


is your button set setOnClickListener????

TextView productView = (TextView)fuitemView.findViewById(R.id.product);
EditText priceView = (EditText)fuitemView.findViewById(R.id.price);


is your button set setOnClickListener????
Button btn=(Button)fuitemView.findViewById(R.id.btn);
btn.setFocusable(false);
btn.setOnClickListener( new YourListener() );




productView.setText(productString);
priceView.setText(priceString);
floda
Freshman
Freshman
 
Posts: 5
Joined: Thu Sep 17, 2009 8:20 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 7 guests