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.
Related
i want to click on Button which is located inside of List Item. I tried those codes, it didnt give me error but still not doing the job.
Screen:
I tried these code (1):
EntityListItem view2 = solo.getView(EntityListItem.class,1);
solo.clickOnView(view2.DeleteEntity);
solo.sleep(3000);
I tried these code (2):
ListView myList = (ListView)solo.getView(com.hh.android.R.id.lister);
View listElement = myList.getChildAt(0);
View alt = listElement.findViewById(com.hh.android.R.id.footer);
solo.clickOnView(alt.findViewById(com.imona.android.R.id.DeleteEntity));
I tried these code (3):
ListView myList = (ListView)solo.getView(com.hh.android.R.id.lister);
View listElement = myList.getChildAt(0);
solo.clickOnView(listElement.findViewById(com.hh.android.R.id.DeleteEntity) );
this entity list
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ListView
android:id="#+id/lister"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#android:color/transparent"
android:dividerHeight="10.0sp" >
</ListView>
This is List Item
<LinearLayout
android:id="#+id/footer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_marginRight="20dp"
android:id="#+id/DeleteEntity"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/ic_action_delete"/>
</LinearLayout>
Use this code
ListView ListView=(ListView)solo.getView(R.id.listview);
View view=ListView.getChildAt(0);
Button button=(Button)view.findViewById(R.id.button);
solo.clickOnView(button);
This solved my problem :
ListView myList = (ListView) solo.getView(com.hh.android.R.id.lister,1); //1 is ipmortant, dont know why
EntityListItem til = (EntityListItem) myList.getChildAt(0);
solo.clickOnView(til.DeleteEntity);
I get LIstView with list id, then i get list item object from that ListView, using index number. Then click on that item object view's button, text whatever.
I coded these methods to help =)
protected View getViewAtListView(int resListViewID, int position) {
return ((ListView) getSolo().getView(resListViewID)).getChildAt(position);
}
protected View getViewAtRowListView(int resListView, int position, int viewIDInRowView) {
return getViewAtListView(resListView, position).findViewById(viewIDInRowView);
}
protected void clickOnViewAtRowView(int resListView, int position, int viewIDInRowView) {
View view = getViewAtRowListView(resListView, position, viewIDInRowView);
assertNotNull(view);
getSolo().clickOnView(view);
}
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 am trying to retrieve a certain id when an item in list is clicked on.
How can i retrieve the value +id/name? I want to use it in an alert dialog ash shown below.
I'm currently working with a simple toast which just shows "name" at the moment when a list item is clicked on
Here is the xml code for my list item:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/image" android:textSize="20dip"
android:layout_marginLeft="10dip"/>
<ImageView
android:id="#+id/image"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_centerHorizontal="true"
android:src="#drawable/stub" android:scaleType="centerCrop"/>
<TextView
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/image"
android:textSize="20dip" android:layout_marginLeft="10dip"/>
</RelativeLayout>
Alert dialog code:
list.setOnItemClickListener(new OnItemClickListener() {
String artistname = "get +id/name here";
String items[] = {"Youtube", "Soundcloud"};
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
AlertDialog.Builder builder = new
AlertDialog.Builder(JsonActivity.this);
builder.setTitle(artistname);
builder.setItems(items, new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alert = builder.create();
alert.show();
}});
}
Try this
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//name of the artist...
String name=((TextView)view.findViewById(R.id.name)).getText();
}
I think this is what you want...I hope this will help you
When we display a list using Adapter.
In the getView method we fetch a value from a collection on the basis of position of item in the list. The data source of the adapter i am talking about.
like this.
public View getView(){
holder.name.setText(data.get(position));
}
When a list item get clicked you will get the position that get clicked using listItemOnClickListener. In that callback
call the get method of the data source again to get the element you paste over the list item.
Hope you got the point. As i didn't posted much code to support it.
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"
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