AutoCompleteTextView Items not Clickable when containing custom view like two textview - android

This is custom View layout.xml
<?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="25dp"
android:background="#drawable/newbackrow"
android:clickable="true"
android:orientation="vertical" >
<TextView
android:id="#+id/txt_search_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:layout_toLeftOf="#+id/txt_search_sport"
android:layout_weight="1"
android:focusable="false"
android:inputType="textMultiLine"
android:maxLines="2"
android:singleLine="false"
android:text="TextView"
android:textColor="#android:color/white"
android:textSize="13dp"
android:textStyle="bold" />
<TextView
android:id="#+id/txt_search_sport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:focusable="false"
android:layout_marginLeft="10dp"
android:background="#color/red"
android:padding="0dp"
android:text="TextView"
android:textColor="#android:color/white"
android:textSize="10dp"
android:textStyle="bold" />
</RelativeLayout>
// layour inflate in getview() methode
view perfectly dispay but its not clickable ,let me suggest where going to wrong or any other
** // autocompletetextview OnItemclicklistener**
search_text.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View myView, int position,
long arg3) {
Toast.makeText(getApplicationContext(), "Position = "+position, Toast.LENGTH_LONG).show();
}
}

Related

Handle Button Clicks in CursorAdapter

I want to achieve drop down and sum up for a View in my ListView Data is being fetched from the database and am using a cursor adapter, I have tried to add onClickListner for the button in onItemClickListner But no Success.
The problem I am facing is When button DROPDOWN is clicked it will make relative layout Visible but when clicked again it won't relative layout visibility to GONE
Heres my Code for CursorAdapter
public class ProductViewCursorAdapter extends CursorAdapter {
private CardView cv_singleItem;
private RelativeLayout infoLayout;
private Button dropDown;
public ProductViewCursorAdapter(Context context, Cursor c) {
super(context, c, 0);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.custom_product_view_listview,parent,false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
cv_singleItem = view.findViewById(R.id.cv_custom_single_product);
infoLayout = view.findViewById(R.id.layout_dropdown_relative);
dropDown = view.findViewById(R.id.expand_user_info_listView);
dropDown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (infoLayout.getVisibility() == View.GONE){
TransitionManager.beginDelayedTransition(cv_singleItem,new AutoTransition());
infoLayout.setVisibility(View.VISIBLE);
dropDown.setBackgroundResource(R.drawable.ic_sumup);
}else{
Log.i("THIS IS TEST","DEMO VIEW "+ infoLayout.getVisibility());
TransitionManager.beginDelayedTransition(cv_singleItem,new AutoTransition());
infoLayout.setVisibility(View.GONE);
dropDown.setBackgroundResource(R.drawable.ic_dropdown);
}
}
});
}
}
ListView single View XML
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:id="#+id/cv_custom_single_product"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/TextAppearance.AppCompat.Title">
<LinearLayout
android:background="#D5000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/image_layout_main"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/iv_product_image_listView"
android:layout_width="match_parent"
android:scaleType="fitCenter"
android:src="#drawable/selimage"
android:layout_height="200dp"/>
<TextView
android:id="#+id/tv_no_image_available"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No Image Available"
android:textAlignment="center"
android:paddingTop="5dp"
android:textSize="15sp"
android:textStyle="bold|italic"
android:textColor="#color/white"/>
<TextView
android:id="#+id/tv_product_name_listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kurti"
android:textSize="18sp"
android:textColor="#FFFFFF"
android:fontFamily="sans-serif-black"
android:textStyle="italic"
android:layout_marginLeft="20dp"
android:layout_below="#id/iv_product_image_listView"
android:layout_marginTop="-25dp"/>
<Button
android:id="#+id/expand_user_info_listView"
android:layout_alignParentEnd="true"
android:layout_below="#id/iv_product_image_listView"
android:focusable="false"
android:layout_marginTop="-49dp"
android:layout_marginEnd="8dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/ic_dropdown"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/layout_dropdown_relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<TextView
android:id="#+id/tv_date_dropdown_listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2020-12-14"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#E2FFFFFF"
android:fontFamily="sans-serif-condensed"
android:textStyle="bold"
android:layout_marginStart="4dp"/>
<TextView
android:id="#+id/tv_profit_dropdown_listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profit 500"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#E2FFFFFF"
android:fontFamily="sans-serif-condensed"
android:textStyle="bold"
android:layout_marginStart="4dp"
android:layout_alignParentEnd="true"/>
<TextView
android:id="#+id/tv_sellingPrice_dropdown_listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selling Price: 800"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#E2FFFFFF"
android:fontFamily="sans-serif-condensed"
android:textStyle="bold"
android:layout_marginStart="4dp"
android:layout_below="#id/tv_date_dropdown_listView"
android:paddingTop="3dp"/>
<TextView
android:id="#+id/tv_actualPrice_dropDown_listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Actual Price: 850"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#E2FFFFFF"
android:fontFamily="sans-serif-condensed"
android:textStyle="bold"
android:layout_marginStart="4dp"
android:layout_alignParentRight="true"
android:layout_below="#id/tv_profit_dropdown_listView"
android:paddingTop="3dp"/>
<TextView
android:id="#+id/tv_pending_dropdown_listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pending: 0"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#F50057"
android:fontFamily="sans-serif-condensed"
android:textStyle="bold"
android:layout_marginStart="4dp"
android:layout_below="#id/tv_sellingPrice_dropdown_listView"
android:paddingTop="3dp"/>
<Button
android:id="#+id/btn_update_single_item"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_below="#id/tv_actualPrice_dropDown_listView"
android:layout_alignParentEnd="true"
android:background="#drawable/ic_update"/>
</RelativeLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
You are using the same infoLayout for every list view row. Every time bindView() gets called you replace the infoLayout with new layout. This of course causes the problem that when you use infoLayout.setVisibility(View.VISIBLE); the infoLayout it's not the same layout it was when the list view row was created because you have replaced it.
So replace,
infoLayout = view.findViewById(R.id.layout_dropdown_relative);
with
final RelativeLayout infoLayout = view.findViewById(R.id.layout_dropdown_relative);

Android custom ListView item setOnItemClickListener not working

I have a custom ListView item. setOnClickListener is not working. I've been searching for the solution for hours, but couldn't find anything. I think the problem is not 'set focusable false' things, because I've already done it. Please, help me.
payments_list_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:focusable="false"
android:focusableInTouchMode="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="12dp"
android:paddingLeft="10dp">
<ImageView
android:layout_width="46dp"
android:layout_height="35.3dp"
android:textColor="#000000"
android:id="#+id/payment_item_name2"
android:layout_gravity="center_horizontal|start"
android:src="#drawable/payments_internet"
android:textSize="20sp"
android:text="Test"
android:textStyle="bold"
android:capitalize="words" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#8c8c8c"
android:id="#+id/payment_item_name"
android:layout_gravity="center_horizontal|center"
android:textSize="20sp"
android:layout_marginLeft="10dp"
android:text="Test"
android:textStyle="bold"
android:capitalize="words" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="8dp"
android:layout_marginTop="12dp"
android:paddingRight="10dp">
<ImageView
android:layout_width="15.3dp"
android:layout_height="20.1dp"
android:src="#drawable/arrow_right_red"
android:layout_gravity="center_vertical|right"
android:id="#+id/backButton"
android:textAllCaps = "true"
/>
</LinearLayout>
Here is my Activity onCreate:
ListView paymentsList = (ListView) findViewById(R.id.paymentsList);
paymentsList.setAdapter(testAdapter);
paymentsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.w("Clicked23", "CLicked23");
}
});
`
My Adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View payments_list_item = inflater.inflate(R.layout.payments_list_item, parent, false);
String payment = getItem(position);
TextView payment_item_name = (TextView) payments_list_item.findViewById(R.id.payment_item_name);
payment_item_name.setText(payment.toUpperCase());
return payments_list_item;
}
//EDIT
Finally solved my problem
Thanks to #mustafasevgi`s answer:
I added
android:descendantFocusability="blocksDescendants" to my custom ListView item layout.
Can you add this code?
android:descendantFocusability="blocksDescendants"
payment_list_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:descendantFocusability="blocksDescendants">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="12dp"
android:paddingLeft="10dp">
<ImageView
android:layout_width="46dp"
android:layout_height="35.3dp"
android:textColor="#000000"
android:id="#+id/payment_item_name2"
android:layout_gravity="center_horizontal|start"
android:src="#drawable/payments_internet"
android:textSize="20sp"
android:text="Test"
android:textStyle="bold"
android:capitalize="words" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#8c8c8c"
android:id="#+id/payment_item_name"
android:layout_gravity="center_horizontal|center"
android:textSize="20sp"
android:layout_marginLeft="10dp"
android:text="Test"
android:textStyle="bold"
android:capitalize="words" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="8dp"
android:layout_marginTop="12dp"
android:paddingRight="10dp">
<ImageView
android:layout_width="15.3dp"
android:layout_height="20.1dp"
android:src="#drawable/arrow_right_red"
android:layout_gravity="center_vertical|right"
android:id="#+id/backButton"
android:textAllCaps = "true"
/>
</LinearLayout>
check the following code:
public class CustomListActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
ListView paymentsList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_list);//your layout view
paymentsList = (ListView) findViewById(R.id.paymentsList);
paymentsList.setAdapter(testAdapter);
paymentsList.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//your logic
Toast.makeText(this, "Listview Clicked", Toast.LENGTH_SHORT).show();
}
}
Try this:
paymentsList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.w("Clicked23", "CLicked23");
}
});
Remove this from your xml
android:focusable="false"
android:focusableInTouchMode="false"
how about modifying your payments_list_item to this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingBottom="12dp"
android:paddingTop="12dp">
<ImageView
android:id="#+id/payment_item_name2"
android:layout_width="46dp"
android:layout_height="35.3dp"
android:src="#drawable/payments_internet"/>
<TextView
android:id="#+id/payment_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="Test"
android:textColor="#8c8c8c"
android:textSize="20sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/backButton"
android:layout_width="15.3dp"
android:layout_height="20.1dp"
android:layout_gravity="center_vertical|right"
android:src="#drawable/arrow_right_red"/>
</LinearLayout>
you have written very complex layout..

ListView OnItemClickListener not working on second adapter

I have a fragment with a ListView and two buttons above this ListView. The two buttons call the following methods which reload the ListView with a different adapter for the data source. The problem is with the second one. When calling the showWhenWhere(), the onItemClick() is never fired.
private void showAtoZ() {
lineupAdapter = new LineupAdapter(parentView.getContext());
listView.setAdapter(lineupAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// ListView Clicked item index
System.out.println("A to Z tapped");
int itemPosition = position;
if(parentActivity != null) {
parentActivity.changeFragment(new BandFragment());
}
}
});
loadData();
}
private void showWhenWhere() {
whenWhereAdapter = new LineupWhenWhereAdapter(parentView.getContext());
listView.setAdapter(whenWhereAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// ListView Clicked item index
System.out.println("When/Where tapped");
int itemPosition = position;
if(parentActivity != null) {
parentActivity.changeFragment(new BandFragment());
}
}
});
loadData();
}
UPDATE - I determined it's caused by the XML for the whenWhereAdapter, which I'm pasting below:
<?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="150dp" android:id="#+id/lineupWhenWhereView">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/bandImageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:scaleType="centerCrop"
android:background="#android:color/darker_gray" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:id="#+id/textView"
android:background="#drawable/list_row_bg" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Elijah & The Moon"
android:id="#+id/bandTitle"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginBottom="30dp"
android:textSize="23sp"
android:maxLines="2"
android:textColor="#android:color/white" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="25dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#android:color/black">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12:00 PM"
android:id="#+id/whenLabel"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:textColor="#android:color/holo_orange_light"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="|"
android:id="#+id/divider"
android:layout_toRightOf="#+id/whenLabel"
android:layout_toEndOf="#+id/whenLabel"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginBottom="6dp"
android:layout_alignParentBottom="true"
android:textIsSelectable="true"
android:textSize="13sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="West Stage"
android:id="#+id/whereLabel"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/divider"
android:layout_toEndOf="#+id/divider"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:textSize="12sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD TO SCHEDULE"
android:id="#+id/addButton"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/add_to_schedule"
android:drawablePadding="5dp"
android:textColor="#android:color/holo_orange_light"
android:layout_marginRight="10dp" />
</RelativeLayout>
</RelativeLayout>
Any idea why this would cause the OnItemClickListener to not work?
ListView only maintains one OnItemClickListener at time. When you set the second listener, only that listener will be called.
-------EDIT -------
After seeing your row layout, I think you should set
android:descendantFocusability="blocksDescendants"
on your very first RelativeLayout.
That happen because TextViews && Buttons inside layout are blocking click event.

OnItemClickListener not work

I have a listview. And i need do click in each element inside listview. But the onItemClickListener don't work. I try put each textView with focusable="false" and put android:descendantFocusability="blocksDescendants" but nothing works and i don't know where is my error.
Here is my code
xml of each element inside listview
**xml_item_list**
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="5dip"
android:paddingBottom="8dip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:background="#android:drawable/list_selector_background"
android:clickable="true"
android:longClickable="true"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="#+id/lDate"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginRight="3dp"
android:background="#drawable/et_style"
android:gravity="center"
android:textColor="#c2c2c2"
android:textAppearance="?android:attr/textAppearanceMedium"
android:focusable="false" />
<TextView
android:id="#+id/lDescription"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_weight="1"
android:gravity="center"
android:background="#drawable/et_style"
android:textColor="#c2c2c2"
android:textAppearance="?android:attr/textAppearanceMedium"
android:focusable="false"/>
<TextView
android:id="#+id/lMissValue"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:gravity="center_horizontal|center"
android:layout_gravity="center"
android:textColor="#DDCC2EFA"
android:textAppearance="?android:attr/textAppearanceMedium"
android:focusable="false"/>
</TableRow>
**main_xml_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:background="#drawable/background_tile"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="50dp">
<ImageView
android:id="#+id/bankTransaction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"/>
<TextView
android:id="#+id/contextLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/et_style"
android:textSize="28dp"
android:layout_gravity="bottom" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
</ListView>
</LinearLayout>
Code inside onCreate() function of activity
onCreate()
listView = getListView();
listView.setClickable(true);
listView.setOnItemClickListener(
new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View view,int position, long id) {
Log.w(TransactionView.class.getName(), "Item click listener");
Toast.makeText(TransactionView.this, "Item clicado", Toast.LENGTH_LONG).show();
}
}
);
Anyone can help me?
Thanks for yout time.
try like this
lvlList = (ListView)findViewById(R.id.lvlList);
lvlList.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> a, View v,int position, long id)
{
Toast.makeText(getBaseContext(), "Click", Toast.LENGTH_LONG).show();
}
});
First of all,check that you have added this line or not in your code.
public class MyActivity extends Activity implements OnItemClickListener
Then add this code in your onCreate() of activity ...
ListView listView = (ListView) findViewById(R.id.lisview);
listView.setOnItemClickListener(this);
#Override
public boolean onItemClick(AdapterView<?> parent, View v, int pos,long id)
{
System.out.println("~~~~~~Item Clicked");
return false;
}
change this ::
android:id="#+id/list"
Hope this helps :)
My Solution.....
public class TransactionView extends ListActivity implements **AdapterView.OnItemClickListener**{}
**onCreate()**{
adapter = new SimpleCursorAdapter(
this,
R.layout.transaction_item_view,
c,
new String[]{MySQLiteHelper.C_TRANSACTION_DUE, MySQLiteHelper.C_TRANSACTION_DESCRIPTION, MySQLiteHelper.C_TRANSACTION_VALUE, MySQLiteHelper.C_TRANSACTION_COVERVALUE, MySQLiteHelper.C_TRANSACTION_STATE, MySQLiteHelper.C_TRANSACTION_SOURCE},
new int[]{ R.id.lDate, R.id.lDescription, R.id.lMissValue},SimpleCursorAdapter.IGNORE_ITEM_VIEW_TYPE);
}
setListAdapter(adapter);
listView = getListView();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View view,int position, long id) {
Log.w(TransactionView.class.getName(), "Item click listener");
Toast.makeText(TransactionView.this, "Item clicado", Toast.LENGTH_LONG).show();
}
}
);
**item_list_row**
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="5dip"
android:paddingBottom="8dip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:background="#android:drawable/list_selector_background"
>
<TextView
android:id="#+id/lDate"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginRight="3dp"
android:background="#drawable/et_style"
android:gravity="center"
android:textColor="#c2c2c2"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<TextView
android:id="#+id/lDescription"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_weight="1"
android:gravity="center"
android:background="#drawable/et_style"
android:textColor="#c2c2c2"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<TextView
android:id="#+id/lMissValue"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:gravity="center_horizontal|center"
android:layout_gravity="center"
android:textColor="#DDCC2EFA"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
**main_xml_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:background="#drawable/background_tile"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>

OnItemCLickLIstener doesn't work on ListView

I have an activity with a ListView. ListView with custom views. I add OnItemClickLIstener to the ListView. and when i click on item, in result i see nothing.
Activity with ListView:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/silver_conv">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="40dp" android:layout_alignParentTop="true" android:id="#+id/topcontainer"
android:background="#color/black">
</FrameLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/chat_list" android:layout_below="#+id/topcontainer"
android:layout_above="#+id/last_action"
android:cacheColorHint="#00000000"
android:layout_marginRight="2dp" android:clickable="true"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="last action was at time" android:id="#+id/last_action"
android:longClickable="false"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="false" android:layout_above="#+id/action_container"
android:layout_marginBottom="5dp" android:layout_alignParentLeft="false" android:layout_marginLeft="5dp"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="43dp" android:layout_alignParentBottom="true" android:id="#+id/action_container"
android:background="#drawable/conv_botom_action_gradient">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="#string/send"
android:id="#+id/send_message_btn" android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:background="#drawable/blue_button_selector" android:textColor="#color/white"
android:layout_marginLeft="3dp" android:layout_marginTop="3dp"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/add_attach_btn"
android:layout_alignParentLeft="true" android:layout_alignParentBottom="true"
android:background="#drawable/add_attach_button_selector"
android:layout_marginRight="2dp" android:layout_marginBottom="3dp" android:layout_marginTop="3dp"/>
<EditText android:layout_width="40dp" android:layout_height="wrap_content" android:id="#+id/message_et"
android:layout_toRightOf="#+id/add_attach_btn" android:layout_toLeftOf="#+id/send_message_btn"
android:singleLine="true" android:layout_alignParentBottom="true" android:hint="Type message here"
android:background="#drawable/message_input" android:layout_marginBottom="3dp"
android:gravity="center_vertical" android:layout_marginTop="3dp"/>
</RelativeLayout>
View item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:gravity="center_vertical|left" android:focusable="false">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/incoming_message"
android:id="#+id/container" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"
android:focusable="false">
<FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
android:focusableInTouchMode="true" android:id="#+id/attach_container" android:focusable="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="saa"
android:id="#+id/message_text" android:textSize="17sp" android:textColor="#color/black"
android:focusable="false"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/date" android:textColor="#color/black" android:singleLine="true" android:lines="1"
android:maxLines="1" android:ellipsize="none" android:layout_marginLeft="5dp" android:focusable="false"/>
And finally clickListener:
private AdapterView.OnItemClickListener clickLister = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
try {
LinearLayout container = (LinearLayout)view.findViewById(R.id.container);
TextView message = (TextView)container.findViewById(R.id.message_text);
message.setTextColor(mContext.getResources().getColor(R.color.white));
Log.e("My item is", "" + pos);
}catch (Exception e){
e.printStackTrace();
}
}
};
And here is Initialization of ListView:
mConvListView = (ListView)findViewById(R.id.chat_list);
mConvListView.setDivider(null);
mConvListView.setDividerHeight(0);
mConvListView.setItemsCanFocus(false);
mConvListView.setOnItemClickListener(clickLister);
mConvListView.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
mConvListView.setStackFromBottom(true);
P.s. Sorry for a lot code. But I can't find any suggestion a second day.
By setting focusable objects in your row layout, you are preventing the ListView from getting the touch event.
This FrameLayout is consuming the touch event:
<FrameLayout
android:id="#+id/attach_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:focusable="false"/>
Remove the focusable settings so it looks like this:
<FrameLayout
android:id="#+id/attach_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
(You really should organize your XML so that is is readable, in Eclipse use Ctrl+Shift+F.)
Make Focus for all components as follows :
android:focusable="false"
android:focusableInTouchMode="false"
I guess you are not getting the item the right way. Try this:
int position = (int) adapterView.getSelectedItemId();
Log.i("Position:", Integer.toString(position));
Edit
Try this piece of code.
ListView lv = (ListView)findViewById(R.id.chat_list);
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
int position = (int) parent.getSelectedItemId();
Log.i("Position:", Integer.toString(position));
}
});

Categories

Resources