We are developing an Android application which creates plans and schedules. I have an ExpandableListView and I need to add deleting buttons where the user can delete a specific schedule or the complete plan. The Schedule item is the Child of the Plan item.
I was developing deleting the schedule part first. I thought that each child item can include one text view and the deleting button. This is the schedule_group.xml:
<?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"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.8">
<TextView
android:id="#+id/scheduleGroup"
android:layout_width="355dp"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textColor="#android:color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2">
<ImageButton
android:id="#+id/scheduleDeleteBtn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:srcCompat="#drawable/delete_icon"
android:clickable="true"
></ImageButton>
</LinearLayout>
</LinearLayout>
And here is the PlansFragment.java where I create and delete the plans and the schedules.
package com.example.easyplan;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* A simple {#link Fragment} subclass.
*/
public class PlansFragment extends Fragment {
MainAdapter mainAdapter;
ExpandableListView planExplandable;
List<String> listPlanName;
HashMap<String, List<String>> listSchedules;
ImageButton scheduleDeleteBtn;
TextView textView;
static int count = 0;
View view;
public PlansFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_plans_fragment, container, false);
listPlanName = new ArrayList<String>();
listSchedules = new HashMap<String, List<String>>();
scheduleDeleteBtn = view.findViewById(R.id.scheduleDeleteBtn);
scheduleDeleteBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
deleteSchedule();
}
});
// get the listview
planExplandable = (ExpandableListView) view.findViewById(R.id.plansExpandable);
textView = view.findViewById(R.id.scheduleGroup);
createAPlan(3);
createAPlan(1);
createAPlan(3);
createAPlan(4);
mainAdapter = new MainAdapter(getActivity(), listPlanName, listSchedules);
// setting list adapter
planExplandable.setAdapter(mainAdapter);
return view;
}
The problem is here:
private void deleteSchedule() {
}
private void createAPlan(int sch){
listPlanName.add("Plan#"+(count+1));
List<String> schedules = new ArrayList<String>();
if(sch > 0 ){
for (int i = 1; i <= sch; i++){
schedules.add("Schedule#" + i);
}
}
listSchedules.put(listPlanName.get(count),schedules);
count++;
}
}
I'm not sure how to access the child item via the button. If you could help that would be great! Thank you.
I added the remove part under getChildView() method. If anyone needs a similar solution:
package com.example.easyplan;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
public class MainAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> listPlan; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> listScheduleMap;
ImageButton scheduleDeleteBtn;
public MainAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this.listPlan = listDataHeader;
this.listScheduleMap = listChildData;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this.listScheduleMap.get(this.listPlan.get(groupPosition))
.get(childPosititon);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.schedule_group, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.scheduleGroup);
scheduleDeleteBtn = convertView.findViewById(R.id.scheduleDeleteBtn);
scheduleDeleteBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
List<String> plan = listScheduleMap.get(listPlan.get(groupPosition));
plan.remove(childPosition);
notifyDataSetChanged();
}
});
txtListChild.setText(childText);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this.listScheduleMap.get(this.listPlan.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this.listPlan.get(groupPosition);
}
#Override
public int getGroupCount() {
return this.listPlan.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.plan_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.planGroup);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Related
i have tried several methods to make my items clickable i have read about focus and added the lines in my XML file Relative layout. nothing seems to help. the click listener still doesn't seem to do anything for me.. please help!!!
this is my activity list view class:
package com.example.trezah12.adminmodule;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class AdminList extends AppCompatActivity {
CustomAdapterAdmin adapterAdmin;
ArrayList<Admin> list;
ListView list1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_list);
DBhandler dBhandler = new DBhandler(this);
list = new ArrayList<>();
list1 = (ListView) findViewById(R.id.listview4);
viewData();
list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long Id) {
Intent intent = new Intent(AdminList.this, LaunchActivity.class);
}
});
}
private void viewData() {
final DBhandler dbHandler3 = new DBhandler(AdminList.this);
Cursor cursor = dbHandler3.viewData();
if (cursor.getCount() == 0){
Toast toast = Toast.makeText(getApplicationContext(), "Sorry no Data Found!!", Toast.LENGTH_SHORT);
}
else {
while (cursor.moveToNext()) {
Admin admin = new Admin();
admin.setUsername(cursor.getString(0));
list.add(admin);
list1 = (ListView) findViewById(R.id.listview4);
adapterAdmin = new CustomAdapterAdmin(list, AdminList.this);
list1.setAdapter(adapterAdmin);
}
}
}
}
below is my custom adapter class for admin:
package com.example.trezah12.adminmodule;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by trezah12 on 23/10/2018.
*/
public class CustomAdapterAdmin extends BaseAdapter {
private List<Admin> adminList = new ArrayList<Admin>();
private Context activity;
private static LayoutInflater inflater = null;
public CustomAdapterAdmin(List<Admin> adminList, Context activity) {
this.adminList = adminList;
this.activity = activity;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return adminList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (convertView == null) {
v = inflater.inflate(R.layout.customeadmin, null);
}
Admin admin = adminList.get(position);
TextView txt1 = (TextView) v.findViewById(R.id.textView1);
txt1.setText(admin.getUsername());
TextView txt2 = (TextView) v.findViewById(R.id.textView2);
txt2.setText(admin.getPassword());
return v;
}
}
Below is my XML file for list view
<?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"
>
<ListView
android:id="#+id/listview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</ListView>
</RelativeLayout>
that is because yout did not start the activity
list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long Id) {
Intent intent = new Intent(AdminList.this, LaunchActivity.class);
startActivity(intent);
}
});
I need to add onClick to the child items in ExpandableListView. I have reviewed other posts regarding this, but I could not integrate the code into mine, possibly due to a different variation of ExpandableListView codes.
It would be great if you can provide some in code explanation as well. Many thanks.
Here are my source codes:
activity_main.xml
`<?xml version="1.0" encoding="utf-8"?>
RelativeLayout 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=".MainActivity">
<ExpandableListView
android:id="#+id/expLV"
android:layout_width="match_parent"
android:layout_height="match_parent"></ExpandableListView>
</RelativeLayout>`
list_parent.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="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/listP"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="?
android:attr/expandableListPreferredItemPaddingLeft"
android:textSize="20dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
/>
</LinearLayout>
list_child.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">
<TextView
android:id="#+id/listC"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="?
android:attr/expandableListPreferredChildPaddingLeft"
android:textSize="14dp"
android:paddingBottom="20dp"
android:paddingTop="20dp"/>
</LinearLayout>
ExpandableListAdapter.java
package com.example.ehsan.myexplistview;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataHeader;
private HashMap<String, List<String>> listHashMap;
public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listHashMap) {
this.context = context;
this.listDataHeader = listDataHeader;
this.listHashMap = listHashMap;
}
#Override
public int getGroupCount() {
return listDataHeader.size();
}
#Override
public int getChildrenCount(int i) {
return listHashMap.get(listDataHeader.get(i)).size();
}
#Override
public Object getGroup(int i) {
return listDataHeader.get(i);
}
#Override
public Object getChild(int i, int i1) {
return listHashMap.get(listDataHeader.get(i)).get(i1);
}
#Override
public long getGroupId(int i) {
return i;
}
#Override
public long getChildId(int i, int i1) {
return i1;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
String parentText = (String)getGroup(i);
if (view == null)
{
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.list_parent, null);
}
TextView listP = (TextView)view.findViewById(R.id.listP);
listP.setTypeface(null, Typeface.BOLD);
listP.setText(parentText);
return view;
}
#Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
final String childText = (String)getChild(i,i1);
if (view == null)
{
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.list_child, null);
}
TextView listC = (TextView)view.findViewById(R.id.listC);
listC.setText(childText);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(i1==0){
Intent intent = new Intent(activity,OneTwoThree.class);
activity.startActivity(intent);
}
else if (i1 ==1){
Intent intent = new Intent(activity,FourFiveSix.class);
activity.startActivity(intent);
}
else if (i1 ==2){
Intent intent = new Intent(activity,SevenEightNine.class);
activity.startActivity(intent);}
else if (i1 ==3){
Intent intent = new Intent(activity,TenElevenTwelve.class);
activity.startActivity(intent);}
}
});
return view;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
MainActivity.java
package com.example.ehsan.myexplistview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ExpandableListView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ExpandableListView expandableListView;
private ExpandableListAdapter expandableListAdapter;
private List<String> listP;
private HashMap<String, List<String>> listC;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandableListView = (ExpandableListView)findViewById(R.id.expLV);
initData();
expandableListAdapter = new ExpandableListAdapter(this, listP, listC);
expandableListView.setAdapter(expandableListAdapter);
}
private void initData(){
listP = new ArrayList<>();
listC = new HashMap<>();
listP.add("ABC");
listP.add("DEF");
listP.add("GHI");
listP.add("JKL");
List <String> abc = new ArrayList<>();
abc.add("123");
List <String> def = new ArrayList<>();
def.add("456");
def.add("789");
List <String> ghi = new ArrayList<>();
ghi.add("101112");
ghi.add("131415");
ghi.add("161718");
List <String> jkl = new ArrayList<>();
jkl.add("192021");
jkl.add("222324");
jkl.add("252627");
jkl.add("282930");
listC.put(listP.get(0),abc);
listC.put(listP.get(1),def);
listC.put(listP.get(2),ghi);
listC.put(listP.get(3),jkl);
}
}
You can set child click of expandable list in two ways
1.write child click event inside the getChildView() method.
#Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
Page page =(Page) getChild(groupPosition, childPosition);
convertView = inflater.inflate(R.layout.child_list_layout, null);
Button mButton=(Button)convertView.findViewById(R.id.button1);
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Your code goes here ....
}
});
return convertView;
}
2.write click directly from expandable listview.
mExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(
ExpandableListView parent, View v,
int groupPosition, int childPosition,
long id) {
GoCategory(mainMenusList.get(groupPosition)
.getPagesList().get(childPosition));
return false;
}
});
I'm trying to follow this guide on creating a ListView with two different layouts to create an activity for a settings menu but some parts are ambiguous to me and my attempts aren't working.
http://android.amberfog.com/?p=296
activity_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.example.austin.kanadrill.SettingsActivity">
<ListView
android:id="#+id/settingsListView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
SettingsActivity.java
package com.example.austin.kanadrill;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
public class SettingsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
ListView settingsListView = findViewById(R.id.settingsListView);
SettingsAdapter settingsAdapter = new SettingsAdapter();
settingsAdapter.addItem("Hiragana");
settingsAdapter.addItem("Katakana");
settingsAdapter.addItem("Fonts");
settingsAdapter.addCheckBoxItem("Speed Mode");
settingsListView.setAdapter(settingsAdapter);
}
}
SettingsAdapter.java
package com.example.austin.kanadrill;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.TreeSet;
public class SettingsAdapter extends BaseAdapter {
final int TYPE_ITEM = 0;
final int TYPE_CHECKBOX_ITEM = 1;
final int TYPE_MAX_COUNT = 2;
Context ctx;
ArrayList mData = new ArrayList();
LayoutInflater mInflator;
TreeSet mCheckBoxSet = new TreeSet();
public SettingsAdapter() {
mInflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
public void addCheckBoxItem(final String item) {
mData.add(item);
mCheckBoxSet.add(mData.size()-1);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
return mCheckBoxSet.contains(position) ? TYPE_CHECKBOX_ITEM : TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public String getItem(int position) {
return (String) mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if(convertView == null) {
holder = new ViewHolder();
switch(type) {
case TYPE_ITEM:
convertView = mInflator.inflate(android.R.layout.simple_list_item_1, null);
holder.textView = (TextView) convertView.findViewById(R.id.settingsListView);
break;
case TYPE_CHECKBOX_ITEM:
convertView = mInflator.inflate(android.R.layout.simple_list_item_multiple_choice, null);
holder.textView = (TextView) convertView.findViewById(R.id.settingsListView);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText((String)mData.get(position));
return convertView;
}
private static class ViewHolder {
public TextView textView;
}
}
I faced issues especially in the constructor for SettingsAdapter because I think I needed to create a context to call getSystemService() and I doubt I did that properly. I also did not understand exactly what to do in getView() so that is likely wrong too. My goal is to just have a ListView where the first three Strings are simple_list_item_1 and a fourth string is simple_list_item_multiple_choice. How can I fix this code to accomplish that, or just start from scratch?
Thank you.
There are 2 issues in your code
Pass the Activity context to Settings Apdapter, It must be throwing exception.
SettingsActivity.java
SettingsAdapter settingsAdapter = new SettingsAdapter(this);
SettingsAdpater.java
public SettingsAdapter(Context context) {
ctx = context
mInflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
and for textview id, use android.R.id.text1 (that's the id defined in R.layout.simple_list_item_1 and android.R.layout.simple_list_item_multiple_choice)
switch(type) {
case TYPE_ITEM:
convertView = mInflator.inflate(android.R.layout.simple_list_item_1, null);
holder.textView = (TextView) convertView.findViewById(android.R.id.text1);
break;
case TYPE_CHECKBOX_ITEM:
convertView = mInflator.inflate(android.R.layout.simple_list_item_multiple_choice, null);
holder.textView = (TextView) convertView.findViewById(android.R.id.text1);
break;
}
Let me know if you didn't understand any part, happy to help.
I am encountering errors when trying to use the constructor of my arrayAdapter for a listView. When I call it like this:
ListArrayAdapter adapters = new ListArrayAdapter(this, R.layout.list_view_row_item, foodList);
I get the error (in eclipse):
the constructor ListArrayAdapter(Context, int, ArrayList<ListItem>) is undefined.
This is the code for the arrayAdapter:
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import android.app.Activity;
import android.app.LauncherActivity.ListItem;
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;
public class ListArrayAdapter extends ArrayAdapter<ListItem> {
Context mContext;
int layoutResourceId;
ListItem data[] = null;
private ProgressBar progressBar;
private int progressStatus = 0;
private TextView textView;
private Handler handler = new Handler();
ArrayList<ListItem> foodList = new ArrayList<ListItem>();
public ListArrayAdapter(Context mContext, int layoutResourceId, ArrayList<ListItem> foodList) {
super(mContext, layoutResourceId, foodList);
this.layoutResourceId = layoutResourceId;
this.mContext = mContext;
this.foodList = foodList;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
// inflate the layout
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
}
return convertView;
}
The main part of my program:
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
import com.app.fridgelist.*;
public class MainScreen extends Activity {
private static Calendar d = Calendar.getInstance();
private static Date kurrentTime = d.getTime();
public int listItems;
public static Context mainScreen;
public ArrayList<ListItem> foodList = new ArrayList<ListItem>();
private static ListItem newFood1 = new ListItem(0, "Pasta", 99, (int) (kurrentTime.getTime()));
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
if (savedInstanceState == null) {
//getFragmentManager().beginTransaction()
//.add(R.id.container, new PlaceholderFragment())
//.commit();
}
mainScreen = this;
foodList.add(newFood1);
listItems = 1;
ListArrayAdapter adapters = new ListArrayAdapter(getBaseContext(), R.layout.list_view_row_item, foodList);
ListView yourListView = (ListView) findViewById(R.id.listOne);
//yourListView.setAdapter(adapters);
//yourListView.setOnItemClickListener(new OnItemClickListener() {
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_screen, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_screen, container, false);
return rootView;
}
}
}
// try this way,hope this will help you...
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/lstFood"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="#+id/txtFood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private ListView lstFood;
ArrayList<HashMap<String,String>> foodList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lstFood = (ListView) findViewById(R.id.lstFood);
foodList = new ArrayList<HashMap<String, String>>();
HashMap<String,String> food1 = new HashMap<String,String>();
food1.put("id","1");
food1.put("name","pasta");
food1.put("prize","99");
food1.put("time",String.valueOf(Calendar.getInstance().getTime()));
HashMap<String,String> food2 = new HashMap<String,String>();
food2.put("id","2");
food2.put("name","pizza");
food2.put("prize","199");
food2.put("time",String.valueOf(Calendar.getInstance().getTime()));
foodList.add(food1);
foodList.add(food2);
CustomAdapter adapters = new CustomAdapter(this,foodList);
lstFood.setAdapter(adapters);
lstFood.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HashMap<String,String> food = foodList.get(position);
System.out.println("Food Id : "+food.get("id"));
System.out.println("Food Name : "+food.get("name"));
System.out.println("Food Prize : "+food.get("prize"));
System.out.println("Food Time : "+food.get("time"));
}
});
}
class CustomAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<HashMap<String,String>> foodList;
public CustomAdapter(Context mContext,ArrayList<HashMap<String,String>> foodList) {
this.mContext = mContext;
this.foodList = foodList;
}
#Override
public Object getItem(int position) {
return foodList.get(position);
}
#Override
public int getCount() {
return foodList.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
holder.txtFood = (TextView) convertView.findViewById(R.id.txtFood);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.txtFood.setText(foodList.get(position).get("name"));
return convertView;
}
class ViewHolder{
TextView txtFood;
}
}
}
Change here from
ListArrayAdapter adapters = new ListArrayAdapter(getBaseContext(), R.layout.list_view_row_item, foodList);
to
ListArrayAdapter adapters = new ListArrayAdapter(YourActivityName.this, R.layout.list_view_row_item, foodList);
yourListView.setAdapter(adapters);
Hi all i have problemm that getView() is not called. Sup please. And in project i am using ViewPager. So maybe it's becouse of that
Here my MainActivity
package com.uinleader.animewatcher;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
private List<View> pages;
private View recent;
private View top_seven;
private ViewPager mPager;
private final ArrayList<Parsed> info = new ArrayList<Parsed>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUi();
LVInit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(android.R.menu.main, menu);
return true;
}
private void initUi() {
LayoutInflater inflater = LayoutInflater.from(this);
pages = new ArrayList<View>();
recent = inflater.inflate(R.layout.recent, null);
recent.setTag(getString(R.string.recent));
top_seven = inflater.inflate(R.layout.top_seven, null);
top_seven.setTag(getString(R.string.top));
pages.add(recent);
pages.add(top_seven);
PAdapter adapter = new PAdapter(pages);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(adapter);
mPager.setCurrentItem(0);
}
private void LVInit() {
LayoutInflater inflater = LayoutInflater.from(this);
Parsed one = new Parsed("One", "1", R.drawable.ic_launcher);
Parsed two = new Parsed("Two", "2", R.drawable.ic_launcher);
Parsed three = new Parsed("Three", "3", R.drawable.ic_launcher);
View v = inflater.inflate(R.layout.top_seven, null);
info.add(one);
info.add(two);
info.add(three);
ListView lv = (ListView) v.findViewById(R.id.listView1);
MArrayAdapter radapter = new MArrayAdapter(MainActivity.this, R.id.listView1, info);
lv.setAdapter(radapter);
}
}
Here is adapter
package com.uinleader.animewatcher;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by uinleader on 28.09.13.
*/
public class MArrayAdapter extends ArrayAdapter<Parsed> {
private final Activity activity;
private final ArrayList<Parsed> items;
public MArrayAdapter(Activity a, int textViewResourceId, ArrayList<Parsed> items) {
super(a, textViewResourceId, items);
activity = a;
this.items = items;
Log.e("ListViewDebug", "Inside Adapter");
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.e("ListViewDebug", "Start Function");
View view = convertView;
ViewHolder holder;
if (view == null) {
Log.e("ListViewDebug", "In First IF");
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.row_layout, parent, false);
holder = new ViewHolder();
holder.title = (TextView) view.findViewById(R.id.anime_title);
holder.ep_num = (TextView) view.findViewById(R.id.ep_number);
holder.ep_preview = (ImageView) view.findViewById(R.id.ep_preview);
view.setTag(holder);
}else {
holder = (ViewHolder) view.getTag();
}
Parsed item = items.get(position);
if(item!=null) {
holder.title.setText(item.anime_title);
holder.ep_num.setText("Episode: "+item.ep_num);
holder.ep_preview.setImageResource(item.img);
}
return view;
}
#Override
public int getCount() {
return items.size();
}
private static class ViewHolder {
public TextView title;
public TextView ep_num;
public ImageView ep_preview;
}
}
Here is my class for data. And XML's
package com.uinleader.animewatcher;
/**
* Created by uinleader on 28.09.13.
*/
public class Parsed {
public String anime_title;
public String ep_num;
public int img;
public Parsed (String anime_title, String ep_num, int img ){
this.anime_title = anime_title;
this.ep_num = ep_num;
this.img = img;
}
}
XML for list.
<?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" >
<ListView
android:id="#+id/listView1"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</ListView>
</LinearLayout>
XML for row's
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
>
<ImageView
android:layout_width="200px"
android:layout_height="200px"
android:id="#+id/ep_preview"
android:layout_column="0"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/anime_title"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/ep_preview" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ep_number"
android:layout_below="#+id/anime_title"
android:layout_toRightOf="#+id/ep_preview" />
</RelativeLayout>
The setContentView(R.layout.activity_main); already "inflates" the current layout the activity is using..i don't why you are inflating other stuff in the "init" methods, that is probably why nothing is being called because the activity remains displaying the view set on R.layout.activity_main.
ArrayAdapter works a bit differently than BaseAdapter - which is for what you're implementing your getView
I suggest several options to fix :
change your ArrayAdapter to BaseAdapter
or use the built in ArrayList that comes with the ArrayList adapter
in your constructor write the following :
public MArrayAdapter(Activity a, int textViewResourceId, ArrayList<Parsed> items) {
super(a, textViewResourceId, items); //<== call to super instantiates the items
activity = a;
//ArrayListAdapter has a built in arrayList - use it
Log.e("ListViewDebug", "Inside Adapter");
}
change your getView to this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.e("ListViewDebug", "Start Function");
View view = convertView; // **note why do you allocate again? just write convertview everywhere instead of view**
ViewHolder holder;
if (view == null) {
Log.e("ListViewDebug", "In First IF");
//**I recommend instantiating the inflater once inside the constructor**
LayoutInflater inflater =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.row_layout, parent, false);
holder = new ViewHolder();
holder.title = (TextView) view.findViewById(R.id.anime_title);
holder.ep_num = (TextView) view.findViewById(R.id.ep_number);
holder.ep_preview = (ImageView) view.findViewById(R.id.ep_preview);
view.setTag(holder);
}else {
holder = (ViewHolder) view.getTag();
}
Parsed item = (Parsed)getItem(position); //<==== this
if(item!=null) {
holder.title.setText(item.anime_title);
holder.ep_num.setText("Episode: "+item.ep_num);
holder.ep_preview.setImageResource(item.img);
}
return
view;
}