I browsed StackOverflow for days before deciding to post my own question. I really can't figure this out.
I'm using ActionBarSherlock to implement a viewpager with 2 tabs. I managed to get a ListView on the first tab and everything is working perfectly, at least until I click on one of the list items.
Here's the interesting code in my SherlockListFragment
#Override
public void onViewCreated(View view, Bundle savedInstanceState){
// Click event for single list row
getListView().setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getActivity(), "CLICKED", Toast.LENGTH_SHORT).show();
HashMap<String, String> videoz = new HashMap<String, String>();
videoz = (HashMap<String, String>) parent.getAdapter().getItem(position);
String videoid = "test";
Prova singleitem = new Prova();
Bundle bundle = new Bundle();
bundle.putString("KEY_ID", videoid);
singleitem.setArguments(bundle);
FragmentTransaction trans = getFragmentManager().beginTransaction();
trans.replace(R.id.main_layout, singleitem);
trans.addToBackStack(null);
trans.commit();
}
});
}
This code actually works and open up my "Prova" fragment, but it breaks everything. It appears below my admob (which is normally sticked to the bottom) and the tab bar doesn't work anymore. What am I missing here?
Listview with tab bar still working:
http://imgur.com/FapadTH,TByd05U#1
After clicking an item:
http://imgur.com/FapadTH,TByd05U#0
My main layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/main_layout"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="#+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="xxxxxx"
ads:adSize="SMART_BANNER"
ads:loadAdOnCreate="false"
/>
</LinearLayout>
</LinearLayout>
THANKS IN ADVANCE!
Related
I have a problem with onItemClick method in android.
I know other people have encountered such problems, found some references to their questions on SO, read about the answers provided and some other articles on the internet but it didn't help. Things seem a bit different in my case.
I have a listview of some objects for which I want to show details when they're clicked. But Awkwardly the listener is working fine for only the first object of the listview. The app keeps aborting when I click on other elements of the listview. I can't figure out what's going on.
As I was using the id to retrieve the object I tried changing the id by the position, which didn't work either.
Here is the code of the onCreate method in my activity (DrinkCategoryActivity)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drink_category);
ArrayAdapter<Drink> listAdapter = new ArrayAdapter<>(
this, android.R.layout.simple_list_item_1, Drink.drinks
);
ListView listDrinks = (ListView) findViewById(R.id.list_drinks);
listDrinks.setAdapter(listAdapter);
AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listDrinks,
View itemView, int position, long id) {
Intent intent = new Intent(DrinkCategoryActivity.this, DrinkActivity.class);
intent.putExtra(DrinkActivity.EXTRA_DRINKID, (int) id);
startActivity(intent);
}
};
listDrinks.setOnItemClickListener(itemClickListener);
}
My DrinkActivity (the one that should show me the details of each Drink object when clicked in the listview) onCreate method code is shown below
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drink);
int drinkId = (int) getIntent().getExtras().get(EXTRA_DRINKID);
Drink drinkItem = Drink.drinks[drinkId];
ImageView image = (ImageView) findViewById(R.id.drink_image);
image.setImageResource(drinkItem.getImageResourceId());
TextView drink_name = (TextView) findViewById(R.id.drink_name);
drink_name.setText(drinkItem.getName());
TextView drink_description = (TextView) findViewById(R.id.drink_description);
drink_description.setText(drinkItem.getDescription());
TextView drink_price = (TextView) findViewById(R.id.drink_price);
String price = ""+drinkItem.getPrice();
drink_price.setText(price);
}
I don't know whether there is something wrong with the xml files so here they are :
activity_drink_category.xml goes below
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.DrinkCategoryActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="#layout/customer_screen_toolbar_layout"/>
<ListView
android:layout_marginTop="10dp"
android:id="#+id/list_drinks"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
And activity_drink.xml goes here
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activities.DrinkActivity">
<include
layout="#layout/customer_screen_toolbar_layout"/>
<ImageView
android:id="#+id/drink_image"
android:layout_width="190dp"
android:layout_height="190dp" />
<TextView
android:id="#+id/drink_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/drink_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/drink_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#EB8E13"
android:textColor="#color/white" />
</LinearLayout>
Any help will be valuable. Thanks in advance
You need to pass "Position" instead of "id" in Intent. As you describe above its works fine for the first position. Then you need to Use position
intent.putExtra(DrinkActivity.EXTRA_DRINKID, (int) position);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drink_category);
ArrayAdapter<Drink> listAdapter = new ArrayAdapter<>(
this, android.R.layout.simple_list_item_1, Drink.drinks
);
ListView listDrinks = (ListView) findViewById(R.id.list_drinks);
listDrinks.setAdapter(listAdapter);
AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listDrinks,
View itemView, int position, long id) {
Intent intent = new Intent(DrinkCategoryActivity.this, DrinkActivity.class);
intent.putExtra(DrinkActivity.EXTRA_DRINKID, (int) position);
startActivity(intent);
}
};
listDrinks.setOnItemClickListener(itemClickListener);
}
I have a search interface (with an android search widget). When I retrieve results, each results is wrapped into a folding-cell. Once the folding-cell is loaded, I fill the cell-title with a simple TextView. Then, I fill the content with a TextView (which works). In addition to the TextView, my cell-content also contains a LinearLayout. I use this layout to add a fragment, using a FragmentTransaction.
My problem is that each fragment is supposed to fill a different folding-cell, but only they all adds to the first folding-cell (so I guess that FragmentTransaction manager is adding all fragments to the same LinearLayout, the one of the first Folding-Cell).
Here is a screen of 1st and 2nd folding-cells, unfolded:
Here is the code for the search results :
try {
JSONArray jsonarr = new JSONArray(response);
for (int i = 0; i < jsonarr.length(); i++) {
JSONObject row = jsonarr.getJSONObject(i);
Bundle args = new Bundle();
args.putString("email", row.getString("email"));
args.putString("name", row.getString("nom_organisme"));
Fragment newFragment = new FragmentOrganisationFoldingCell();
newFragment.setArguments(args);
getFragmentManager().beginTransaction().add(R.id.container_organisation_presentation_for_search, newFragment).commit();
organisationList.add(newFragment);
}
if(jsonarr.length() == 0){
errorText.setVisibility(View.VISIBLE);
}
} catch (JSONException e) {
e.printStackTrace();
}
Here is the FramgmentOrganisationFoldingCell:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_organisation_folding_cell, container, false);
final FoldingCell fc = (FoldingCell) view.findViewById(R.id.folding_cell);
Bundle args = getArguments();
TextView organisationName = view.findViewById(R.id.search_organisation_default_name);
organisationName.setText(args.getString("name"));
TextView t = view.findViewById(R.id.cell_content);
t.setText(args.getString("name"));
LinearLayout fragContainer = (LinearLayout) view.findViewById(R.id.cell_content_frag);
getFragmentManager().beginTransaction().replace(fragContainer.getId(), new FragmentProfileOrganisation(), "Organisation" + args.getString("name")).commit();
fc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fc.toggle(false);
}
});
return view;
}
Here is fragment_organisation_folding_cell, which is the view I inflate in FragmentOrganisationFoldingCell :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/block">
<com.ramotion.foldingcell.FoldingCell
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/folding_cell"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false"
android:clipToPadding="false">
<!-- CONTENT (UNFOLDED) LAYOUT (MUST BE AT LEAST 2x times BIGGER than content layout bellow)-->
<include layout="#layout/cell_content_layout" />
<!-- TITLE (FOLDED) LAYOUT (MUST BE AT LEAST 2x times SMALLER than content layout above) -->
<include layout="#layout/cell_title_layout" />
</com.ramotion.foldingcell.FoldingCell>
</LinearLayout>
Here is the XML code for the cell_content_layout, which contains the LinearLayout I use to add my fragments. (So I beleive that this is the file that creates the trouble).
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="60sp"
android:layout_margin="40dp"
android:id="#+id/cell_content"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cell_content_frag">
</LinearLayout>
</LinearLayout>
Here is the fragment_organisation_search XML file :
...
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/block"
android:id="#+id/container_organisation_presentation_for_search"
android:layout_margin="5dp">
</LinearLayout>
...
Do anyone have any trouble adding fragments into Folding-cells too? If anyone have any idea I would be really thankful !
The following command in your code that is parsing search results will add newFragment to a view with ID R.id.container_organisation_presentation_for_search. That ID is in your XML for the actual fragment.
getFragmentManager().beginTransaction().add(R.id.container_organisation_presentation_for_search, newFragment).commit();
I think instead what you want may be:
getFragmentManager().beginTransaction().add(R.id.cell_content_frag, newFragment).commit();
I have a listview with buttons in each row in fragment, the number of buttons retrieve based on sqlite database. I want to make when button click it move to an another new activity and parse button names. But when i click the buttons nothing happens, i have already try to use this code in xml file from ListView setOnItemClickListener not working by adding button, but it still not working.
android:focusable="false"
android:focusableInTouchMode="false"
android:descendantFocusability="blocksDescendants"
Does anyone have solution? Thanks before and below is my code.
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
DatabaseHandler db = new DatabaseHandler(getActivity());
listView = (ListView) getView().findViewById(R.id.list_addcard);
List<CardType> listCardType = db.getAllCardType();
for(CardType ct : listCardType){
String log = "ID: " + ct.getTypeId() + " Category Name: " + ct.getTypeCategory();
Log.d("Result: ", log);
categoryArray.add(ct);
}
adapter = new ListAdapterAddCard(getActivity(), R.layout.list_row_addcard, categoryArray);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, final int position, long id){
categoryName = categoryArray.get(position).getTypeCategory();
Intent intent = new Intent(getActivity(), DetailCategory1.class);
intent.putExtra("categoryName", categoryName);
startActivity(intent);
}
});
}
listview.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_height="match_parent"
android:background="#color/background">
<ListView
android:id="#+id/list_addcard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#null"
android:layout_marginTop="30dp"/>
</RelativeLayout>
listrow.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_height="match_parent"
android:orientation="horizontal"
android:padding="8dp"
android:descendantFocusability="blocksDescendants">
<Button
android:id="#+id/category_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/category"
android:focusable="false"
android:focusableInTouchMode="false"/>
</RelativeLayout>
Firstly remove the following from your button xml..
android:focusable="false"
android:focusableInTouchMode="false"
then in your custom adapter where you inflates your custom xml cell in the list, get the button reference and set the onclick listenet over there for that button.
For me it did not work having an startActivity function within the application as within the function you don't have access to the context, you need to add the variable of the host activity. Basically I think you need to do the following:
Activity host = (Activity) listView.getContext();
Intent intent = new Intent(host, DetailCategory1.class);
....
host.startActivity(intent);
This is what I need to do in getView method of my adapter for an standard listview.
category_name Button is stealing the focus from the ListView item, you could either add onClick listener to the each item button or simply use TextView instead styled as Button, this is a very bad practice though and confusing to your users
<?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="match_parent"
android:orientation="horizontal"
android:padding="8dp"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="#+id/category_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/category"
style="#android:style/Widget.Button"/>
</RelativeLayout>
First you need to set clickable to your custom layout button in your case
<Button
android:id="#+id/category_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/category"
android:clickable="true"/>
Next, you are setting the OnItemClickListener to the "ListView" (<< parent) itself not the
(Button>>) children listView.setOnItemClickListener(new OnItemClickListener(){
BUT,
you can get a reference to the category_name button once you have inflated your ListView by doing something like this;
Button myButton = (Button) list_addcard.findViewById(R.id.category_name);
If you need more help you can always visit the Android Documentation for
ListView ListView Documentation
Cheers.
So finally i found the answer, i change the setOnClickListener to my adapter class and it's work fine for me. Thank you all for your suggestion.
holder.txtCategoryName.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(context, DetailCategory.class);
int pos = (Integer) v.getTag();
categoryName = categoryItems.get(pos).getTypeCategory();
intent.putExtra("categoryName", categoryName);
context.startActivity(intent);
}
});
Am creating a master/detail fragment from this link. This works fine, now here instead of textview in detail fragment class i would like to implement listviews, (ie) i would like to display the details of the masterview as a child listview, hence i tried
detail.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:id="#+id/listview1"
/>"
<ListView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:id="#+id/listview2"
/>"
</LinearLayout>
and in my detailFragment class
public class DetailFragment1 extends ListFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] values = new String[] { "1", "2", "3",
"4" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
My problem is child listview is displaying at initial stage but i want to display this listview when i click the first row of master page.Am new to fragments, so help me in achieving this. Thanks in advance..
Create a CallBack interface:
public interface Callbacks {
public void onItemSelected(long id);
}
Let the fragment inplement it:
public class DetailFragment1 extends ListFragment implements CallBacks {
In the ListFragment OnListitemClick:
#Override
public void onListItemClick(ListView listView, View view, int position, long id) {
mCallbacks.onItemSelected(id);
}
In the Activity:
Public class MyActivity extends FragmentActivity implements Callbacks {
#Override
public void onItemSelected(long id) {
// Replace the detail fragment with the corresponding item selected in the list
}
Instead of doing like this, you can design a layout which contains ListView as master and frameLayout container. In frameLayout container, you can dynamically inflate layout, which is designed in such a way that you can have child listview and details view.
<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="horizontal"
tools:context=".NavigationActivity" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#color/strip_background"
android:layout_weight="1"
android:orientation="vertical" >
<ListView
android:id="#+id/nav_lv_transactions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none" >
</ListView>
</LinearLayout>
<FrameLayout
android:id="#+id/nav_fl_frag_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="14"
android:background="#android:color/darker_gray" >
</FrameLayout>
</LinearLayout>
In onclick listerner put this code,
Bundle arguments = new Bundle();
arguments.putString(ARG_ITEM_ID,
"Some Name");
MyClassFragment newFragment = new MyClassFragment();
newFragment.setArguments(arguments);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.nav_fl_frag_container, newFragment);
transaction.commit();
Try this. It will work
I've been agonising for a while now about something which ought to be relatively simple, but I can't seem to get it to work. The data for each ListView is displaying correctly, and I can select and activate the onItemClickListener by using the trackball. However, I cannot scroll down or select any of the items by touch, which I know I should be able to. Any help much appreciated. My code and xml follows:
public class POITab extends TabActivity implements AdapterView.OnItemClickListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.poitab);
ListView raillist=(ListView)findViewById(R.id.raillist);
Cursor mrailStationsCursor = db.type_query(KEY_RAIL);
setupTab(raillist, mrailStationsCursor, "Rail", R.drawable.logo);
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("touched", " yes");
}
public void setupTab (ListView list, Cursor cursor, String tabname, int drawable) {
startManagingCursor(cursor);
String[] from_all = new String[]{DbAdapter.KEY_NAME};
int[] to_all = new int[] {android.R.id.text1};
list.setAdapter(new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1, cursor, from_all, to_all));
list.setOnItemClickListener(this);
TabHost.TabSpec spec= getTabHost().newTabSpec(tabname);
spec.setContent(list.getId());
spec.setIndicator(tabname, getResources().getDrawable(drawable));
getTabHost().addTab(spec);
}
}
poitab.xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="62px">
<ListView android:id="#+id/raillist"
android:choiceMode="singleChoice"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
</ListView>
</FrameLayout>
</TabHost>`
I've found out what the problem was - I had a ListView declared in the layout (not shown above, perhaps I should have included the whole source) that wasn't being used, and all touch events were being handled by that. So, moral of the story, only have the ListViews that you need within a TabHost, otherwise it will cause you problems.