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);
Related
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;
}
}
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 am trying to load a fragment from the SideMenu. There is a listview in the side menu from where I want to load a Fragment in the application. There are 3 items in the list view. A code for clicking on 0th and 3rd position is working fine, but not working for 1st position ie i==1.
what i have to do for this !
package comm.design.amer.sidemenu_new;
import android.app.FragmentManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;<code>
public class MainActivity extends AppCompatActivity {
private ArrayList<listView> Listview = new ArrayList<listView>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slideview);
//Create the ListView Items
Listview.add(new listView("page1", "enter", R.drawable.a));
Listview.add(new listView("page2", "enter", R.drawable.b));
Listview.add(new listView("page3", "enter", R.drawable.c));
Listview.add(new listView("page4", "enter", R.drawable.d));
//Call the Adapter
mycomstumerAdapter adapter = new mycomstumerAdapter(this, Listview);
ListView listview = (ListView) findViewById(R.id.list);
listview.setAdapter(adapter);
//make selection
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
selectItem(i);
}
});
}
public void selectItem (int i){
if (i==0){
Toast.makeText(getApplicationContext(),"bn",Toast.LENGTH_LONG).show();}
else if (i==1) {
Fragment1 fragment0 = new Fragment1();
FragmentManager fragmentm = getFragmentManager();
fragmentm.beginTransaction()
.add(R.id.linear,fragment0)
.commit();
}
else if (i==2)
finish();
}
public class mycomstumerAdapter extends BaseAdapter {
Context context;
ArrayList<listView> Listview;
public mycomstumerAdapter(Context context, ArrayList<listView> Listview) {
this.context = context;
this.Listview = Listview;
}
#Override
public int getCount() {
return Listview.size();
}
#Override
public Object getItem(int i) {
return Listview.get(i);
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View view1;
if (view == null) {
LayoutInflater inflater = getLayoutInflater();
view1 = inflater.inflate(R.layout.listview, null);
} else
view1 = view;
TextView title = (TextView) view1.findViewById(R.id.textView);
TextView detail = (TextView) view1.findViewById(R.id.textView2);
ImageView imageView = (ImageView) view1.findViewById(R.id.imageView);
title.setText(Listview.get(i).Title);
detail.setText(Listview.get(i).Detail);
imageView.setImageResource(Listview.get(i).imageView);
return view1;
}
}
}
In your code I have seen you are using add fragment instead of that try with replacing the fragment. See the below code:-
Fragment1 fragment0 = new Fragment1();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.linear,fragment0);
fragmentTransaction.commit();
This question already has answers here:
What does the "Assigned value is never used" warning mean?
(5 answers)
Closed 7 years ago.
After creating my activity, I get this warning:
Variable 'station' is never used
I not certain as to what that warning means nor how to fix it. I'm guessing the 'station' variable hasn't been declared however I don't know the correct way to declare it. All help would be appreciated.
FragmentWCLine.java
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class FragmentWCLine extends android.support.v4.app.Fragment {
public final static String EXTRA_MESSAGE = "Station_key";
private class WC {
private CharSequence station;
private CharSequence zone;
private Class<? extends Activity> activityClass;
private Class<? extends android.support.v4.app.Fragment> fragmentClass;
public WC(int stationResId, int zoneResId, Class<? extends Activity> activityClass, Class<? extends android.support.v4.app.Fragment> fragmentClass) {
this.fragmentClass = fragmentClass;
this.activityClass = activityClass;
this.station = getResources().getString(stationResId);
this.zone = getResources().getString(zoneResId);
}
#Override
public String toString() { return station.toString(); }
public String getzone(){ return zone.toString(); }
}
private static WC[] mWC;
private boolean mTwoPane;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_wc_line, container, false);
// Instantiate the list of stations.
mWC = new WC[]{
new WC(R.string.bank, R.string.zone_1, WCBankActivity.class, FragmentWCBank.class),
new WC(R.string.wat, R.string.zone_1, WCWATActivity.class, FragmentWCWAT.class)
};
final ListView listView = (ListView)v.findViewById(R.id.list_wc);
listView.setAdapter(new MyAdapter(getActivity(), mWC));
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(mTwoPane){
setItemNormal();
View rowView = view;
setItemSelected(rowView);
}
else{
Intent intent = new Intent(getActivity(), mWC[position].activityClass);
String station = mWC[position].station.toString();
intent.putExtra(EXTRA_MESSAGE, station);
startActivity(intent);
}
}
public void setItemSelected(View view){
View rowView = view;
view.setBackgroundColor(Color.parseColor("#66CCCC"));
TextView tv0 = (TextView)rowView.findViewById(R.id.list_item_station);
tv0.setTextColor(Color.parseColor("#000099"));
TextView tv1 = (TextView)rowView.findViewById(R.id.list_item_zone);
tv1.setTextColor(Color.parseColor("#000099"));
}
public void setItemNormal()
{
for (int i=0; i< listView.getChildCount(); i++) {
View v = listView.getChildAt(i);
v.setBackgroundColor(Color.TRANSPARENT);
TextView tv0 = ((TextView) v.findViewById(R.id.list_item_station));
tv0.setTextColor(Color.WHITE);
TextView tv1 = ((TextView) v.findViewById(R.id.list_item_zone));
tv1.setTextColor(Color.parseColor("#B5B5B5"));
}
}
});
return v;
}
static class MyAdapter extends BaseAdapter {
static class ViewHolder {
TextView station;
TextView zone;
}
LayoutInflater inflater;
WC[] mWC;
public MyAdapter(Context contexts, WC[] samples) {
this.mWC = samples;
inflater = LayoutInflater.from(contexts);
}
#Override
public int getCount() {
return mWC.length;
}
#Override
public Object getItem(int position) {
return mWC[position];
}
#Override
public long getItemId(int position) {
return 0;
}
/**set selected position**/
private int selectPosition = -1;
public void setSelectPosition(int position){
if(position!=selectPosition){
selectPosition = position;
}
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_dualline, null);
viewHolder = new ViewHolder();
viewHolder.station = (TextView) convertView.findViewById(R.id.list_item_station);
viewHolder.zone = (TextView) convertView.findViewById(R.id.list_item_zone);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.station.setText(mWC[position].station);
viewHolder.zone.setText(mWC[position].getzone());
//change item color
if(position==selectPosition){
convertView.setBackgroundColor(Color.parseColor("#000099"));
viewHolder.station.setTextColor(Color.parseColor("#000099"));
}else {
}
return convertView;
}
}
}
WCBankActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
public class WCBankActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "Station_key";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_wc_bank);
if (savedInstanceState == null) {
// Get the message from the intent
Intent intent = getIntent();
// Notice to specify the sender Activity for the message
String station = intent.getStringExtra(WCBankActivity.EXTRA_MESSAGE);
FragmentWCBank newFragment = new FragmentWCBank();
FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.detail_container, newFragment);
transaction.commit();
}
}
}
FragmentWCBank.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentWCBank extends android.support.v4.app.Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_wc_bank, container, false);
return v;
}
}
What you are seeing is a Warning rather than an Error. The difference being a Warning will not prevent your program from running, but an Error would.
The warning you are seeing is due to the fact that you are declaring a variable station but then never using it. Your IDE is smart enough to notice this and is trying to warn you as a reminder that you aren't using the variable that you created.
In this case it's this line:
String station = intent.getStringExtra(WCBankActivity.EXTRA_MESSAGE);
You declare a variable station and give it a value but then you never read that value or do anything else with it. Therefore you could theoretically remove the variable with no other impact to your app.
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;
}