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;
Related
I am using a expandable list view in my application with group expand and group collapse listeners.
But what happens is the right arrow click in the group row works absolutely perfect but when i tap on the row the group does not expand in a single clcik.
I have to tap it multiple times ,only then it expands.
here is the expandable list view group click listener code:
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
groupName = _groupNameList.get(i);
_header = _child.get(groupName);
if (_header.size() == 1 && _header.get(0).getServiceName().equals(groupName)) {
expandableListView.setGroupIndicator(null);
String id=_header.get(0).getServiceId();
Intent intent = new Intent(CategoryDetails1.this, ServiceSelectionOptionsListActivity.class);
SharedPreferences.Editor editor = CategoryDetails1.this.getSharedPreferences(AppConstants.VERIFICATION, CategoryDetails1.this.MODE_PRIVATE).edit();
editor.putString(AppConstants.SUBSERVICEID, id);
editor.commit();
startActivity(intent);
}
return false;
}
});
and here is the header row 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_gravity="center"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tvGroupName"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:textSize="18dp"
android:layout_marginBottom="10dp"
android:textStyle="bold"
android:layout_marginRight="25dp"
android:layout_alignParentLeft="true"
android:inputType="textMultiLine"
android:textColor="#color/selecteditem"
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:focusable="false" have tried this
android:layout_weight="1" />
<ImageView
android:id="#+id/groupindicator"
android:layout_width="30dp"
android:layout_marginBottom="10dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_alignParentRight="true"
android:src="#drawable/circle_plus" />
</RelativeLayout>
this is my getgroup view method
public View getGroupView(int groupPosition, boolean b, View view, ViewGroup viewGroup) {
if (view == null) {
view = mLayoutInflater.inflate(R.layout.expandable_list_header, null);
}
TextView tvGroupName = (TextView) view.findViewById(R.id.tvGroupName);
tvGroupName.setFocusable(false); //have tried it also
ImageView ivgroupicon = (ImageView) view.findViewById(R.id.groupindicator);
// ivgroupicon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.servicescollapseicon));
tvGroupName.setText((String) getGroup(groupPosition));
int size = _child.get((String) getGroup(groupPosition)).size();
view.setPadding(0, 10, 0, 10);
if (size == 1 && _child.get((String) getGroup(groupPosition)).get(0).getServiceName().equals((String) getGroup(groupPosition))) {
ivgroupicon.setVisibility(View.GONE);
} else
ivgroupicon.setVisibility(View.VISIBLE);
if (b) {
ivgroupicon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.servicesexpandicon));
} else {
ivgroupicon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.servicescollapseicon));
}
return view;
}
Now what I want is i want smooth group expand and collapse listener in my activity.I have tried setFocusable to false in both my xml and adapter files but in Vain.
So guys please suggest me something asap.I am stuck in this from a very long time.
Add below property to your
TextView and ImageView
android:focusable="false"
And in Root Layout :RelativeLayout
android:descendantFocusability="blocksDescendants"
I have a ListView in one of my activities that I have bound to an ArrayList using a custom ArrayAdapter. I have set an OnItemClickListener to the ListView which should call a method that starts another activity. However, I find that when I click on the ListView items, it only sometimes works. Sometimes it will start the activity as it should; other times it seems to detect the click (the ripple effect appears on the list item) but does nothing; other times it doesn't even appear to detect the click (the ripple effect doesn't appear).
I've tried all the usual suggestions that I've come across: blocking descendants on the parent view item, setting clickable and focusable to false on all the components of the item views, setting isEnabled to return true in the custom adapter, etc, but the behavior remains the same. Any help appreciated. Here is the relevant code:
Activity containing the ListView:
public class ViewCollectionActivity extends AppCompatActivity {
private final String className = this.getClass().getSimpleName();
private CollectionHandler collectionHandler;
private Context context;
private ArrayList<Game> displayedCollection;
private GameCollectionAdapter collectionAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_collection);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
context = this;
collectionHandler = CollectionHandler.getInstance(this);
TextView view = null;
if (collectionHandler.getDisplayedCollection().size() > 0) {
view = (TextView) findViewById(R.id.no_items_textview);
view.setVisibility(View.GONE);
}
String currentDate = collectionHandler.getDateLastSynchronised();
view = (TextView) findViewById(R.id.last_updated_textview);
view.setText("Last synchronised: " + currentDate + " Total games: " + String.valueOf(collectionHandler.getDisplayedCollection().size()));
collectionAdapter = collectionHandler.getCollectionAdapter();
ListView listView = (ListView) findViewById(R.id.collection_list_view);
listView.setAdapter(collectionAdapter);
AdapterView.OnItemClickListener collectionItemClickListener = new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
launchGameDetailsActivity(position);
}
};
listView.setOnItemClickListener(collectionItemClickListener);
}
public void launchGameDetailsActivity(int position){
Log.d(className,"Starting lauchGameDetailsActivity method");
collectionHandler.setSelectedGame(position);
Intent intent = new Intent(this,ViewGameDetailsActivity.class);
startActivity(intent);
Log.d(className, "Ending lauchGameDetailsActivity method");
}
The XML for the activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.bleachedlizard.ludome.viewcollection.ViewCollectionActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Synchronise Collection"
android:onClick="synchroniseCollection"/>
<TextView
android:id="#+id/last_updated_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Last synchronised: "
android:textAlignment="center"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Display Collection"
android:visibility="gone"
android:onClick="displayCollection"/>
<ListView
android:id="#+id/collection_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</ListView>
<TextView
android:id="#+id/no_items_textview"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="You have no items in your collection."
android:textAlignment="center"
android:textSize="20sp"/>
</LinearLayout>
The XML for the item views:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/collection_item_layout"
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal"
android:clickable="false"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false">
<ImageView
android:id="#+id/collection_item_image"
android:layout_width="75dp"
android:layout_height="75dp"
android:src="#drawable/testimage"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
/>
<TextView
android:id="#+id/collection_item_name"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:padding="16dp"
android:singleLine="false"
android:textColor="#android:color/darker_gray"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:textIsSelectable="false"/>
<TextView
android:id="#+id/collection_item_plays"
android:layout_width="100dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:padding="8dp"
android:textColor="#android:color/darker_gray"
android:text="Plays: 0"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:textIsSelectable="false"/>
</LinearLayout>
The code for the custom adapter:
public class GameCollectionAdapter extends ArrayAdapter<Game> {
private ArrayList<Game> collection;
public GameCollectionAdapter(Context context, int resource, ArrayList<Game> collection){
super(context, resource, collection);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout gameView = (LinearLayout) convertView;
LayoutInflater mInflater = LayoutInflater.from(getContext());
if (gameView == null) {
gameView = (LinearLayout) mInflater.inflate(R.layout.collection_item_view, null);
}
//Game game = collection.get(position);
Game game = super.getItem(position);
if (game != null) {
// This is how you obtain a reference to the TextViews.
// These TextViews are created in the XML files we defined.
TextView gameTitle = (TextView) gameView.findViewById(R.id.collection_item_name);
TextView numOfPlays = (TextView) gameView.findViewById(R.id.collection_item_plays);
ImageView thumbnail = (ImageView) gameView.findViewById(R.id.collection_item_image);
// check to see if each individual textview is null.
// if not, assign some text!
if (gameTitle != null){
gameTitle.setText(game.getTitle());
}
if (numOfPlays != null){
numOfPlays.setText("Plays: " + String.valueOf(game.getNumOfPlays()));
}
if (thumbnail != null){
thumbnail.setImageBitmap(game.getThumbnail());
}
}
// the view must be returned to our activity
return gameView;
}
#Override
public boolean isEnabled(int position) {
return true;
}
}
I discovered what was causing the problem: the way I had set up the array that backed the ListView meant that it was downloading and storing the Bitmaps for every element in the array all the time. Once I changed the implementation so that it only downloaded the images as the ListView required them, then that seemed to improve performance and the onClickListener started to work fine.
The implementation I used was the exact same one shown here:
http://developer.android.com/training/displaying-bitmaps/process-bitmap.html
I think the issue is due to the position of the item selection whenever you click you have an list position which is passed to your method launchGameDetailActivity(int position) check with log or toast on item click what all the position you are getting do the needful.
Here is my code try this like this if it helps.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(RecipeClass.this, "Position is" + position, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(RecipeClass.this, RecipeIngredients.class)
intent.putExtra("position", position);
startActivity(intent);
}
Check your arraylist value also whether they are not null.
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 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);
}
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
{
}
}