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();
Related
I have a popup in my page which has a listview. I have created design of popup in a seperate xml and loading it on some button click in my main page. The popup has a listview with each row having a image and a textview. I am not able to get row selection in listview in android 4.1 but it is working in 5.0. Can anyone please suggest me the solution?
Listview:
<ListView android:id="#+id/lstSites"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fastScrollEnabled="true"
android:layout_margin="5dp"
android:descendantFocusability="blocksDescendants"></ListView>
</LinearLayout>
Listview Item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false">
<ImageView
android:id="#+id/thumbImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:id="#+id/tvSite"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Name"
android:focusable="false"
android:focusableInTouchMode="false"/>
adding row click listner:
lstSiteMenu = (ListView) popupView.findViewById(R.id.lstSites);
adapter = new MenuItemsAdapter(SiteActivity.this, arrMenu);
// Attach the adapter to a ListView
lstSiteMenu.setAdapter(adapter);
lstSiteMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
final int position, long id) {
}
Try all of solutions:
popupwindow.setFocusale(true);
listView xml : "android:focusable="true"
do not use lstSiteMenu.setOnItemClickListener
instead go to your Adapters getView and add
convertView.setOnClickListener
Hope it will help you!
remove this property from listview , android:descendantFocusability="blocksDescendants"
and
android:focusable="false"
android:focusableInTouchMode="false"
from text and imageview.
And try This is sample example,
String names[] ={"A","B","C","D"};
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.row_file, null);
alertDialog.setView(convertView);
alertDialog.setTitle("List");
ListView lv = (ListView) convertView.findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,names);
lv.setAdapter(adapter);
alertDialog.show();
otherwise your code is fine. it works.
go through this link for more details.
Another official link for the same.
You need to remove android:descendantFocusability="blocksDescendants" from your ListView.
Also remove android:focusable="false" and android:focusableInTouchMode="false" from your row layout in the ImageView and the TextView.
The list item is supposed to be clickable, there is disable it obtaining focus
Set row onclick listener in adapter class
public class MenuItemsAdapter extends Base Adapter{
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
/*
Paste your adapter code
*/
customView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v("log", "You clicked at pos "+position);
}
});
}
}
This code worked in all sdk versions.
Also remove these properties from xml files
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false"
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.
please refer this image https://www.dropbox.com/s/6zoj9lw10oc07xa/to_dropbox.png
what i am doing :
i am creating a list_view , in which i am adding custom adapter .
what i am using :
i am using , listView , customAdapter , menuitem .
listView : single listview in whole application
customadapters : 3 custom adapters
menuitem : 1
How i am implementing :
i have data base from which things are fetched properly , and from that database i have entered these values in my listview by filtering that data in 3 types :
1st adapter_type is entered by default ( in onCreate ) .
adapter = new Adapter_forCompletedReminder( array_today_title , this) ;
ls.setAdapter(adapter) ;
2nd adapter_type is entered in my listview by pressing menuitem .
adapter = new Adapter_forCompletedReminder( array_past_2_day_title , this) ;
ls.setAdapter(adapter) ;
3rd adapter_type is entered in my listview by pressing menuitem .
adapter = new Adapter_forCompletedReminder( array_other_day_title , this) ;
ls.setAdapter(adapter) ;
what is my problem :
this code is added inside onCreate() method .
ls.setOnItemClickListener( new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> adapterView , View view , int position ,long arg3)
{
Log.i("Item clicked","tushar:itemclicked") ;
}
});
when i have tried to implement AdapterView.OnItemClickListener() , it is not working ...
code is not crashing ( no red lines in log cat ).
code is not executing in the click of llist_view_element
thanks , for reading my problem .
You use checkbox in customview_completedxml_listview.xml that is why onItemClick listener is not working. If you set clickable = "false" in checkbox then onItemclick listener will work.
If you want want that checkbox will stil work then you have to set onclicklistener event in you custom adapter class.
// I edit getView
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = LayoutInflater.from(ob) ;
View v = inflater.inflate(R.layout.customview_completedxml_listview, null ) ;
TextView txt = ( TextView ) v.findViewById(R.id.txt_fordisplayingdata) ;
txt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(ob, "Hello", Toast.LENGTH_SHORT).show();
}
});
txt.setText(recieved_Array[position]) ;
return v ;
}
///////////////////////
// Second solution set android:focusable="false" in checkbox
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
>
<TextView
android:id="#+id/txt_fordisplayingdata"
android:layout_width="240dp"
android:text="display data"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
/>
<TextView
android:id="#+id/txt_fordisplayingLargerdata"
android:layout_width="240dp"
android:text="display data larger bahut larger "
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:visibility="gone"
/>
<View
android:layout_width="2dp"
android:layout_toRightOf="#id/txt_fordisplayingdata"
android:layout_height="15dp"
android:layout_marginLeft="15dp"
android:layout_centerVertical="true"
android:id="#+id/view_forcompletedtask"
/>
<CheckBox
android:layout_toRightOf="#id/view_forcompletedtask"
android:id="#+id/checkbox_tocomplete"
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:focusable="false"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
/>
</RelativeLayout>
Here are few things you can try :-
If there is any button(or checkbox) or any element in your listview item which handles click event then do this for each element:-
android:focusable = "false"
android:focusableInTouchMode = "false"
Try setting this
list.setItemsCanFocus(false);
Override the onItemClick() method
ls.setOnItemClickListener( new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapterView , View view , int position ,long arg3)
{
Log.i("Item clicked","tushar:itemclicked") ;
}
});
I really can't say what exactly problem you have, but I wrote very simple example for you. Try it, and if it works - just port your current project into my sample project. https://docs.google.com/file/d/0Bz4Xd7Ju_kbYbVlyd1dvYTJZYTg/edit?usp=sharingalways
P.S.: I recommend you to read about "best practices in Android", when you finish your idea ( about ViewHolder pattern).
I have created a listview using the default ArrayAdapter. However, to select an item from the list I have to tap the item and not just the row. How can I change it in order to just click the row the item is in rather than the item itself?
Here is the code for my listview:
ListView mListView = (ListView) findViewById(android.R.id.list);
mListView.setAdapter(new ArrayAdapter<String>(this, R.layout.row, ans));
mListView.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View v, int i,
long l) {
Log.d(LOG_TAG,"Selected option"+i);
startActivity(intent);
finish();
}
});
Here is the xml for the listview:
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:divider="#drawable/list_divider"
android:dividerHeight="1dp" />
Here is the row.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#style/list_answers"
/>
Change your child views of the row to include android:focusable="false". This will allow you to click the whole row. Right now, since each individual view thinks it can gain focus, they happen "on top" of the row.
P.S. What we really need is the XML for the ROW of the ListView. Not the ListView itself. :)
Hope this helps,
FuzzicalLogic
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