This question already has answers here:
I get the error "Unreachable statement" return in android
(3 answers)
Closed 5 years ago.
I'm pretty new to Fragment based activities. I'm working on a collapsible listview inside the CollapsingToolbarLayout.
Here, I'm facing Unreachable statement error in java file. Not able to get rid of it.
My code -
public class MenuActivity extends android.support.v4.app.Fragment {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_booktable, container, false);
// get the listview
expListView = (ExpandableListView) container.findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getActivity().getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getActivity().getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getActivity().getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("South Indian");
listDataHeader.add("Quick Bites");
listDataHeader.add("Soups");
// Adding child data
List<String> SouthIndian = new ArrayList<String>();
SouthIndian.add("Kara Bath");
SouthIndian.add("Chow Chow Bath");
SouthIndian.add("2 Idli 1 Vada");
SouthIndian.add("Rava Idli");
SouthIndian.add("Curd Vada");
SouthIndian.add("Masala Dosa");
List<String> QuickBites = new ArrayList<String>();
QuickBites.add("Veg Sandwich");
QuickBites.add("Veg Toast Sandwich");
QuickBites.add("Bread Butter Jam");
QuickBites.add("Bread Jam");
QuickBites.add("Pakoda");
QuickBites.add("Masala Puri");
List<String> Soups = new ArrayList<String>();
Soups.add("Tomato Soup");
Soups.add("Sweet corn Soup");
Soups.add("Veg Soup");
Soups.add("Veg Schezwan Soup");
Soups.add("Veg Noodles Soup");
listDataChild.put(listDataHeader.get(0), SouthIndian); // Header, Child data
listDataChild.put(listDataHeader.get(1), QuickBites);
listDataChild.put(listDataHeader.get(2), Soups);
}
}
How do I fix the error for the line expListView = (ExpandableListView) container.findViewById(R.id.lvExp); and make this code run?
move return inflater.inflate(R.layout.activity_booktable, container, false) to the end of method onCreateView()
Your first line in onCreateView already returns the View so the rest of the code is not executed:
return inflater.inflate(R.layout.activity_booktable, container, false);
You have to replace it with
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.activity_booktable, container, false);
...
return myView;
}
and then better do the findViewById on this view, e.g.
expListView = (ExpandableListView) myView.findViewById(R.id.lvExp);
Related
I work with a ListView populated by a JSON, this part works, but when I have clicked on a list item, the click doesn't work.
I have read things about
setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS); But I don't understand where to put that.
Here is my code
This a fragment displayed in a TabLayout in MainActivity
public class Tab3 extends Fragment {
private View v;
private ListView listView;
private ArrayList<CustomModel> mCustomArrayList = new ArrayList<CustomModel>();
private AdapterCustom adapter1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String equipeJSONString=((MainActivity)getActivity()).equipeJSONString;
try {
JSONObject obj = new JSONObject(equipeJSONString);
JSONArray equipe = obj.getJSONArray("equipe");
for (int i = 0; i < equipe.length(); i++) {
JSONObject c = equipe.getJSONObject(i);
//stock les valeurs du Json dans des vars
String nom = c.getString("nom");
String photo = c.getString("photo");
String texte = c.getString("texte");
mCustomArrayList.add(new CustomModel(nom, texte,photo));
}
} catch (Throwable t) {
Log.e("Tab3", "Erreur Could not parse malformed JSON : \"" + equipeJSONString + "\"");
}
}
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
v =inflater.inflate(R.layout.tab_3,container,false);
// v.setBackgroundColor(Color.BLACK);
listView = (ListView) v.findViewById(R.id.customlist);
adapter1 = new AdapterCustom(getActivity(), mCustomArrayList);
// Assign adapter to ListView
listView.setAdapter(adapter1);
// ListView Item Click Listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.v("Tab3","click on Item");
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getActivity().getApplicationContext(),
"Position :" + itemPosition + " ListItem : " + itemValue, Toast.LENGTH_LONG)
.show();
Intent myIntent = new Intent(getActivity(),CustomActivity.class);
startActivity(myIntent);
}
});
return v;
}
}
Edit : I have done a test with String[]
If I use a String[] to populate the list it works, I can click.
Code tested on OnCreateView :
String[] values2 = new String[] { "Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
"Android Example",
"List View Source Code",
"List View Array Adapter",
"Android Example List View"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, android.R.id.text1, values2);
So what is wrong when I populate with my AdapterCustom ?
listView.setClickable(true);//in fragment
#Override
public boolean isEnabled(int position) //in adapter
{
return true;
}
you can use a switch case inside onItemClick
like
switch(position)
{
case 0:
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getActivity().getApplicationContext(),
"Position :" + itemPosition + " ListItem : " + itemValue, Toast.LENGTH_LONG)
.show();
Intent myIntent = new Intent(getActivity(),CustomActivity.class);
startActivity(myIntent);
}
Ok I have found, I had a
.setOnClickListener(new View.OnClickListener() {
in my CustomAdapter that handled the click.
Making those tests helped me to find the solution.
I am creating an application, which has different screens for admin users and different screens for normal users. When admin logs in, screen will be displayed which consists of expandable list views. The Expandable list view header is a string array. The child items are the list of values obtained from database. Now, please let me know how can I use expandable list view in my case? Since I have different list for child views should I use many adapters? When I try to use ExpandableListAdapter, It tells me to implement some 8 methods, should I use all those if yes how? The following code snippet is what which I have now:
This is my Admin Activity class:
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleCursorTreeAdapter;
import java.util.List;
public class AdminActivity extends AppCompatActivity {
Toolbar toolbar;
ExpandableListAdapter listAdapter;
List<String> titleText;
SQLiteDataBaseAdapter db;
ExpandableListView login, android, ios, testing, java, dotNet, os, hr, others;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
toolbar = (Toolbar) findViewById(R.id.appBar);
toolbar.setTitle(" Admin Screen");
toolbar.setTitleTextColor(Color.WHITE);
login = (ExpandableListView) findViewById(R.id.expandableListViewLogin);
android = (ExpandableListView) findViewById(R.id.expandableListViewAndroid);
ios = (ExpandableListView) findViewById(R.id.expandableListViewIos);
testing = (ExpandableListView) findViewById(R.id.expandableListViewTesting);
java = (ExpandableListView) findViewById(R.id.expandableListViewJava);
dotNet = (ExpandableListView) findViewById(R.id.expandableListViewDotNet);
os = (ExpandableListView) findViewById(R.id.expandableListViewOS);
hr = (ExpandableListView) findViewById(R.id.expandableListViewHR);
others = (ExpandableListView) findViewById(R.id.expandableListViewOthers);
// Lsit of values for header. One for each list view.
titleText.add("User Id Authentication");
titleText.add("Android Posts Authentication");
titleText.add("iOS Posts Authentication");
titleText.add("Testing Posts Authentication");
titleText.add("Java Posts Authentication");
titleText.add("Dot Net Posts Authentication");
titleText.add("OS Posts Authentication");
titleText.add("HR Posts Authentication");
titleText.add("Others Posts Authentication");
SQLiteDataBaseAdapter db = new SQLiteDataBaseAdapter(this);
List<String> childData = db.getAndroidList();
//setting the list adapter
listAdapter = new ExpandableListAdapter(this, titleText, childData);// this tells to implement some 8 methods, should I implement??
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_admin, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I have so many expandable list views in one screen The array list is for the headers one for each expandable list view, the children will be again list of values from database. Please let me know how to use expandable list view in my case. I am very new to android and this is the first time I am working on Expandable List View. All suggestions are welcome. Thanks in advance.
You can find good tutorials for Expandable listview in the following link.
http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
You can remove unwanted header and child from the String List(based on Admin/User) before give it as input to Expandable list view adapter
You should set adapter:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
toolbar = (Toolbar) findViewById(R.id.appBar);
toolbar.setTitle(" Admin Screen");
toolbar.setTitleTextColor(Color.WHITE);
login = (ExpandableListView) findViewById(R.id.expandableListViewLogin);
android = (ExpandableListView) findViewById(R.id.expandableListViewAndroid);
ios = (ExpandableListView) findViewById(R.id.expandableListViewIos);
testing = (ExpandableListView) findViewById(R.id.expandableListViewTesting);
java = (ExpandableListView) findViewById(R.id.expandableListViewJava);
dotNet = (ExpandableListView) findViewById(R.id.expandableListViewDotNet);
os = (ExpandableListView) findViewById(R.id.expandableListViewOS);
hr = (ExpandableListView) findViewById(R.id.expandableListViewHR);
others = (ExpandableListView) findViewById(R.id.expandableListViewOthers);
titleText.add("User Id Authentication");
titleText.add("Android Posts Authentication");
titleText.add("iOS Posts Authentication");
titleText.add("Testing Posts Authentication");
titleText.add("Java Posts Authentication");
titleText.add("Dot Net Posts Authentication");
titleText.add("OS Posts Authentication");
titleText.add("HR Posts Authentication");
titleText.add("Others Posts Authentication");
SQLiteDataBaseAdapter db = new SQLiteDataBaseAdapter(this);
List<String> childData = db.getAndroidList();
//setting the list adapter
listAdapter = new ExpandableListAdapter(this, titleText, childData);
ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);
listView.setAdapter(listAdapter);
}
**Its Working**
package com.keshav.myexpandablelistviewexampleworkinginactivity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// tODO get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// TODO preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
Toast.makeText(getApplicationContext(),
"Group Clicked " + listDataHeader.get(groupPosition),
Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
// TODO Colapse Here Using this... in android
int previousGroup = -1;
boolean flag = false;
#Override
public void onGroupExpand(int groupPosition) {
Log.e("keshav", "onGroupClick is -> " + groupPosition);
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
if (groupPosition != previousGroup && flag) {
expListView.collapseGroup(previousGroup);
}
previousGroup = groupPosition;
flag = true;
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Todo Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Months");
listDataHeader.add("Top 250");
listDataHeader.add("Now Showing");
listDataHeader.add("Coming Soon..");
// Adding child data
List<String> weeks = new ArrayList<String>();
weeks.add("Sunday");
weeks.add("Monday");
weeks.add("Tuesday");
weeks.add("Wednesday");
weeks.add("Thursday");
weeks.add("Friday");
weeks.add("Saturday");
// Adding child data
List<String> top250 = new ArrayList<String>();
top250.add("Om Shanti Om");
top250.add("Badshah");
top250.add("Bahubali Part 1");
top250.add("Carry on Jatta");
top250.add("Sholey");
top250.add("Mard");
top250.add("Dewwar");
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("Bahubali");
nowShowing.add("Kabali");
nowShowing.add("Luckky Di Unlukky Story");
nowShowing.add("Sachin Billions Dream");
nowShowing.add("Red 2");
List<String> comingSoon = new ArrayList<String>();
comingSoon.add("Tubelight ");
comingSoon.add("Bahubali 3 2018");
comingSoon.add("Dhoom 4");
comingSoon.add("Hindi Medium");
listDataChild.put(listDataHeader.get(0), weeks);
listDataChild.put(listDataHeader.get(1), top250); // Header, Child data
listDataChild.put(listDataHeader.get(2), nowShowing);
listDataChild.put(listDataHeader.get(3), comingSoon);
}
}
package com.keshav.myexpandablelistviewexampleworkinginactivity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// tODO get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// TODO preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
Toast.makeText(getApplicationContext(),
"Group Clicked " + listDataHeader.get(groupPosition),
Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
// TODO Colapse Here Using this... in android
int previousGroup = -1;
boolean flag = false;
#Override
public void onGroupExpand(int groupPosition) {
Log.e("keshav", "onGroupClick is -> " + groupPosition);
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
if (groupPosition != previousGroup && flag) {
expListView.collapseGroup(previousGroup);
}
previousGroup = groupPosition;
flag = true;
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Todo Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Months");
listDataHeader.add("Top 250");
listDataHeader.add("Now Showing");
listDataHeader.add("Coming Soon..");
// Adding child data
List<String> weeks = new ArrayList<String>();
weeks.add("Sunday");
weeks.add("Monday");
weeks.add("Tuesday");
weeks.add("Wednesday");
weeks.add("Thursday");
weeks.add("Friday");
weeks.add("Saturday");
// Adding child data
List<String> top250 = new ArrayList<String>();
top250.add("Om Shanti Om");
top250.add("Badshah");
top250.add("Bahubali Part 1");
top250.add("Carry on Jatta");
top250.add("Sholey");
top250.add("Mard");
top250.add("Dewwar");
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("Bahubali");
nowShowing.add("Kabali");
nowShowing.add("Luckky Di Unlukky Story");
nowShowing.add("Sachin Billions Dream");
nowShowing.add("Red 2");
List<String> comingSoon = new ArrayList<String>();
comingSoon.add("Tubelight ");
comingSoon.add("Bahubali 3 2018");
comingSoon.add("Dhoom 4");
comingSoon.add("Hindi Medium");
listDataChild.put(listDataHeader.get(0), weeks);
listDataChild.put(listDataHeader.get(1), top250); // Header, Child data
listDataChild.put(listDataHeader.get(2), nowShowing);
listDataChild.put(listDataHeader.get(3), comingSoon);
}
}
activity_main.xml
<?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="vertical"
android:background="#f4f4f4" >
<ExpandableListView
android:id="#+id/lvExp"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:cacheColorHint="#00000000"/>
</LinearLayout>
list_group.xml
<?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="vertical"
android:padding="8dp"
android:background="#000000">
<TextView
android:id="#+id/lblListHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textSize="17dp"
android:textColor="#f9f93d" />
</LinearLayout>
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="55dip"
android:orientation="vertical" >
<TextView
android:id="#+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17dip"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textColor="#000000"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
</LinearLayout>
This is my Activity I used to display my items in Expandable List View. At the moment I have hard coded the values which should come to the Expandable List View.
String subTotal = getIntent().getStringExtra("subTotal"); // I have these 2 values
String price = getIntent().getStringExtra("price");
These are the 2 values I wanna load dynamically.
In the below I have prepareListData(), where I have hard coded values to my ExpandableListView.
listDataHeader.add("Top 250"); // subTotal
List<String> top250 = new ArrayList<String>();
top250.add("The Shawshank"); //price
I want to know how to replace these hard coded values with my dynamic values. Any help would be highly appreciated.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cart_activity);
String subTotal = getIntent().getStringExtra("subTotal"); // i ahve these 2 values
String price = getIntent().getStringExtra("price");//
ActionBar actionBar = getActionBar();
getActionBar().setIcon(
new ColorDrawable(getResources().getColor(android.R.color.transparent)));
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
// Enabling Back navigation on Action Bar icon
actionBar.setDisplayHomeAsUpEnabled(true);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
Toast.makeText(getApplicationContext(),
"Group Clicked " + listDataHeader.get(groupPosition),
Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Top 250"); // subTotal
// Adding child data
List<String> top250 = new ArrayList<String>();
top250.add("The Shawshank"); //price
listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
mExpandableList = (ExpandableListView)findViewById(R.id.lvExp);
mExpandableList.setIndicatorBounds(width - GetPixelFromDips(50), width - GetPixelFromDips(10)); }
public int GetPixelFromDips(float pixels) {
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (pixels * scale + 0.5f);
}
I was able to overcome this by doing.
String price;
String subTotal ;
subTotal = getIntent().getStringExtra("subTotal");
price = getIntent().getStringExtra("price");
listDataHeadersubTotal= new ArrayList<String>();
listDataHeaderPrice= new ArrayList<String>();
listDataHeadersubTotal.add(subTotal);
listDataHeaderPrice.add(price);
List<String> price= new ArrayList<String>();
price.add("The Shawshank");
List<String> subTotal= new ArrayList<String>();
subTotal.add("The Shawshank");
listDataChild.put(listDataHeadersubTotal.get(0), subTotal);
listDataChild.put(listDataHeaderPrice.get(0), price);
I have an expandle listview witha few children. If i click a child i want it to open it own activity. Right now i got it to work to go to 1 activity only. So when for example when i click on Mambo beach i want it to open an activity with the information of Mambo beach. And when i click on Avis car Rental it should open that acitivity
below my example:
public class TodoFragment extends Fragment {
public TodoFragment(){}
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_todo,container, false);
}
#Override
public void onStart() {
super.onStart();
// get the listview
expListView = (ExpandableListView) getView().findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_LONG).show();
return false;
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
//You have to create next activity say NextActivity.java
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding group data
listDataHeader.add("Culture");
listDataHeader.add("Beaches");
listDataHeader.add("Car Rental");
listDataHeader.add("Dinner");
// Adding child data
List<String> culture= new ArrayList<String>();
culture.add("Grotten van Hato");
culture.add("Ostrich Farm");
culture.add("Shete Boka national Park");
culture.add("Landhuis Knip");
culture.add("Christoffelpark");
culture.add("Navy Museum");
culture.add("Post Museum");
List<String> beaches = new ArrayList<String>();
beaches.add("Mambo Beach");
beaches.add("Knip");
beaches.add("Playa Kalki");
beaches.add("Westpunt");
beaches.add("Boca Santa Cruz");
beaches.add("Cas Abao");
beaches.add("Playa PortoMari");
beaches.add("Kontiki Beach");
beaches.add("Jan Thiel Beach");
List<String> car = new ArrayList<String>();
car.add("budget rental");
car.add("Avis Rental");
car.add("Alamo Car Rental");
car.add("Noordstar Rental");
car.add("Europa Rental");
List<String> dinner = new ArrayList<String>();
dinner.add("Truk di Pan");
dinner.add("Burger King");
dinner.add("Punda Food");
// Header, Child data
listDataChild.put(listDataHeader.get(0), culture);
listDataChild.put(listDataHeader.get(1), beaches);
listDataChild.put(listDataHeader.get(2), car);
listDataChild.put(listDataHeader.get(3), dinner);
}
}
You could implement this by simply creating, and launching, a different Intent based on the value of childPosition. For example:
switch (childPosition)
{
case 0:
Intent intentChild0 = new Intent(yourMainActivity.this, ActivityForChild0.class);
startActivity(intentChild0);
break;
case 1:
Intent intentChild1 = new Intent(yourMainActivity.this, ActivityForChild1.class);
startActivity(intentChild1);
break;
//Etc...
default:
break;
}
Adding this to your OnChildClickListener will cause a different Activity to be launched depending on what child was clicked.
There are 2 solutions: one is switch the child position and redirect the activity.But this solution has a problem, because you will have many activities and your project will not be ordered.
The other solution is implement one activity and many fragments depends of how many child you have. In your activity you only redirect the fragment depends of the child id.
I want to add a button in every list item and when the user press it to make a phone call. But when the user press the text, nothing happens..is that possible? This is my code:
public class museum extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.museum);
ListView list = (ListView) findViewById(R.id.list);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", "Archaeological Museum of Chania");
map.put("address", "Chalidon 21 , Chania");
mylist.add(map);
map = new HashMap<String, String>();
map.put("name", "Byzantine Museum");
map.put("address", "Theotokopoulou 82 , Chania");
mylist.add(map);
// ...
ListAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.row_museum,
new String[] {"name", "address"}, new int[] {R.id.TextView01, R.id.TextView02});
list.setAdapter(mSchedule);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch( position )
{
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
}
}
});
}
}
Ya you can do. For that
You will have to create a layout file for the list row that contains the textview and the button.
Use that layout inside a customized ArrayAdapter.
See an eg in this site.
You must have a custom list adapter like the one given below.
public class CustomListAdapter extends BaseAdapter {
private ArrayList<SingleElementDetails> allElementDetails;
private Context con;
private LayoutInflater mInflater;
public CustomListAdapter(Context context, ArrayList<SingleElementDetails> results) {
allElementDetails = results;
mInflater = LayoutInflater.from(context);
con=context;
public View getView(int position, View convertView, ViewGroup parent)
{
convertView = mInflater.inflate(R.layout.listview1, null);
Button bt=(Button)convertView.findViewById(R.id.bt);
TextView textview1= (TextView) convertView.findViewById(R.id.dishname_entry);
TextView textview2 = (TextView) convertView.findViewById(R.id.category_entry);
TextView textview3=(TextView)convertView.findViewById(R.id.description_entry);
textview1.setText(allElementDetails.get(position).getDishName());
textview2.setText(allElementDetails.get(position).getCategory());
textview3.setText(allElementDetails.get(position).getDescription());
bt.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent intent=new Intent(con,MainActivity.class);
con.startActivity(intent);
}
});
return convertView;
}
}
you can use a Custom adapter like this:
ListView lv_ArchivePartylist;
ArrayList<Parties> select_archived_party;
lv_ArchivePartylist = (ListView)findViewById(R.id.archive_ListView01);
lv_ArchivePartylist.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
// TODO Auto-generated method stub
if(view.findViewById(R.id.img_chkbox_archive).getVisibility()==TextView.GONE)
{
view.findViewById(R.id.img_chkbox_archive).setVisibility(TextView.VISIBLE);
Toast.makeText(ctx_archive, "Name="+archived_parties.get(position).getPartyTitle(), Toast.LENGTH_SHORT).show();
select_archived_party.add(archived_parties.get(position));
}
}
});
Then,instead of textview,use buttons and on that button's individual click event,you can write the code to make a call...Hope it helps:-)