I have an Expandable listview and I'm trying to start a new activity by clicking on the child row, But it doesn't work with me
Also I want to add child items and parent items both dynamically to be able to change them with several strings from string.xml
package com.example.khloud.finaaaaaal_hospital;
import java.util.ArrayList;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;
public class Third extends ExpandableListActivity implements
OnChildClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ExpandableListView expandbleLis = getExpandableListView();
expandbleLis.setDividerHeight(2);
expandbleLis.setGroupIndicator(null);
expandbleLis.setClickable(true);
setGroupData();
setChildGroupData();
MyExpandableAdapter mNewAdapter = new MyExpandableAdapter(groupItem, childItem);
mNewAdapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),
this);
getExpandableListView().setAdapter(mNewAdapter);
expandbleLis.setOnChildClickListener(this);
}
public void setGroupData() {
groupItem.add("htc");
groupItem.add("sony");
}
ArrayList<String> groupItem = new ArrayList<String>();
ArrayList<Object> childItem = new ArrayList<Object>();
public void setChildGroupData() {
ArrayList<String> child = new ArrayList<String>();
child.add("1");
childItem.add(child);
child = new ArrayList<String>();
child.add("1");
child.add("2");
childItem.add(child);
}
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Intent child0Intent = new Intent(Third.this, Final_list.class);
startActivity(child0Intent);
return true;}}
Related
I am trying to get an index of a item in an array that is selectable in a list view. The only issue is that when i click on the item, it only returns the index of -1 which means that it doesnt match when in fact there should be a match. Thanks in advanced!
package com.goldleaf.branden.goldleafcomics;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.kosalgeek.android.json.JsonConverter;
import com.kosalgeek.genasync12.*;
import java.io.FileInputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class GlimpseListFragment extends ListFragment {
List<String> glimpse = new ArrayList<String>();
List<String> titles = new ArrayList<String>();
List<UniverseListing> universalListings = new ArrayList<UniverseListing>();
public GlimpseListFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.fragment_glimpse_list, container, false);
return rootView;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String url = "http://goldleafcomics.com/application/UniverseGlimpse.JSON";
PostResponseAsyncTask task = new PostResponseAsyncTask(getActivity(), new AsyncResponse() {
#Override
public void processFinish(String s) {
universalListings = new JsonConverter<UniverseListing>().toArrayList(s, UniverseListing.class);
Toast.makeText(getActivity(), "Application Data Refreshed", Toast.LENGTH_LONG).show();
ArrayList<String> glimpse = new ArrayList<String>();
for(UniverseListing value: universalListings){
glimpse.add(value.universeGlimpse);
}
ArrayList<String> titles = new ArrayList<String>();
for(UniverseListing value: universalListings){
titles.add(value.universeId);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, titles);
setListAdapter(adapter);
}
});
task.execute(url);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String value = (String)getListAdapter().getItem(position);
int index = this.titles.indexOf(value);
String value2 = Integer.toString(index);
Toast.makeText(getActivity(), value2, Toast.LENGTH_LONG).show();
}
}
you define glimpse two times, could you try to make those changes,
List<String> glimpse;
and inside onCreate initialize it
glimpse = new ArrayList<>();
and do the same for titles and universalListings.
Try using these two sections of code. You are shadowing the member variables with local variables of the same name.
public class GlimpseListFragment extends ListFragment {
List<String> glimpse;
List<String> titles;
List<UniverseListing> universalListings;
Later...
glimpse = new ArrayList<String>();
for(UniverseListing value: universalListings){
glimpse.add(value.universeGlimpse);
}
titles = new ArrayList<String>();
for(UniverseListing value: universalListings){
titles.add(value.universeId);
}
You don't need to initialize Arraylists with the class. You should typically lazily initialize them (in other words, when you need to)
This question already has answers here:
Cardview onclick opens a new activity
(2 answers)
Closed 6 years ago.
Everytime I click a different card an activity will be called like that EvsActivity.
But if no. of cards are 100 then i have to create 100 activity which is not a feasible solution.
so anybody with a better solution plz ur help will be appreciated
//Recyclerview with cardview code
package com.studyleague.app;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
public class AvailableContent extends TemplateFragment {
#Bind(R.id.available_recycler_view)
RecyclerView recyclerView;
public AvailableContent() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.available_content, container, false);
ButterKnife.bind(this, v);
recyclerView.setHasFixedSize(true);
GridLayoutManager glm = new GridLayoutManager(getActivity(), 2, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(glm);
List<AvailableContentModel> content = new ArrayList<>();
for (int i = 0; i < AvailableContentData.acName.length; i++) {
content.add(new AvailableContentModel(AvailableContentData.acIcon[i], AvailableContentData.acName[i]));
}
final AvailableContentAdapter contentAdapter = new AvailableContentAdapter(getActivity(), content);
recyclerView.setAdapter(contentAdapter);
return v;
}
}
//Adapter//
package com.studyleague.app;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
public class AvailableContentAdapter extends RecyclerView.Adapter<AvailableContentAdapter.ContentViewHolder> {
private final List<AvailableContentModel> dataSet;
private Context context;
public ArrayList<AvailableContentModel> content;
public AvailableContentAdapter(Context context, List<AvailableContentModel> content) {
this.context = context;
this.dataSet = content;
}
#Override
public ContentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.available_content_card, parent, false);
return new ContentViewHolder(view);
}
#Override
public void onBindViewHolder(final ContentViewHolder hold, final int listPosition) {
hold.acImg.setImageResource(dataSet.get(listPosition).getContentImg());
hold.acText.setText(dataSet.get(listPosition).getContentName());
}
#Override
public int getItemCount() {
return dataSet.size();
}
public class ContentViewHolder extends RecyclerView.ViewHolder {
#Bind(R.id.available_card_image_view)
ImageView acImg;
#Bind(R.id.available_card_text_view)
TextView acText;
public ContentViewHolder(final View view) {
super(view);
ButterKnife.bind(this, view);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (getAdapterPosition()) {
case 0:
Toast.makeText(v.getContext(), "Mechanics", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(v.getContext(), "B. E. E.", Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(v.getContext(), "Maths", Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(v.getContext(), "Chemistry", Toast.LENGTH_SHORT).show();
break;
case 4:
Toast.makeText(v.getContext(), "Physics", Toast.LENGTH_SHORT).show();
break;
case 5:
context.startActivity(new Intent(context, EvsActivity.class));
break;
default:
Toast.makeText(v.getContext(), "YOLO", Toast.LENGTH_SHORT).show();
break;
}
}
});
}
}
}
//Activity that is called when i click one of the card//
package com.studyleague.app;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
public class EvsActivity extends AppCompatActivity {
#Bind(R.id.exp_list)
ExpandableListView expListView;
private List<String> chap;
private HashMap<String, List<String>> hashMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_evs);
ButterKnife.bind(this);
// preparing list data
prepareListData();
// setting list adapter
ExpandableListAdapter listAdapter = new ExpandableListAdapter(this, chap, hashMap);
expListView.setAdapter(listAdapter);
// expListView on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
String groupName = chap.get(groupPosition);
String childName = hashMap.get(groupName).get(childPosition);
Toast.makeText(getApplication(), groupName + " : " + childName, Toast.LENGTH_SHORT)
.show();
Intent i = new Intent(EvsActivity.this, Study.class);
startActivity(i);
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
// Hash map for both header and child
hashMap = new HashMap<>();
// Array list for header
chap = new ArrayList<>();
// Adding headers to list
chap.add("1. Multidisciplinary Nature of Environmental Studies");
chap.add("2. Sustainable Development");
chap.add("3. Environmental Pollution");
chap.add("4. Environmental Legislation");
chap.add("5. Renewable Sources of Energy");
chap.add("6. Environment and Technology");
chap.add("7. Another Chapter");
chap.add("8. Almost Another Chapter");
// Array list for child items
List<String> child1 = new ArrayList<>(fillChildData(1, 5));
List<String> child2 = new ArrayList<>(fillChildData(2, 3));
List<String> child3 = new ArrayList<>(fillChildData(3, 11));
List<String> child4 = new ArrayList<>(fillChildData(4, 6));
List<String> child5 = new ArrayList<>(fillChildData(5, 4));
List<String> child6 = new ArrayList<>(fillChildData(6, 2));
List<String> child7 = new ArrayList<>(fillChildData(7, 2));
List<String> child8 = new ArrayList<>(fillChildData(8, 2));
// Adding header and children to hash map
hashMap.put(chap.get(0), child1);
hashMap.put(chap.get(1), child2);
hashMap.put(chap.get(2), child3);
hashMap.put(chap.get(3), child4);
hashMap.put(chap.get(4), child5);
hashMap.put(chap.get(5), child6);
hashMap.put(chap.get(6), child7);
hashMap.put(chap.get(7), child8);
}
private Collection<String> fillChildData(int index, int n) {
List<String> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
list.add("Group " + index + " - Child : " + (i + 1));
}
return list;
}
}
//Code of data incase needed//
package com.studyleague.app;
public class AvailableContentData {
static final Integer[] acIcon = {
R.mipmap.mec,
R.mipmap.elec,
R.mipmap.mat,
R.mipmap.chem,
R.mipmap.ic,
R.mipmap.env
};
static final String[] acName = {
"Mechanics",
"B. E. E.",
"Maths",
"Chemistry",
"Physics",
"E. V. S."
};
}
Typed this up under the wrong account (cant comment, sorry), didn't stick I guess. You create one Activity with one ListView and pass in the adapter that you want to use from the CardView intent.
Butterfly Cardview -> BUTTERFLY -> ButterflyAdapter
Baseball Bat Cardview -> BASEBALL -> BaseballBatAdatper
if your items are not related. If they are related you can use the same adapter but send in a different layout based on what you sent in.
Butterfly -> R.layout.butterfly_items
Whale -> R.layout.whale_items
New android programmer is here.
First : I dont know many things about list view and as I found out , its complicated to work with it.
So , I want to put my database data (Contains Id , Name) to a listview and get the Id of the item is clicked.
I have searched many but i just found this :
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class test extends Activity {
String[] wordlist = new String[] { "a", "b", "c" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wordlist[2] = "abds";
ListView list = new ListView(this);
list.setAdapter((ListAdapter) new MyAdapter(test.this, wordlist));
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Object entry= parent.getItemAtPosition(position);
Toast.makeText(test.this, entry.toString(), Toast.LENGTH_SHORT).show();
}
});
setContentView(list);
}
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, String[] strings) {
super(context, -1, -1, strings);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout listLayout = new LinearLayout(test.this);
listLayout.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT,
AbsListView.LayoutParams.WRAP_CONTENT));
listLayout.setId(5000);
TextView listText = new TextView(test.this);
listText.setId(5001);
listLayout.addView(listText);
listText.setText(super.getItem(position));
return listLayout;
}
}
}
Just i can show strings , not Id.
Use SimpleCursorAdapter to populate ListView with data from database, for example - https://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/ (first link from google).
How to get the item id in an onItemClick handler
Read more about ListView and Adapters, your code is awful )
I have a problem my custom List view.I have data in hash map and add into array list which is shown in below code.what is my problem is in my hash map contain 20 values and i try set into the list view but first data only display other or not.Thanks advance
This is my CustomAdapter
package com.example.node10.databasetesting;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static com.example.node10.databasetesting.DataBase.*;
public class ListViewEmployee extends AppCompatActivity implements View.OnClickListener {
private SimpleCursorAdapter dataAdapter;
private Button btn_view;
private Button btn_submit;
ArrayList<HashMap<Integer,String>> emp;
HashMap<Integer,String> map;
Set<Integer> keyValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_employee);
DataBase dataBase=new DataBase(this);
map=new HashMap<>();
map=dataBase.getValueEmpTable();
emp=new ArrayList<HashMap<Integer, String>>();
keyValue=map.keySet();
emp.add(map);
btn_view= (Button) findViewById(R.id.id_btn_emp_view);
btn_view.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.getId()==R.id.id_btn_emp_view){
displayList();
// selectItem();
}
}
private void displayList() {
//create the Arrayt adapter
ListView listview= (ListView) findViewById(R.id.id_listview);
final MyAdapter adapter=new MyAdapter(this,R.layout.custom_listview,emp);
listview.setAdapter(adapter);
}
public class MyAdapter extends ArrayAdapter<HashMap<Integer,String>>{
boolean[] checkBoxState;
ViewHolder viewholder;
public MyAdapter(Context context, int resource,ArrayList<HashMap<Integer,String>> map) {
super(context, resource, map);
// create the boolean array for check box selection
checkBoxState= new boolean[map.size()];
}
// create the class for caching the view
class ViewHolder{
TextView txtView;
CheckBox checkBox;
}
#Override
public View getView( final int position, View convertView, ViewGroup parent) {
if(convertView==null){
LayoutInflater objlayout= (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=objlayout.inflate(R.layout.custom_listview,null);
viewholder=new ViewHolder();
viewholder.txtView= (TextView) convertView.findViewById(R.id.id_checkbox_textview);
viewholder.checkBox= (CheckBox) convertView.findViewById(R.id.id_checkbox);
convertView.setTag(viewholder);
}else{
viewholder = (ViewHolder) convertView.getTag();}
// viewholder.txtView.setText(emp.get(position).toString());
viewholder.txtView.setText(emp.get(position).get(position+1).toString());
viewholder.checkBox.setChecked(checkBoxState[position]);
viewholder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
checkBoxState[position] = true;
} else {
checkBoxState[position] = false;
}
}
});
return convertView;
}
}
}
Now you put to your adapter ArrayList of HashMap. I don't know why are you doing that, but now your ArrayList emp contains ony one element - that was added in line
emp.add(map);
So, ArrayAdapter for array with size = 1 will show 1 elemet.
If you want to show in list all elements from your map. I suggest to use
ArrayList emp and comvert map to list by using map.values()
ArrayList<String> emp = (ArrayList<String>)map.values();
Then change your adapter to
public class MyAdapter extends ArrayAdapter<String>
I am new to android please help me out for this problem.
In expandable list view clicking on each child list new activity should open.
here is my code
package com.example.index;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;
public class IndexMainActivity 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_index_main);
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
}
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Part 1");
listDataHeader.add("Part 2");
listDataHeader.add("Part 3");
// Adding child data
List<String> parta = new ArrayList<String>();
parta.add("Sweet Hour Of Prayer");
parta.add("Prayer and Royal Family");
parta.add("The Holy Bible-King James Version");
parta.add("William shakespeare - Scriptres about prayer");
List<String> partb = new ArrayList<String>();
partb.add("Samuel Rutherford/scriptures-Thankfulness");
List<String> partc = new ArrayList<String>();
partc.add("Matthew Henry/Scriptures on - Faith");
partc.add("John Wesley/Scriptures on - Freedom");
partc.add("Charles Simeon/Scriptures on -Protection");
partc.add("Christmas Evans/Scriptures on - Guidance");
listDataChild.put(listDataHeader.get(0), parta); // Header, Child data
listDataChild.put(listDataHeader.get(1), partb);
listDataChild.put(listDataHeader.get(2), partc);
}
}
there are three list i.e part a , part b and part c now by clicking on part a i will get four child list by clicking on first child list i.e Sweet Hour of prayer new activity should open.
Try Below Code:
public class MyActivity extends Activity implements ExpandableListView.OnChildClickListener
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.event_mainactivity);
mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
ExpandableListAdapter adapter = new ExpandableListAdapter(this,
mExpandableListView, mGroupCollection);
mExpandableListView.setAdapter(adapter);
mExpandableListView.setOnChildClickListener(this);
}
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
{
Toast.makeText(getApplicationContext(), "Go to Activity :: "+childPosition, Toast.LENGTH_LONG).show();
return true;
}
}
There is a onChildClickListener for ExpandableListView. Check this code:
yourListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// Open activity using intent...
return false;
}
});
Hope it helps!