Hello I'm new on android I have this trouble with my code
package com.example.spinner;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
Spinner lista;
String[] datos = {"opcion1", "opcion2", "Ir a listView"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lista = (Spinner) findViewById(R.id.lista1);
ArrayAdapter<String> adaptador = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, datos);
lista.setAdapter(adaptador);
lista.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int i, long l) {
// TODO Auto-generated method stub
switch (i){
case 1:
Toast to = Toast.makeText(getApplicationContext(), datos[i], Toast.LENGTH_SHORT);
to.show();
break;
case 2:
Intent int = new Intent(MainActivity.this, VentanaListView.class);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
In the switch case 2 is where a litte box appears saying :
Intent cannot be resolved to a variable
I have already import the Intent using import android.content.Intent; and It says
The import android.content.Intent is never used
Please do you know what should I do?
Change this
Intent int = new Intent(MainActivity.this, VentanaListView.class)
to
Intent intent = new Intent(MainActivity.this, VentanaListView.class)
int is a keyword in java
You probably want to call startActivity(intent) also
You can't name a variable int; that's a keyword. Choose a different variable name, such as intent.
Intent int = new Intent(MainActivity.this, VentanaListView.class);
int is a reseverd word in java (a Keyoword) ,and it can not be used a variable`s name
Related
I want to open a new activity when I click on one of the name of the "string", for example, click on "pila 12v" and open new activity, in this case open "Main5Activity".
When placing the "case values [0]" a message appears:constant expression required.
package com.example.epson.mp;
import android.content.Intent;
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.AutoCompleteTextView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
AutoCompleteTextView values;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String[] values = new String[]{"bateria 9v", "pila 9v", "bateria 12v", "pila 12v"};
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,values);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txtMercancias);
textView.setThreshold(3);//will start working from third character
textView.setAdapter(adapter); //setting the adapter data into the AutoCompleteTextView
textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
LinearLayout ll = (LinearLayout) view;
//This will give you the string value of selected list item
TextView listItem = (TextView) ll.getChildAt(0);
//You can do this or apply own logic to find the selected value case
switch (listItem.getText().toString()){
case values[0]:
Intent myintent0 = new Intent(view.getContext(), Main2Activity.class);
startActivityForResult(myintent0, 0);
break;
case values[1]:
Intent myintent1 = new Intent(view.getContext(), Main3Activity.class);
startActivityForResult(myintent1, 1);
break;
case values[2]:
Intent myintent2 = new Intent(view.getContext(), Main4Activity.class);
startActivityForResult(myintent2, 2);
break;
case values[3]:
Intent myintent3 = new Intent(view.getContext(), Main5Activity.class);
startActivityForResult(myintent3, 3);
break;
}
}
});
}
}
I am trying to make a "News App" with Android Studio. I have on my main activity a ListView and a floating button. On my second activity I got two TextViews, two EditViews and one Button. What I'd like to do is the following:
First I press on the floating Button from my main Activity. Then the second Activity should pop up and there I write on the two EditText's and then press on the button. After that I should go back to the main activity and there, a new Item should be added to the ListView. The new item should be filled with the text from the second activity's EditTexts.
This is my main activity:
package news;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Get subject value from addpost Activity
final String new_value = getIntent().getStringExtra("newSubject");
String [] new_subject = new String []
{new_value};
FloatingActionButton add_post_button = findViewById(R.id.post_btn);
final ListView post_view = findViewById(R.id.news_feed);
//Create Adapter to add the new subject to listview
if (new_subject != null) {
ArrayAdapter newslist_adapter = new ArrayAdapter(
MainActivity.this,
android.R.layout.simple_expandable_list_item_1, new_subject);
post_view.setAdapter(newslist_adapter);
}
add_post_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, addpost_activity.class);
MainActivity.this.startActivity(intent);
}
});
}
}
This is the second activity(addpost_activity):
package news;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.content.Intent;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class addpost_activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addpost_activity);
Button post_button = findViewById(R.id.post_btn);
final EditText subject_text = findViewById(R.id.subject_edit);
post_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(subject_text.getText() != null) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("newSubject", subject_text.getText().toString());
startActivity(intent);
}
}
});
}
}
My problem is now that the app crashes every time I start it. I also get this message from my logs:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
Solved:
Thanks to Munir, the problem was solved.
Here are my activities
MainActivity:
package news;
import android.app.Activity;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayAdapter newslist_adapter;
ArrayList<String> new_subject = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton add_post_button = findViewById(R.id.post_btn);
//Create Adapter to add the new subject to listview
add_post_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, addpost_activity.class);
startActivityForResult(intent, 1);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
final ListView post_view = findViewById(R.id.news_feed);
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String new_value = data.getStringExtra("newSubject");
new_subject.add(new_value);
newslist_adapter = new ArrayAdapter(
MainActivity.this,
android.R.layout.simple_expandable_list_item_1, new_subject);
post_view.setAdapter(newslist_adapter);
}
}
}
}
This is my second Activity(addpost_activity):
package news;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.content.Intent;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class addpost_activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addpost_activity);
Button post_button = findViewById(R.id.post_btn);
final EditText subject_text = findViewById(R.id.subject_edit);
post_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(subject_text.getText() != null) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("newSubject", subject_text.getText().toString());
setResult(Activity.RESULT_OK, intent);
finish();
}
}
});
}
}
For that purpose I suggest using Fragments instead of two Activities. The MainActivity then holds all your data.
See Fragments guide.
From your MainActivity call the addpost_activity using startActivityForResult() method Instead of startActivity();
In your addpost_activity set the data which you want to return back to FirstActivity
post_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(subject_text.getText() != null) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("newSubject", subject_text.getText().toString());
setResult(Activity.RESULT_OK,intent );
finish();
}
}
});
And in the MainActivity Access the result by overriding onActivityResult()
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("newSubject");
//Add this value to your adapter and call notifyDataSetChanged();
}
}
}
use ArrayList insted of String [] if you want to keep old entry in list change your code like below.
Main Activity
declare this variable as global
ArrayAdapter newslist_adapter;
ArrayList<String> new_subject = new ArrayList<>();
set Adapter like
newslist_adapter = new ArrayAdapter(
MainActivity.this,
android.R.layout.simple_expandable_list_item_1, new_subject);
post_view.setAdapter(newslist_adapter);
change startActivity to startActivityForResult
add_post_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, addpost_activity.class);
startActivityForResult(intent,100);
}
});
Override this method in onActivity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100) {
String result=data.getStringExtra("newSubject");
new_subject.add(result)
//Add this value to your adapter and call newslist_adapter.notifyDataSetChanged();
}
}
And In your addpost_activity set the data which you want to return back
post_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(subject_text.getText() != null) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("newSubject", subject_text.getText().toString());
setResult(100,intent );
finish();
}
}
});
In MainActivity.java, you should override a function named "onActivityRecsult" this method will be called when you come back from second activity to first activity.
I have two classes for my translation app. One(Voice.java) in which we speak and RecognizerIntent gets our speech and displays it in form of a list. This is voice.java
package com.example.testing;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class Voice extends Activity implements OnClickListener{
ListView lv;
static final int check=1111;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.voice);
lv=(ListView)findViewById(R.id.lvVoiceReturn);
Button b=(Button)findViewById(R.id.bVoice);
b.setOnClickListener(this);
lv.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.bVoice:
Intent i=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak Up");
startActivityForResult(i, check);
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode == check && resultCode==RESULT_OK){
ArrayList<String> results=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));
}
super.onActivityResult(requestCode, resultCode, data);
}
}
And another class TranslateActivity which has one button and two edit text. Button works as translator of text from 1st edit text to translated text in second edit Text and speaks the translated text also . Code is as follows:
package com.example.testing;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.memetix.mst.language.Language;
import com.memetix.mst.translate.Translate;
public class TranslateActivity extends Activity implements OnClickListener {
EditText etInput
;
EditText etOutput;
TextToSpeech tts;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_translate);
Translate.setClientId("d6159b31-37ba-4668-a31f-de0de6c47a38");
Translate.setClientSecret("UMjSB3cHaSkLE+2xw4+a4dLIFahLdQQqh1YOhXdqfh4");
Button Trans = (Button)findViewById(R.id.translate);
etInput = (EditText)findViewById(R.id.input);
etOutput = (EditText)findViewById(R.id.output);
Trans.setOnClickListener(this);
tts =new TextToSpeech(TranslateActivity.this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
// TODO Auto-generated method stub
if(status !=TextToSpeech.ERROR){
tts.setLanguage(Locale.US);
}
}
});
}
#Override
public void onClick(View v) {
//get the text entered
String In =etInput.getText().toString();
String s[]=In.split(" ");
//String out[]= new String[3];
String str=new String();
String outString=new String();
//String Out;
try {
for(int i=0;i<s.length;i++){
if(s[i].equals("who")){
str="whoa";
//etOutput.setText("whoa");
//str=etOutput.getText().toString();
//tts.speak(str,TextToSpeech.QUEUE_FLUSH, null);
}else if(s[i].equals("are")){
// etOutput.setText("arey");
str="arey";
//str=etOutput.getText().toString();
//tts.speak(str,TextToSpeech.QUEUE_FLUSH, null);
}else if(s[i].equals("you")){
str="ram";
//etOutput.setText("your");
//str=etOutput.getText().toString();
//tts.speak(str,TextToSpeech.QUEUE_FLUSH, null);
}
outString+=str ;
outString+=" ";
}
tts.speak(outString,TextToSpeech.QUEUE_FLUSH, null);
// String Out = Translate.execute(In, Language.AUTO_DETECT, Language.FRENCH);
// etInput.setText(Out);
// etOutput.setText(Out);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Now I want an on click listener for list view such that whatever line I click on list View, it gets used as edit Text of 2nd class(TranslateActivity.java) and gets translated without clicking the button on 2nd class. That is functionality of button in 2nd class is done by just clicking on list view in 1st class.Anyone who knows how to do this?
I have this problem accessing an extra from an intent.
The value i'm parsing is a long type, and I need this value to be stored in a database.
So this is what I have so far:
MainActivity:
package com.example.calendar;
import java.sql.Date;
import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CalendarView;
import static android.provider.BaseColumns._ID;
import static com.example.calendar.Constants.TABLE_NAME;
import static com.example.calendar.Constants.TIME;
import static com.example.calendar.Constants.TITLE;
import static com.example.calendar.Constants.DETAILS;
import static com.example.calendar.Constants.DATE;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class MainActivity extends Activity implements OnClickListener {
private AppointmentsData appointments;
CalendarView calendar;
Date date = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View createButton = findViewById(R.id.btn_create);
View editButton = findViewById(R.id.btn_viewEdit);
View deleteButton = findViewById(R.id.btn_delete);
View moveButton = findViewById(R.id.btn_move);
View searchButton = findViewById(R.id.btn_search);
View translateButton = findViewById(R.id.btn_translate);
View exitButton = findViewById(R.id.exit);
createButton.setOnClickListener(this);
editButton.setOnClickListener(this);
deleteButton.setOnClickListener(this);
moveButton.setOnClickListener(this);
searchButton.setOnClickListener(this);
translateButton.setOnClickListener(this);
exitButton.setOnClickListener(this);
appointments = new AppointmentsData(this);
calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
date = new Date(year, month, dayOfMonth);
}
});
}
#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, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i;
switch (v.getId()) {
case R.id.btn_create:
i = new Intent(this, CreateAppointment.class);
i.putExtra(DATE, date.getTime());
startActivity(i);
break;
case R.id.btn_viewEdit:
i = new Intent(this, EditViewAppointment.class);
i.putExtra(DATE, date.getTime());
startActivity(i);
break;
case R.id.btn_move:
i = new Intent(this, MoveAppointment.class);
i.putExtra(DATE, date.getTime());
startActivity(i);
break;
case R.id.btn_delete:
i = new Intent(this, DeleteAppointment.class);
i.putExtra(DATE, date.getTime());
startActivity(i);
break;
case R.id.btn_search:
i = new Intent(this, SearchAppointment.class);
startActivity(i);
break;
case R.id.btn_translate:
i = new Intent(this, TranslateAppointment.class);
i.putExtra(DATE, date.getTime());
startActivity(i);
break;
case R.id.exit:
finish();
break;
}
}
}
And the other Activity to use the value:
package com.example.calendar;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.EditText;
import android.app.Activity;
import android.content.Intent;
import static android.provider.BaseColumns._ID;
import static com.example.calendar.Constants.TABLE_NAME;
import static com.example.calendar.Constants.TIME;
import static com.example.calendar.Constants.TITLE;
import static com.example.calendar.Constants.DETAILS;
import static com.example.calendar.Constants.DATE;
import static com.example.calendar.Constants.CONTENT_URI;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class CreateAppointment extends Activity implements OnClickListener{
private static String[] FROM = { _ID, DATE, TIME, TITLE, DETAILS};
private static String ORDER_BY = TIME + " ASC";
AppointmentsData appointments;
CalendarView calendar;
String string;
EditText nameTextBox;
EditText timeTextBox;
EditText detailsTextBox;
Button createButton;
SQLiteDatabase db;
Intent fetchDate = getIntent();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create);
createButton = (Button) findViewById(R.id.apptSave);
nameTextBox = (EditText)findViewById(R.id.apptName);//Assign the global name box
timeTextBox = (EditText)findViewById(R.id.apptTime);//Assign the global time box
detailsTextBox = (EditText)findViewById(R.id.apptDetails);//Assign the global details box
calendar = (CalendarView)findViewById(R.id.calendar);
createButton.setOnClickListener(this);
appointments = new AppointmentsData(this);
string = "row";
long dateFecth = fetchDate.getLongExtra(DATE, defaultValue);
}
private void addAppointment(String string) {
/* Insert a new record into the Events data
source. You would do something similar
for delete and update. */
String getTitle = nameTextBox.getText().toString();
String getTime = timeTextBox.getText().toString();
String getDetails = detailsTextBox.getText().toString();
db = appointments.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DATE, calendar.getDate());
values.put(TIME, getTime);
values.put(TITLE, getTitle);
values.put(DETAILS, getDetails);
getContentResolver().insert(CONTENT_URI, values);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.apptSave:
addAppointment(string);
finish();
break;
}
}
}
The error is on the long dateFecth = fetchDate.getLongExtra(DATE, defaultValue); line and I don't know what to use for the second argument, since everything I think of gives me an error.
Can someone please help me with this?
Move fetchDate = getIntent(); inside onCreate method of Activity as:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create);
//.your code here..
fetchDate = getIntent(); // get intent here from previous Activity
long dateFecth = fetchDate.getLongExtra(DATE, defaultValue);
}
First check - Value is associated with the given name.
if you don't want to use defaultValue try with bundle as follow
if (getchDate.hasExtra("Date")) {
Bundle bundle = new Bundle();
bundle = getchDate.getExtras();
Object object = bunlde.get(Date);
Now parse this object in your desire type.
}
You should check if result of getIntent() is not empty at first (an of course the call should be inside onCreate, not constructor). If it's not empty, then get long variable from it. defaultValue stands for a value returned from a method if a value for specified key is not found.
Ok. I'm working on a project, I have already created the listview, but I want to click on it and go to another page e.g like when you click a button it goes to another page, exactly like that, this is my code so far:
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class listV extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter( new ArrayAdapter<String>(this, R.layout.listview,Food));
ListView list = getListView();
list.setTextFilterEnabled(true);
list.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(), Toast.LENGTH_SHORT).show();
}
});
}
static final String[] Food = new String[]{
"Physical Activity" , "Healthy Diet", "Childhood Obesity"
};
}
Any help would be appreciated.
I'm just a beginner so please try to explain in detail.
Looks like you're 95% of the way there. In your onItemClick method of the listener, you just have to start the new activity like you normally would. You can use the 3rd argument of the onItemClick to give you the position of the listview item that was click and use that to differentiate the activity you call OR pass it into a single activity:
#Override
public void onItemClick(AdapterView<?> arg0, View position, int arg2, long arg3) {
Intent i;
if( position == 1 ){
i = new Intent(listV.this, MyFirstActivity.class);
} else if (position == 2){
i = new Intent(listV.this, MySecondActivity.class);
} else if (position == 3) {
i = new Intent(listV.this, MyThirdActivity.class);
} else {
return;
}
startActivity(i);
}
I am a fairly new programmer myself and I am attempting to create the same thing. A ListView where the user is able to click each individual item in the list and each item will start its own activity. Please check out my code based on the help you gave earlier.
package com.tylerbmc.test;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Main extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.abdominals)));
ListView list = getListView();
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent i;
if (position == 1) {
i = new Intent(Main.this, Second.class);
startActivity(i);
}
else if (position == 2) {
i = new Intent(Main.this, Third.class);
startActivity(i);
}
}
});
}
}