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);
}
Related
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 the child of listview and set the visibility of its two hidden element to true.
My code is
canvasListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
/*
joinCanvasBtn.setEnabled(true);
joinCanvasPasswordTxt.setEnabled(true);*/
Log.d(TAG,"Selected positon : "+position);
int index = canvasListView.getFirstVisiblePosition() + position;
View v = canvasListView.getChildAt(index);
if(v!=null) {
joinCanvasBtn = (Button) v.findViewById(R.id.joinCanvasBtn);
canvasPassword = (EditText) v.findViewById(R.id.joinCanvasPasswordTxt);
joinCanvasBtn.setVisibility(View.VISIBLE);
joinCanvasPasswordTxt.setVisibility(View.VISIBLE);
}
else {
Log.d(TAG,"Unable to find the selected child of listView");
}
}
});
But this is not working its giving me Nullpointer exception. My layout file is given below. Basically I want to show the password and join button whenever user click on that row.
<?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="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<!-- Item Name -->
<TextView
android:id="#+id/canvasName"
android:height="100dp"
/>
<TextView
android:id="#+id/canvasCreator"
android:text="TextView" />
<EditText
android:id="#+id/joinCanvasPasswordTxt"
android:visibility="invisible"
/>
<Button
android:id="#+id/joinCanvasBtn"
android:text="Join"
android:visibility="invisible"
/>
</LinearLayout>
Please help me to find the solution
Try to write
int index = position - canvasListView.getFirstVisiblePosition();
instead of
int index = canvasListView.getFirstVisiblePosition() + position;
I'm building an Android application using ListView. I have list view items as show below.
^ List Item Index 0
^ List Item Index 1
The xlm is as follows.
<?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="wrap_content"
android:orientation="horizontal"
android:padding="4dip" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/icon" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.93"
android:orientation="vertical"
android:padding="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<!-- JFN bottom used to be 2, top 6 -->
<!-- Name Label -->
<TextView
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="0dip"
android:paddingTop="0dip"
android:textColor="#43bd00"
android:textSize="16sp"
android:textStyle="bold" />
<!-- Email label -->
<TextView
android:id="#+id/email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="0dip"
android:textColor="#acacac" />
<!-- Mobile number label -->
<TextView
android:id="#+id/mobile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Mobile: "
android:textColor="#5d5d5d"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="vertical" >
<ImageView
android:id="#+id/chat"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="2dip"
android:gravity="right"
android:src="#drawable/chat" />
<ImageView
android:id="#+id/calendar"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="2dip"
android:gravity="right"
android:src="#drawable/calendar" />
</LinearLayout>
</LinearLayout>
I'm trying to detect four different click events:
When the picture of the dog is clicked
When the middle section is clicked
When the chat button is clicked
When the calendar button is clicked
In addition, each of these clicks must be distinguishable based on which item in the ListView has been clicked.
I know how I can tell each ListItem apart: I can use the ListView.setOnItemClickListener as is shown below, which provides an id of the item in the list. But this does not tell me which of the four portions was clicked on.
ListView lv = getListView();
// Listview on item click listener
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
Log.d("JFN","List item clicked with id: "+id);
}
});
I can also bind a different onClick function call to each of the four sections in the xml, but this doesn't let me know which of the list items was clicked.
android:onClick="imgClicked"
I don't think I can combine these two solutions, as it would introduce a race condition. Is there a proper way to accomplish detecting both which list item was clicked, and which part of that particular list item was clicked?
Further Info
The following code shows my creation of the ListView using the adapter.
// Retrieve string, convert to JSON, and extract contacts
String jsonData = jsonToStringFromAssetFolder("maemployees.json",this);
JSONObject jsonObj = new JSONObject(jsonData).getJSONObject(TAG_RESULTS);
int numContacts = jsonObj.getInt(TAG_NUM);
Log.d("JFN","Number of contacts stated: "+numContacts);
contacts = jsonObj.getJSONArray(TAG_CONTACTS);
Log.d("JFN","Number of contacts present: "+contacts.length());
// Loop over contacts
for( int i=0; i<contacts.length(); i++) {
// Extract this one
JSONObject c = contacts.getJSONObject(i);
// Extract strings
String first = (c.has(TAG_FIRST)) ? c.getString(TAG_FIRST) : "";
String last = (c.has(TAG_LAST)) ? c.getString(TAG_LAST) : "";
String city = (c.has(TAG_CITY)) ? c.getString(TAG_CITY) : "";
String state = (c.has(TAG_STATE)) ? c.getString(TAG_STATE) : "";
String country = (c.has(TAG_COUNTRY)) ? c.getString(TAG_COUNTRY) : "";
String costName = (c.has(TAG_COST_NAME)) ? c.getString(TAG_COST_NAME) : "";
// Temporary hash map for single contact
HashMap<String, String> contact = new HashMap<String, String>();
contact.put("name", first+" "+last);
contact.put("email", city+", "+state+", "+country);
contact.put("mobile", costName);
// Adding single contact to list
contactList.add(contact);
}
Log.d("JFN","Done looping contacts");
//Updating parsed JSON data into ListView
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, contactList,
R.layout.list_item, new String[] { "name", "email",
"mobile" }, new int[] { R.id.name,
R.id.email, R.id.mobile });
setListAdapter(adapter);
Allright I was having a similar kind of problem but i was using a ViewPager with a ListFragment and a custom ListView Layout.
In the above case you are using just ImageViews, but just in case if you use controls like ImageButton, CheckBox ,Button etc then you would face problems discussed here and here.
This is simply because such controls can steal focus from the ListView and the complete list item cant be selected/clicked. so I would say that just using ImageViews is a smart move.
I am assuming that you are using an Adapter to set the content of your list. Inside that adapter you can assign onClickListener()s for each item like this:
public class MyListAdapter extends ArrayAdapter<String>{
.......
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView=inflator.inflate(R.layout.channel_list_item, null, true);
ImageView chat=(ImageView) rowView.findViewById(R.id.chat);
chat.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
}
ImageView calendar=(ImageView) rowView.findViewById(R.id.calendar);
calendar.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
}
.......
}
}
but remember that while using controls like ImageButton, CheckBox ,Button you need to assign property android:focusable="false" in the XML. and for the ImageButton you need to do this inside the getView() method:
final ImageButton imgBtn=(ImageButton) rowView.findViewById(R.id.imgBtn);
imgBtn.setFocusable(false);
imgBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do your task here
}
});
or you can also use Nathaniel Waggoner's approach.
Hope I answered your question.
You must be setting the content of your list view in an Adapter, So inside the adapter, there is a method getView(). Inside that getView() define each of your view and then set onCLickListener inside those view.
For Ex:
dog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//operation to be performed.
}
});
similarly apply onCLickListeners() to all other views.
If you don't want to set on click listeners for each item, the other option is to compare the view.getId() value of the view passed into on click, to the ids of the views, something like:
switch(view.getId()){
case(R.id.dog_image):
//do stuff
break;
case(R.id.other_image):
//do stuff
break;
}
Otherwise you can do this programatically by doing the following in onCreate:
.....
setContentView(myView);
ImageView dogImage = (ImageView) findViewById(R.id.dog_image);
dogImage.setOnClickListener(new View.OnClickListener {
#Override
public void onClick(View v) {
// do stuff...
}
})
....
I'm trying to write a test application that consists of a few fragments.
One fragment should contain a listView of all music artists from the device.
Each item of this list is a linearlayout starting with a TextView with the artist name and an empty linearlayout under it as follows:
The list is of this layout:
<ListView
android:id="#+id/artistsLists"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</ListView>
Each item is of the following layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/artistName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="" />
<LinearLayout
android:id="#+id/artistsAlbums"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
I'm populating the list using a SimpleCursorAdapter in the following way:
public class MusicTabFragment extends Fragment
{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_music_tab,container,false);
Cursor artistsCursor = getActivity().getContentResolver().query(Audio.Artists.EXTERNAL_CONTENT_URI, new String[]{Audio.Artists.ARTIST,Audio.Artists._ID}, null, null,Audio.Artists.ARTIST);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(view.getContext(), R.layout.music_artist_list_item_layout, artistsCursor, new String[]{Audio.Artists.ARTIST},new int[]{R.id.artistName},0 );
ListView lView = (ListView)view.findViewById(R.id.artistsLists);
lView.setAdapter(adapter);
lView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
((LinearLayout)view.findViewById(R.id.artistsAlbums)).removeAllViews();
Cursor albumsCursor = getActivity().getContentResolver().query(Audio.Artists.Albums.getContentUri("external", ((Cursor)parent.getItemAtPosition(position)).getLong(1)), new String[]{Audio.Albums.ALBUM, Audio.Albums._ID},null,null,null);
LinearLayout artistLayout = (LinearLayout)view.findViewById(R.id.artistsAlbums);
for(albumsCursor.moveToFirst();!albumsCursor.isAfterLast();albumsCursor.moveToNext())
{
View albumView = LayoutInflater.from(view.getContext()).inflate(android.R.layout.simple_list_item_1,artistLayout,false);
((TextView)albumView.findViewById(android.R.id.text1)).setText(albumsCursor.getString(0));
artistLayout.addView(albumView);
}
Log.d("POPULATE","populated again!");
albumsCursor.close();
}
});
return view;
}
}
This works just fine. when i click an artist name, the linearlayout populates with all of this artist album names.
the problem is, that once a linearLayout scrolls out of view, it shows again from the other edge of the view (PacMan Style) as if another list item's linearLayout was populated.
It happens every time the expanded layout goes out of sight. the funny part is that some times when scrolling back up, the linearLayout shows under a different artist name.
example
I'll be glad to hear how should I implement this fragment. But i will also like to know why this behavior is caused.
Thanks,
Maor.
I have found the solution here at stackoverflow.com
It appears that the view shouldn't hold any data, since it is being used for different data when i scroll back and fourth.
I think holding an external data structure to save each virtual view state is not nice programming. is there a way to keep this data anyway? (for this i will be looking now)
In screen layout as I declared my list view as
<ListView android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
And row layout is as
<?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="wrap_content"
android:orientation="vertical">
<TextView android:id="#+id/text1"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/text2"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
My problem is I want to make my list view as multi selected, but when I add attribute in list view android:choiceMode="multipleChoice" it doesn't work. And one way is there, with java code but when I changed id of listview android:id="#id/multi_selectable_list as before android:id="#id/android:list then at loding time of layout it gives error.
Is there any way, or in same way I am doing something wrong.
Actually I want multisectable list view, rules are
there is a button if I clicked on that list should be change in multiselected.
By default list view can not multiselectable.
You can add this method for being listview multiselect:
listView.setChoiceMode (int choiceMode);//for multiple choiceMode=2,For single choiceMode=1;for none cj\hoicMode=0;
If you mean that you wish to move from clicking the whole row item to clicking each child element separately, then I think this happens automatically [android 2.1].Try assigning an OnClickListener to each view separately. if you wish to get the row item then get the view's parent by using v.getParent().getParent().
//this is the onItemClick() not in use now
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
t.show();
Object a = parent.getAdapter().getItem(position);
if(a instanceof YourListItem)
{
YourListItem yourListItem = (YourListItem) a;
}
else
{
}
}
// this onClick function reconstructs it
public void onClick(View v)
{
if(v.getId() == R.id.b_searchSaved)
{
context.startActivity(new Intent(context, Search.class));
}
else if(v.getId() == R.id.savedRowItemText)
{
t.show();
//there must be some more efficient way to do this
//i'm reconstructing data from onItemClick to find out position and item [route]
AdapterView<?> parent = (AdapterView<?>) v.getParent().getParent();
int position = parent.getPositionForView(v);
Object a = parent.getAdapter().getItem(position);
if(a instanceof YourListItem)
{
YourListItem yourListItem = (YourListItem) a;
}
}
else
{
}
}