I've seen a few examples on here to this problem, but none have solved this issue, observe the code below:
mProductList = hotdealhelper.getCatalog(getResources());
ListView myListView = (ListView) findViewById(R.id.listViewoffers);
myListView.setAdapter(new hotdealadapter(mProductList, getLayoutInflater()));
myListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
System.out.println("Newbie");
}});
I have it in my oncreate lethod, and when I click on a particular position in the list, it is not printing the system.out. I am wondering why this is, I have used this exact code in another class and it works fine. I have the list in a activity class also. Below is the xml if it helps.
<ImageView android:id="#+id/imageView1" android:src="#drawable/dealheader" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true"></ImageView>
<ListView android:layout_height="wrap_content" android:id="#+id/listViewoffers" android:layout_width="match_parent" android:layout_marginTop="35px"></ListView>
<Button android:text="Button" android:id="#+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="0px" android:layout_alignParentBottom="true"></Button>
You may have forgotten the #Override
myListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
System.out.println("Newbie");
}});
EDIT
Maybe you're seeing this bug http://code.google.com/p/android/issues/detail?id=3414 if you have a checkbox or something in your listview. remove the button and see if it works... if it does that was the problem.
Remove System.out.println
Try with Toast.makeText(this, "Hello", Toast.LENGTH_SHORT) to check if click listner works
Related
When a Fragment is inflated, by default I want to item no 10 to be selected.
I tried it with gridview.setSelection(10); but it is not working for me. I read in the forum that others have done it successfully , but I am not achieving the correct output. what should I do?
orari_fine = (GridView) findViewById(R.id.gridView4);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, orari_f);
orari_fine.setAdapter(adapter1);
orari_fine.setSelection(10);
orari_fine.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
OraFine = ((TextView) v).getText().toString();
}
});
XML
<GridView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:listSelector="#drawable/selector"
android:id="#+id/gridView4"
android:background="#9051585B"
android:choiceMode="singleChoice"
android:numColumns="8"
android:textAlignment="center"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_below="#+id/textView13" />
instead of using gridview.setSelection(10); try using gridview.setItemChecked(10, true);
try do this
// set item postion you wanna select
orari_fine.setSelection(10);
orari_fine.setFocusableInTouchMode(true);
orari_fine.requestFocus();
// Notifies the attached observers that the underlying data has been changed
adapter1.notifydatasetchanged();
I want to get the text of a TextView that is in a ListView's item.
See the code and you will find what's my question.
Activity.java:
SimpleAdapter adapter = new SimpleAdapter(
rootView.getContext(),
result,
R.layout.article_list_item,
new String[]{"article_title", "article_PubDate", "article_category", "article_articleNo"},
new int[]{R.id.article_title, R.id.article_PubDate, R.id.article_category, R.id.article_articleNo});
ListView articleItem.setAdapter(adapter);
articleItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//I want get the text of R.id.article_articleNo(see article_list_item.xml)?
//What should I do?
}
});
article_list_item.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:orientation="vertical"
android:id="#+id/article">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文章标题"
android:id="#+id/article_title"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="30-Apr-2014"
android:id="#+id/article_PubDate"
android:layout_gravity="center_horizontal"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="personal_finance"
android:id="#+id/article_category"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9312"
android:id="#+id/article_articleNo"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="20dp"/>
</LinearLayout>
</LinearLayout>
That's all!!
I want to get the text of R.id.article_article (see article_list_item.xml).
How can I do it?
Does this work for you?
articleItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String text = result.get(position).get("article_articleNo");
//^^ does not necessarily need to be a string,
// make this whatever data type you are storing in the map
}
});
The idea is that in your SimpleAdapter, you are populating your ListView with a List<Map<String,Object>> (in this case named result).
Therefore, you can get that specific Map<String,Object> from the List by using .get(position), then once again using .get("article_articleNo") to get the Object from the map.
Edit after seeing your comment: Try this:
articleItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView tv = (TextView) view.findViewById(R.id.article_articleNo);
String text = tv.getText().toString();
}
});
First of all, forget about the xml layout. It's just a placeholder for data, which is used to define the layout of the data, and has got nothing to do with storing the data.
Now, your problem statement can be re-stated as: how to fetch data from an array (or list or arraylist or whatever the format of your result is) on clicking list item.
Hmm. Sounds simple now.
Refer to #RougeBaneling's answer for technical details.
I want to get SelectedItem index of ListView. I select item by tapping on item. OnItemClick event work fine, but getSelectedItemPosition() return -1.
What I do wrong?
xml:
<ListView
android:id="#+id/lvAddEdtList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:choiceMode="singleChoice"
android:drawSelectorOnTop="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:listSelector="?android:attr/listChoiceIndicatorSingle" />
In Activity:
lvAddEdtDel.setAdapter(namesList);
lvAddEdtDel.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
lvAddEdtDel.setSelection(position);
view.setSelected(true);
}
});
public void onClick(View v) {
if(lvAddEdtDel.getSelectedItemPosition() < 0 )
{
Toast.makeText(getApplicationContext(), getString(R.string.ItemNotSelected),Toast.LENGTH_LONG).show();
}
});
sorry for my English
I solved the problem by myself.
Layout of the adapter has chosen: android.R.layout.simple_list_item_1;
In my case, it was necessary to choose: android.R.layout.simple_list_item_single_choice;
In touch mode must yuse getCheckedItemPosition
I have ListActivity with custom ArrayAdapter and following layout for list items:
<?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"
android:paddingTop="5dip"
android:paddingBottom="5dip" >
<ImageView
android:id="#+id/image"
android:layout_width="48dip"
android:layout_height="48dip" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="24.5sp" />
<TextView
android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="12sp"/>
</LinearLayout>
My problem is that, I am unable to click any of the list items. What I've tried so far is
setting focusable, clickable to false (which I found might help) to my ImageView
but, unfortunately, problem remains. Please help, this has been bugging me all day. Thank you.
EDIT: OK, so William's answer helped my out with this one, only problem now is that click on item doesn't highlight it, when I change background colour to black (by setting new theme in manifest for this list activity).
This is my custom style for activity theme <style name="ContentsListTheme">
<item name="android:background">#color/black</item>
</style> from res/values/styles/ folder.
To make it clear, once again, default ListActivity background colour is white. I want it black. When I apply the style from above, list activity item highlighting when clicked is disabled. How can I enable it again, to the default highlighting colour?
In your oncreate try this
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
});
OR you can write this function which will handle the clicking on the list items
protected void onListItemClick(ListView l, View v, int position, long id) {}
I have this working code, and I think in your Java code is the problem.
public class NewsListActivity extends Activity implements OnItemClickListener
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new NewsDataArrayAdapter(NewsListActivity.this, headlines));
listView.setOnItemClickListener(this);
...
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
if (v == null) {
return;
}
try this
ListView lv=(ListView)findViewById(yourlistview);
Lv.setonItemClickListener(this);
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
paret.getItemAtPositon(position);
//this returns you an object cast it into the same object you passed to your adapter cast to string if values passed were string
}
I'm using the excellent drag-sort-listview by Carl Bauer (https://github.com/bauerca/drag-sort-listview) to implement a drag-sort enabled listview. However, my requirement is not to need a drag handle on the list, but instead to allow the user to drag the list items using the item itself.
I've gotten that part to work, by setting the #id/drag property to the list item itself.
However, it has a side-effect of not responding to itemClick and itemLongClick events.
Is there any way to get the item clicks / long clicks to work without having a separate draggable layout?
For reference, my code looks like below -
ListView.xml:
<com.mobeta.android.dslv.DragSortListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dslv="http://schemas.android.com/apk/res/com.myproject"
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
dslv:collapsed_height="1dp"
dslv:drag_scroll_start="0.33"
dslv:max_drag_scroll_speed="0.5" />
ItemView.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="#dimen/list_item_height"
android:orientation="horizontal">
<CheckBox
android:id="#+id/check_box"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"/>
<TextView
android:id="#+id/drag"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:padding="#dimen/list_padding"
android:gravity="center_vertical" />
</LinearLayout>
Activity.java:
DragSortListView listView = (DragSortListView) view.findViewById(R.id.list);
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(arg0.getContext(), ((TextView)arg1).getText(), Toast.LENGTH_SHORT).show();
return false;
}
});
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listView, View itemView, int index,
long id) {
Toast.makeText(getView().getContext(), ((TextView)itemView).getText(), Toast.LENGTH_SHORT).show();
}
});
As a bonus, if anyone can help enable multiple-select in addition to click/longclick, it would be much appreciated.
Thanks!
To be able to use OnItemClick and OnItemLongClick in your list you need to set this parameter to the com.mobeta.android.dslv.DragSortListView layout.
dslv:drag_start_mode="onMove"