java language syntax - java

I'm writing an android apps in Java of Eclipse. I'm not very familiar with the java syntax. I encounter this error.
The constructor Intent(new AdapterView.OnItemClickListener(){},
Class<NoteEditor> ) is undefined
Below is the code
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(this, NoteEditor.class);
startActivity(intent);
}
});
NoteEditor is extends Activity of Android. The above code is correct because I write it in another place it's no error.
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
Intent intent = new Intent(this, NoteEditor.class);
startActivity(intent);
//newGame();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

The context used in your code is wrong, as you're using the anonymous inner class's this. What you should be using is the Activity's context, like so:
Intent intent = new Intent(Category.this, NoteEditor.class);
The first parameter indicates the calling class's context. So you can use the Activity's this or getBaseContext()
public Intent (Context packageContext, Class<?> cls)

Here in your code this refers to your new AdapterView class not a activity,
and for Intent constructor you have to pass a reference of your current activity or application's base context,
replace your code,
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(getBaseContext(), NoteEditor.class);
startActivity(intent);
}
});
EDIT: also you can write
Intent intent = new Intent(<your current activity name>.this, NoteEditor.class);

Your problem is that this applies to the anonymous inner class rather than your Context subclass instance. In general, you'd write YourEnclosingClassName.this to get to that. In your case, you need NodeEditor.this.

Related

NavigationDrawer opening new activity

I implemented a basic navigationDrawer, with an array of strings. I want the user to click on something in the list and go to the corresponding Activity.
However, I am completely at loss here. I have read quite a few answers out here, but it still is not clear how to do this.
I have found a useful way for all the items combining the answers. However it is still returning my MainActivity instead of other activities.
The addDrawerItems() function:
public void addDrawerItems() {
mDrawerList = (ListView) findViewById(R.id.navList);
String[] osArray = {"How-to", "Milestones", "Bridge & Beams", "Settings"};
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0: //First item
Intent intent1 = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent1);
break;
case 1: //Second item
Intent intent2 = new Intent(MainActivity.this, Bridge_BeamsActivity.class);
startActivity(intent2);
break;
case 2: //Third item
Intent intent3 = new Intent(MainActivity.this, MileStonesActivity.class);
startActivity(intent3);
break;
case 3: //Fourth item
Intent intent4 = new Intent(MainActivity.this, HowToActivity.class);
startActivity(intent4);
break;
default:
}
}
});
}
Now my onCreate where I call the addDrawerItems():
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Check if there is any saved data
checkFirstRun();
//Apply saved data
savedData();
//Apply drawer
addDrawerItems();
}
Any help would be much appreciated!
If there are any unclear pieces of code or my explanation, please don't hesitate to reach out.
In case you want to see what is already there, please check out my beta app in the Play Store: https://play.google.com/store/apps/details?id=jhpcoenen.connectlife.beams
Add the following code after setting the adapter:
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
pos =position;
Intent intent = new Intent(MainActivity.this, CorrespondingActivity.class);
startActivity(intent);
switch (position) {
default:
}
}
});
The intent function will take to the desired activity.
EDIT
v.getId() will get you the id of the view, that is the id registered in the R class. As your array only contains strings, the only way to bind the string with the view Id is to manually check it. Build the listener and log your id to identify it
mDrawerList.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Log.d("your App","id clicked "+v.getId());
//this view is the item clicked by the user
Intent i = new Intent(getApplicationContext(),DestinyActivity.class)
i.putExtra("optionSelected",v.getId());
startActivity(i);
}
});
you will get an id like 234252341, and for every item of your list a different id which you can associate with the string
ORIGINAL
You have to add a click listener to your ListView and put an action on it depending on the view clicked. Something like this
mDrawerList.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//this view is the item clicked by the user
Intent i = new Intent(getApplicationContext(),DestinyActivity.class)
i.putExtra("optionSelected",v.getId())
startActivity(i)
}
})
In DestinyActivity you have to get extras with Intent.getExtras and do whatever you need.
PD: Code is handwritten, check it with your IDE

intent getSerializableExtra ClassCastException

In my app I have this line:
Item item = (Item)getIntent().getSerializableExtra(ITEM_TRANSFER);
The error I get is: Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast to com.example.shop.Item
Item Class implements Serializable so I don't know what the problem is.
#Override
public void onItemClick(View view, int position){
Intent intent = new Intent(this, ItemDetailActivity.class);
intent.putExtra(ITEM_TRANSFER, mRecyclerViewAdapter.getItemId(position) );
startActivity(intent);
}
This is how I use Extra
Send Item value instead your passing getItemId()
#Override
public void onItemClick(View view, int position){
Intent intent = new Intent(this, ItemDetailActivity.class);
intent.putExtra(ITEM_TRANSFER, item);
startActivity(intent);
}
Receive
Item item = (Item)getIntent().getSerializableExtra(ITEM_TRANSFER);

ListView Item Open New Activity

I'm still following a couple of tutorials in order to learn Android Development, I am struggling to open new activities. I can open only one activity, in fact all items on my ListView open this one activity, which would be great for ListView item in position 0 (the first item) not all of them.
My code below:
// Listen for ListView Item Click
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(mContext, MyActivity1.class);
mContext.startActivity(intent);
}
});
return view;
}
As you can see MyActivity1.class opens fine, but what if I had a second class called MyActivity2 which should be opened up by listview item in position 1 (second item), how would I do that? Also what does (View arg0) mean? I can't find proper explanation on this?
You can use Itemclick listner.
listviewOBJ.setOnItemClickListener( new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
}
});
here you can use int position to decide which item is clicked.
If my understanding is correct, then here is your answer:
Implement OnItemClickListener to your main activity:
then set listener to your listview: listView.setOnItemClickListener(this);
finallly :
private OnItemClickListener mOnGalleryClick = new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
switch(positon)
{
case 0:
Intent intent = new Intent(mContext, MyActivity1.class);
mContext.startActivity(intent);
break;
case 1:
Intent intent = new Intent(mContext, MyActivity2.class);
mContext.startActivity(intent);
break;
}
}
};
Use listview OnItemClickListener
listView.setOnItemClickListener( new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
switch(positon)
{
case 0:
Intent intent = new Intent(mContext, MyActivity1.class);
mContext.startActivity(intent);
break;
case 1:
Intent intent = new Intent(mContext, MyActivity2.class);
mContext.startActivity(intent);
break;
}
}
});
position is the index of list item. so based on the index you can start the respective activity.
The other way without switch case
String[] classes ={"MyActivity1","MyActivity2"};
Then in onItemClick
String value = classes[position];
Class ourClass =Class.forName("packagename."+value);
Intent intent = new Intent(mContext,ourClass);
mContext.startActivity(intent);
public void onClick(View arg0) {
onClick is called when you click on something, and arg0 (you could call it view, arg0 is so useless as name) is what you clicked.
This callback is often used in Buttons, Views in general but cannot be used with ListView.
If you want to open something when an item in the listview is clicked, you should use setOnItemClickListener!
On AdapterView.OnItemClickListener you have AdapterView<?> parent, View view, int position, long id arguments.
From android doc:
parent: The AdapterView where the click happened.
view: The view within the AdapterView that was clicked (this will be a view provided by the adapter)
position: The position of the item clicked (0 the first, 1 the second etc.)
id: The row id of the item that was clicked.
When you write your onItemClick you should remember you are in an anonymous class!
So you cannot use this when you need to pass your context to new Intent
pastesList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent intent = null;
switch (position)
{
case 0:
intent = new Intent(ActivityClass.this, Class1.class);
break;
case 1:
intent = new Intent(ActivityClass.this, Class2.class);
break;
default:
return;
}
startActivity(intent);
}
});
This line
ActivityClass.this
is used when you need to use the instance of the class ActivityClass (You should replace ActivityClass with your class name) but you cannot access it directly using this which here refer to new AdapterView.OnItemClickListener()
In your code you use mContext i don't know what is it, but i suppose it's a context.
Next time you don't have this variable, follow my advise if you want.
lv.setOnItemClickListener(n## Heading ##ew OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
if(position == 1) {
//code specific to first list item
Intent myIntent = new Intent(view.getContext(), Html_file1.class);
startActivityForResult(myIntent, 0);
}
if(position == 2) {
//code specific to 2nd list item
Intent myIntent = new Intent(view.getContext(), Html_file2.class);
startActivityForResult(myIntent, 0);
}
}
});
What you want to use is an AdapterView.OnItemClickListener.
final ListView list = ...;
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(mContext, MyActivity1.class);
if (position == 1) {
intent = new Intent(mContext, MyActivity2.class);
}
startActivity(intent);
}
});

Passing param to java android class

Here is the class i am passing to
public class AxxessCCPAccountDetails extends Activity {
public AxxessCCPAccountDetails(String username) {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.accountdetails);
}
}
Here is the code that i am passing from
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(view.getContext(), AxxessCCPAccountList.class);
startActivityForResult(myIntent, 0);
}
});
I need to pass a username to a class. How can i pass the param to the class then activate the activity(class) which is linked to a view.
Thanks
Using Intent. You should not create your own constructor for the Activity class.
Here is a short example:
Intent myIntent = new Intent(view.getContext(), AxxessCCPAccountList.class);
myIntent.putExtra("username", userName);
myIntent.putExtra("pass", pass);
And then, in your AxxessCCPAccountList.onCreate method:
String userName = this.getIntent().getStringExtra("username");
String pass = this.getIntent.getStringExtra("pass");
Add the data to the intent (intent.putExtra() or something), and get it on the other side from the intent in onCreate (intent.getExtra() ) by using the same key.
This is the solution
getIntent().getExtras().getString("username");

starting a new activity throws me into debug mode and stops the app

I have a listview. Whenever I strike an item of listview the debugger gets opened instead of starting a new activity (i.e. what I want).
here is the listview code..
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, s));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
startAc();
}
});
the startAc() function is defined on top as:
public void startAc()
{
startActivity( new Intent(this, contents.class) );
}
the contents class file is defined as:
package com.webkul.feedGrabber;
import android.app.ListActivity;
import android.os.Bundle;
public class contents extends ListActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.m);
}
}
P.S. all the xml files are correct.
I think with just startActivity()
public void startAc() {
Intent myIntent = new Intent(getApplicationContext(), contents.class);
startActivity(myIntent);
}
you must have a breakpoint in your code
try
run > remove all breakpoints
Have you tried to start the activity by result?
Intent i = new Intent(this, contents.class);
startActivityForResult(i, SOME_INT);
Actually I am a bit "poked" by the listview code. I would suggest to search there.

Categories