I am having a issue.
listview position 1
button1
listview position 2
button2
This is my designof listview . My adapter is as follow
adapter = new SimpleCursorAdapter(this, R.layout.appareal_listview_text ,cursor, new String[]{"Key_ProductName","Key_SellingPrice"} , new int[] {R.id.title_info_txt_v,R.id.description_info_txt_v});
Now i am giving my xml file appareal_listview_text.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:background="#drawable/layout1"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/title_info_txt_v"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="3dip"
android:layout_marginRight="3dip"
android:textColor="#000000"
android:textSize="15dp"
android:textStyle="bold" />
<TextView
android:id="#+id/description_info_txt_v"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dip"
android:layout_marginLeft="3dip"
android:layout_marginRight="3dip"
android:layout_marginTop="4dip"
android:textColor="#000000"
android:textSize="12dp"
android:textStyle="bold" />
<Button
android:id="#+id/btnBuyNow_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="15dp"
android:background="#drawable/buynow"
android:clickable="true"
android:onClick="buynowClick" />
</LinearLayout>
Now my porblem is that define my button in xml which is call by adapter and button is define only i just once in xml , and i want to perform different task on each click of each position of listview.For that i used
#Override
public void onListItemClick(ListView parent, View view, int position,
long id) {
Intent intent = new Intent(this, asasdad.class);
Cursor cursor = (Cursor) adapter.getItem(position);
intent.putExtra("EMPLOYEE_ID", cursor.getInt(cursor.getColumnIndex("_id")));
startActivity(intent);
System.out.println("list is click");
}
and as well as i can not use this
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// When clicked, show a toast with the TextView text
}
});
So how i will recognize which list button position is clicked to perform different task.
You should use following code.
button.setTag("position");
When button clicked,
String strPos = view.getTag();
Here you will get position of button.
Related
I have get the data from local DB and display it in a ListView and I had a custom row with button
main.xml will contain ListView only
custom-row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
style="#color/background_gradient_start"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:alpha="0.6"
android:background="#color/background_gradient_start"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="177dp"
android:layout_height="75dp"
android:layout_weight="5" >
<TextView
android:id="#+id/textViewFlightNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textViewCodes"
android:layout_alignBottom="#+id/textViewCodes"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+id/textViewCodes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="18dp"
android:background="#color/transparent"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="16dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="142dp"
android:layout_height="match_parent"
android:layout_weight="5">
<Button
android:id="#+id/btn_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:alpha="1"
android:background="#3b3974"
android:focusable="false"
android:focusableInTouchMode="false"
android:minWidth="90dp"
android:text="Share"
android:textColor="#f1ecef" />
</RelativeLayout>
</LinearLayout>
Activity
so under on Create
String [] fromFiledName =new String[]{db.KEY_CODE};
int[] toViewIDs=new int[]{R.id.textViewCodes};
SimpleCursorAdapter myCurAdapter=new SimpleCursorAdapter(
this, //context
R.layout.listview_row_codes,
cursor,
fromFiledName,
toViewIDs
);
// set addapter to list view
ListView mylistPRN=(ListView) findViewById(R.id.codeslist);
mylistPRN.setAdapter(myCurAdapter);
mylistPRN.setAdapter(myCurAdapter);
mylistPRN.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//Intent intent2 = new Intent(this, PromoCodeActivity2.class);
Intent sendIntent = new Intent();
//intent.putExtra(PromoCodeActivity.requestString, httpRequestString);
//getItemAtPosition(position);
Cursor c = (Cursor) parent.getAdapter().getItem(position);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, "share code "+c.getString(c.getColumnIndex("code")));
startActivity(Intent.createChooser(share, "Share Text"));
//startActivity(intentD);
}
});
The code is working and when I click on the list view item it open for sharing but I need it to open when the button is clicked as currently I tried to link the button but I couldn't. Any help
here is what you can do add following two lines to your buttons xml
android:focusable="false"
android:focusableInTouchMode="false"
Then inside adapter getView method attach onclickListener to this button and you are done. Currently there is no listener attached to button in row.
you don't need to use onItemClickListner as you are not clicking on the list Items, rather you have to set the onClickListner on buttons in your getview method
something like this this
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.imagelistlayout, null, true);
Button yourBtn = (Button)rowView.findViewById(R.id.your_btn_id);
yourBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//Do your work here
}
});
return rowView;
}
Also set following properties on your button
android:focusable="false"
android:focusableInTouchMode="false"
ListView:
<ListView android:id="#+id/list11"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:cacheColorHint="#00000000">
</ListView>
custome.xml:
<?xml version="1.0" encoding="utf-8"?>
<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/txt_groupname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:layout_weight="3"
android:text="asdasd"
android:textColor="#color/TextColor" />
<ImageButton
android:id="#+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="#drawable/delete_gp"
android:focusable="false"
android:focusableInTouchMode="false" />
</LinearLayout>
Activity.java:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
long itemValue = listView.getItemIdAtPosition(position);
Log.e("==>", "" + itemValue);
}
});
When I click on the <ImageButton> it works fine, but when I click on <ListView> it does not work.
How can I solve this problem?
ImageButton takes focus when you click on list item
Add the below
android:descendantFocusability="blocksDescendants"
to the root element in custome.xml
You will use this code and it will ask to cast the object you just put the casting after it will fine....
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
long itemValue = parent.getItemIdAtPosition(position);
Log.e("==>", "" + itemValue);
}
});
add android:clickable="true" in the textview and the imagebutton also
I have a list view with single selection mode and would like to get access the position outside the setOnItemClickListener event
As of now I can access its position as shown below
final ListView lv1 = (ListView) findViewById(R.id.list);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
//btnNxt.setEnabled(true);
Intent i = new Intent(getApplicationContext(),
NewActivity.class);
// Pass a single position
i.putExtra("position", position);
// Open SingleItemView.java Activity
startActivity(i);
}
});
Now I would like to know how do I access the selected position outside(ie..on button click event).
btnNxt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Here I need to get the position and selected item
}
});
Listview.xml
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:divider="#3366CC"
android:dividerHeight="2dp"
android:choiceMode="singleChoice"/>
ItemDetails.xml
<?xml version="1.0" encoding="utf-8"?>
<com.example.abc.widget.CheckableRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/sampleimg"
android:layout_width="150dip"
android:layout_height="100dip"
android:paddingRight="15dp"
android:paddingLeft="15dp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="160dp"
android:layout_marginTop="10dp"
android:descendantFocusability="blocksDescendants">
<TextView android:id="#+id/itemname"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/itemdesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/carname"
android:focusable="false"/>
<TextView android:id="#+id/itemprice"
android:textSize="19sp"
android:textStyle="bold"
android:textColor="#003399"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/cardesc"/>
<com.example.abc.widget.InertCheckBox android:id="#+id/itemCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:focusable="false"
android:button="#drawable/checkbox" />
</RelativeLayout>
</com.example.abc.widget.CheckableRelativeLayout>
OrElse how do I check If any listview item is checked or not on button click ?
EDIT:
This is the way I'm trying to access the position from button click
btnNxt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(),
myposition,
Toast.LENGTH_SHORT).show();
}
});
Simply make a member variable (declare it outside of a method like before onCreate() and use that.
public class ...
{
int pos;
// onCreate() and such
final ListView lv1 = (ListView) findViewById(R.id.list);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
pos = position; // initialize it
...
}
});
then use pos inside your onClick() or wherever you need to in the Activity.
If I completely missed something in your question then please explain.
I want to access the child of a ListView at position x. The ListView has the following xml layout code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#drawable/bg_grad_iled">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/iv_icon" />
<TextView android:id="#+id/tv_name"
android:textColor="#color/kategorieTextColor"
android:layout_height="40dp"
android:textSize="16sp"
android:layout_weight=".50"
android:layout_width="0dip"
android:padding="3px"
android:layout_marginLeft="8px"/>
<TextView android:id="#+id/tv_comment"
android:textColor="#color/kategorieTextColor"
android:layout_height="40dp"
android:textSize="16sp"
android:layout_weight=".20"
android:layout_width="0dip"
android:padding="3px"/>
<TextView android:id="#+id/tv_distance"
android:textColor="#color/kategorieTextColor"
android:layout_height="40dp"
android:textSize="16sp"
android:layout_weight=".30"
android:layout_width="0dip"
android:padding="3px"/>
</LinearLayout>
How can I access, (e.g.) the textview 'tv_name' in the 5th child of the ListView?
Thats my current listener:
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(context, "Clicked", 1000).show();
}
});
Perhaps the adapter I use may be important? Refer this
when you click on the list item OnItemClickListener will return the view, so that you can get access through that view ..
Eg:
if you want to get text of tv_name at position 5 simply:
public void onItemClick(AdapterView<?> arg0, View v, final int position,long arg3)
{
TextView text=(TextView)v.findViewById(R.id.tv_name);
Toast.makeText(this, text.getText(), Toast.LENGTH_LONG).show();
}
});
try it and get me the feedback
i tryied to custom my ListView , so i use this item layout 「 list_item.xml」
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="10dp"
android:textSize="16dip"
android:textColor="#000000"
android:gravity="center_vertical"
android:background="#FFFFFF"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_weight="1">
</TextView>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0"
android:background="#FFFFFF"
android:padding="10dip"
android:gravity="center_vertical">
<ImageView
android:id="#+id/pause"
android:src="#drawable/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="pauseBtn_onClick"
/>
<ImageView
android:id="#+id/play"
android:src="#drawable/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="playBtn_onClick"/>
</RelativeLayout>
</LinearLayout>
just as what you saw, there are 2 images in this layout, what i wanna do is,
when user click any item , the play_icon(the second image in the listview item) , will
disappear.
so i write code as the following :
mp3_listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View _view, int index , long arg3)
{
String _url = musicList.get(index).get("link").toString();
LinearLayout _container = (LinearLayout)mp3_listView.getChildAt(index);
RelativeLayout _container2 = (RelativeLayout) _container.getChildAt(1);
ImageView img = (ImageView)_container2.getChildAt(1);
img.setVisibility(View.INVISIBLE);
Toast.makeText(tabDigTest.this , "GG : "+_container.findViewById(R.id.play) , Toast.LENGTH_SHORT).show();
}
}
but something strange occurred .... more than one item's img be hidden because of the:
img.setVisibility(View.INVISIBLE);
i don't why because i only set one img to INVISIBLE ......i think i need help O_o
To retrieve the user click, I guess you set up an OnItemClickListener.
ListView lv = (fecth your list vieW)
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//you might want to use 'view' here
}
});
In the onItemClick method, the view parameter is the view you returned in your ListAdapter's getView method. So if say, you have returned a LinearLayout containing three childrens with id ("textview", "imageview1", "imageview2"), you would simply call view.findViewById(R.id.imageview1) to retrieve the first ImageView.