I have been trying to make a ListView which will have image, textviews and buttons.
I am trying to get the buttons to start a new activity by using the code
but when I use
Intent intent = new Intent(MyListAdapter.this, Login.class);
I get the error Cannot Resolve constructor intent error.
And in the very next line
startActivity(intent);
I get the error cannot resolve method startactivity
I wanted to ask how can I get it to launch the activity I want? I have put the full code below
Any help would be appreciated.
Thank You
public class MyListAdapter extends ArrayAdapter<Hero> {
//the list values in the List of type hero
List<Hero> heroList;
//activity context
Context context;
//the layout resource file for the list items
int resource;
//constructor initializing the values
public MyListAdapter(Context context, int resource, List<Hero> heroList) {
super(context, resource, heroList);
this.context = context;
this.resource = resource;
this.heroList = heroList;
}
//this will return the ListView Item as a View
#NonNull
#Override
public View getView(final int position, #Nullable View convertView, #NonNull ViewGroup parent) {
//we need to get the view of the xml for our list item
//And for this we need a layoutinflater
LayoutInflater layoutInflater = LayoutInflater.from(context);
//getting the view
View view = layoutInflater.inflate(resource, null, false);
//getting the view elements of the list from the view
ImageView imageView = view.findViewById(R.id.imageView);
TextView textViewName = view.findViewById(R.id.textViewName);
TextView textViewTeam = view.findViewById(R.id.textViewTeam);
Button test = view.findViewById(R.id.buttonDelete);
//getting the hero of the specified position
Hero hero = heroList.get(position);
//adding values to the list item
imageView.setImageDrawable(context.getResources().getDrawable(hero.getImage()));
textViewName.setText(hero.getName());
textViewTeam.setText(hero.getTeam());
//adding a click listener to the button to remove item from the list
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MyListAdapter.this, Login.class);
startActivity(intent);
}
});
return view;
}
Intent takes first argument as Context. Since Adapter is not child class of Context you can not directly use this. You have to use context which is passed as a Argument. Modify your code as follows.
tx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(context, Login.class);
context.startActivity(intent);
}
});
Because the params you put inside Intent's constructor is not correct. Use Context instead.
Intent intent = new Intent(yourContext, Login.class);
yourContext.startActivity(intent);
Try to startActivity with Context. Where we can get context in Adapter? Check below:
final Context mContext = test.getContext();
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(mContext, Login.class);
mContext.startActivity(intent);
}
});
Related
Sorry, this is my first question...
I have an ArrayAdapter class and i want to call startActivity from an internal anonymous class, but i'm taking the error "cannot resolve method startActivity"
public View getView(final int position, #Nullable final View convertView, #NonNull ViewGroup parent) {
//inflating the layout
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.itemview_antliostasia, parent, false);
//get the reference to the view objects
final TextView myName = (TextView)row.findViewById(R.id.itemViewAntliostasiaName);
final TextView myID = (TextView)row.findViewById(R.id.itemViewAntliostasiaID);
final TextView myAntlies = (TextView)row.findViewById(R.id.itemViewAntliostasiaAntlies);
ImageView myEdit = (ImageView) row.findViewById(R.id.imageAntliostasiaEdit);
ImageView myDelete = (ImageView)row.findViewById(R.id.imageAntliostasiaDelete);
//providing the element of an ArrayList by specifying its position
myName.setText(dataList.get(position).getName());
myID.setText(dataList.get(position).getId());
myAntlies.setText(dataList.get(position).getAntlies());
myEdit.setImageResource(R.drawable.imgedit);
myDelete.setImageResource(R.drawable.imgdelete);
myEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), EditAntliostasioActivity.class);
String[] editValues = new String[2];
editValues[0] = myName.getText().toString();
editValues[1] = myAntlies.getText().toString();
intent.putExtra("edit values", editValues);
startActivity(intent);
}
});
I think something is going wrong with getContext()...Please help me
startActivity() is a method of Context so you need to call it on the Context object:
getContext().startActivity(intent);
Note that if the Context was not an Activity, then you would need also to set FLAG_ACTIVITY_NEW_TASK on the Intent. But startActivity() is overridden in Activity to not need that, and you have an activity context here.
Try this
Intent intent = new Intent(view.getContext(), EditAntliostasioActivity.class);
The reason for this error was because the getContext() was returning the context of the activity instead of the anonymous class. Hence, you have to use view.getContext().
Create an instance variables of Context..
Class Vehicle
{
Context mContext;
Initialize Context...
public Vehicle(Context mContext)
{
this.mContext = mContext;
}
Whenever you are calling startActivity do like this..in your case..
mContext.startActivity(intent);
I am using listview with custom BaseAdapter to display various item. The listview is inside FragmentA and FragmentA is displayed using MainActivity's view pager. Now i want shared element transion from custom adapter button onclick to Main2Activity.I have searched a lot in SO and tried but none of them solve my issue. when i click button inside custom adapter the Main2Activity is not displaying at all but in that moment if i rotate my mobile screen then its getting displayed. I have checked my code very minutely but could not find whats wrong in it. So please help me in this context.
public class MySBaseAdapter extends BaseAdapter {
private Context mContext;
private List<Map<String,Object>> data=new ArrayList<Map<String, Object>>();
public LayoutInflater inflater=null;
String itemNo, itemId;
Activity activity;
public MySBaseAdapter(Context context, List<Map<String, Object>> data, Activity thisactitity, String itemno, String itemid){
mContext=context;
this.data=data;
this.itemNo=itemno;
this.itemId=itemid;
activity=thisactitity;
inflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, final View convertView, ViewGroup parent) {
item.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imvb.buildDrawingCache();
Bitmap bmp=imvb.getDrawingCache();
Intent intent = new Intent(mContext, Main2Activity.class);
intent.putExtra("bitmap_data",bmp); intent.putExtra("item_name",itemname.getText().toString());
intent.putExtra("item_num",itemNo);
intent.putExtra("item_id",itemId);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Pair<View, String> pair1 = Pair.create((View)imvb,"phototrans");
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, pair1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mContext.startActivity(intent, options.toBundle());
}else {
mContext.startActivity(intent);
}
}
});
Custom adapter class
public class MySBaseAdapter extends BaseAdapter {
private Context mContext;
private List<Map<String,Object>> data=new ArrayList<Map<String, Object>>();
public LayoutInflater inflater=null;
String itemNo, itemId;
Activity activity;
public MySBaseAdapter(Context context, List<Map<String, Object>> data, Activity thisactitity, String itemno, String itemid){
mContext=context;
this.data=data;
this.itemNo=itemno;
this.itemId=itemid;
activity=thisactitity;
inflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, final View convertView, ViewGroup parent) {
item.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imvb.buildDrawingCache();
Bitmap bmp=imvb.getDrawingCache();
Intent intent = new Intent(mContext, Main2Activity.class);
intent.putExtra("bitmap_data",bmp); intent.putExtra("item_name",itemname.getText().toString());
intent.putExtra("item_num",itemNo);
intent.putExtra("item_id",itemId);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Pair<View, String> pair1 = Pair.create((View)imvb,"phototrans");
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, pair1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mContext.startActivity(intent, options.toBundle());
}else {
mContext.startActivity(intent);
}
}
});
Main2Activity
public class Main2Activity extends AppCompatActivity {
Context pContext;
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main31);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
prorileimage.setTransitionName("phototrans");
}
What is item, is it a button or some other view? If its not button have you set its clickable property true in xml file? You can print log inside the click listener.
One more thing I am concern about it as this document says, transition determines how views transits between activity or fragments; and you have taken bitmap so will it give transition or not. I am not sure :
A shared element transition determines how views that are present in two fragments transition between them. For example, an image that is displayed on an ImageView on both Fragment A and Fragment B transitions from A to B when B becomes visible.
I am using an adapter class to populate my listview, but each item in my listview has two textviews to whom I wanna setOnClick listener. So I set it in adapter class and it works fine when I try to show the toast.
But the problem is I cant startActivity within OnClickListener. The app crashes. Please help or suggest an alternate way to achieve the same.
The activity is already mentioned in Manifest.
This Is My Code:-
public class Adapter_NearMe_TyreWorx extends ArrayAdapter<List_NearMe> implements View.OnClickListener {
ArrayList<List_NearMe> arraylist;
private Context context;
private List<List`enter code here`_NearMe> list;
public Adapter_NearMe_TyreWorx(Context context, int resource, List<List_NearMe> objects) {
super(context, resource, objects);
this.context = context;
this.List = objects;
arraylist = new ArrayList<List_NearMe>();
arraylist.addAll(List);
}
TextView Btn_Call;
String Fac_landmark;
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.canvas_two, parent, false);
List_NearMe list= list.get(position);
String Fac_name=list.getName();
Fac_landmark=list.getLandmark();
String Fac_gMap=list.getgMap();
String Fac_contact=list.getContact();
TextView distance=(TextView)view.findViewById(R.id.fac_distance);
TextView Fac_Name=(TextView)view.findViewById(R.id.fac_name);
TextView Fac_Address=(TextView)view.findViewById(R.id.fac_address);
Btn_Call=(TextView)view.findViewById(R.id.btn_call);
TextView Btn_Go=(TextView)view.findViewById(R.id.btn_go);
Btn_Go.setOnClickListener(this);
return view;
}
public void onClick(View v) {
Toast.makeText(getContext(),"Toast text",LENGTH.SHORT).show(); //working toast code
Intent intent = new Intent(getContext(), SampleActivity.class);
getContext().startActivity(intent);
}
}
You have to add a Intent flag to pass a Intent from a Non - Activity class Intent.FLAG_ACTIVITY_NEW_TASK adding this flag in intent will work fine try to pass your Intent like this :
Intent intent = new Intent(getContext(), SampleActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getContext().startActivity(intent);
Or
Intent intent = new Intent(getContext(), SampleActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getContext().startActivity(intent);
replace getContext() with context.
Intent intent = new Intent(context, SampleActivity.class);
context.startActivity(intent);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
context.startActivity(new Intent(context.getApplicationContext(), SampleActivity.class));
}
});
call the method on the context which you are setting in the constructor of the adapter.
as startActivity() can only be started from the method which in the context of the application, as your adapter is not there you would have to reference the context which you have provided at the constructor of your adapter class
With the following code I'm correctly receiving a dynamic list from mysql db and putting the elements in a listview.
public class MenuActivity extends ListActivity implements FetchDataListener {
private ProgressDialog dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
initView();
}
private void initView() {
// show progress dialog
dialog = ProgressDialog.show(this, "", "Loading..");
String url = "http://www.*********.php";
FetchDataTask task = new FetchDataTask(this);
task.execute(url);
}
#Override
public void onFetchComplete(List<Application> data) {
// dismiss the progress dialog
if(dialog != null) dialog.dismiss();
// create new adapter
ApplicationAdapter adapter = new ApplicationAdapter(this, data);
// set the adapter to list
setListAdapter(adapter);
}
#Override
public void onFetchFailure(String msg) {
// dismiss the progress dialog
if(dialog != null) dialog.dismiss();
// show failure message
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
This is my array adapter:
public class ApplicationAdapter extends ArrayAdapter<Application>{
private List<Application> items;
public ApplicationAdapter(Context context, List<Application> items) {
super(context, R.layout.app_cat_list, items);
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null) {
LayoutInflater li = LayoutInflater.from(getContext());
v = li.inflate(R.layout.app_cat_list, null);
}
Application app = items.get(position);
if(app != null) {
TextView titleText = (TextView)v.findViewById(R.id.titleTxt);
if(titleText != null) titleText.setText(app.getTitle());
}
return v;
}
Now I want to click on single row and open another activity passing some values via intent extra.
Where should I implement click listener?
I'm pretty sure it should be inserted in the "getView" but how I pass the app.getTitle() via intent? I know how pass intent extra in general, tried but no click happens.
Any help would be appreciated, thanks
Now I want to click on single row and open another activity passing
some values via intent extra. Where should I implement click listener?
No need to add OnItemClickListener because extending ListActivity in MenuActivity so just override onListItemClick method for handing ListView row click:
#Override
public void onListItemClick(ListView l, View view, int position, long id) {
// your code here...
}
how I pass the app.getTitle() via intent?
Get selected row TextView value in onListItemClick using view parameter:
TextView txtView=(TextView)v.findViewById(R.id.titleTxt);
String selectedText=txtView.getText().toString();
Use selectedText for sending value with Intent in Next Activity
Put this in your getView()
v.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent =new Intent(context, YourActivity.class);
context.startActivity(intent);
}
});
There can be multiple ways and following is one of them:
Set onItemClickListner on your listview in your activity and it will give you a callback i.e onListItemClick. But as you said you want the title their you have to set tag on the convertView in the getView method like covertView.setTag("itemTitle"); and in your onListItemClick get the tag from view and convert it to the title like this v.getTag().toString(); and set it any where you want.
follwoing is the full code:
#Override
public void onListItemClick(ListView l, View view, int position, long id) {
String title = view.getTag().toString();
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("title", title);
startActivity(intent);
// your code here... }
Please post if got stuck anywhere.
When I click the edit_remainder button I want to move my Activity to another Activity.
But where the class extends ArrayAdapter I don't understand how to move to another Activity.
Please help me with an example of the Intent class.
public class mylist extends ArrayAdapter<String> implements OnClickListener
{
private final Context context;
private final String[] values;
Button edit_remainder;
public mylist(Context context, String[] values) {
super(context, R.layout.some, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.buttonadd, parent, false);
edit_remeinder=(Button) rowView.findViewById(R.id.btnEdit);
edit_remeinder.setOnClickListener(this);
return rowView;
}
public void onClick(View v) {
Toast.makeText(getContext(), "hi", 1000).show();
// please enter moving code here
}
}
If I understand correctly, you wish to transfer control to another activity when the user taps on a list item. You will need to issue an intent.
Here's a tutorial: http://www.vogella.com/articles/AndroidIntent/article.html
launch an explicit intent here like:
Intent i=new Intent(context,anotheractivity.class);
startActivity(i);
here the context could be the application context.
Are you create your own adapter(mylist)?
If yes then pass the intent in your main class from you call the mylist adapter.
here I created for listview ,but you can try for your own application.
pass the intent in your main activity instead of your own adapter.
mainActivity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lview = (ListView) findViewById(R.id.listView2);
lviewAdapter = new ListViewAdapter(this, month, number); //here ListViewAdapter is my own adapter
System.out.println("adapter => "+lviewAdapter.getCount());
lview.setAdapter(lviewAdapter);
lview.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(this,"Title => "+month[position]+"=> n Description"+number[position], Toast.LENGTH_SHORT).show();
// here your intent code
}
}
please put this onclick method in your main activity and try again.
public void onClick(View v) {
Toast.makeText(getContext(), "hi", 1000).show();
// please enter moving code here
}