I am trying out below code
here is the MainActivity.java
import java.io.File;
import java.util.ArrayList;
import javax.crypto.spec.PSource;
import android.R.string;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterViewFlipper;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
ListView lv;
String[] items;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lvPlaylist);
final ArrayList<File> mySongs = findSongs(Environment.getExternalStorageDirectory());
items = new String[mySongs.size () ];
for (int i = 0; i < mySongs.size(); i++) {
//toast(mySongs.get(i).getName().toString());
items[i] = mySongs.get(i).getName().toString();
}
ArrayAdapter<String> adp = new ArrayAdapter<String>(getApplicationContext(),R.layout.list_item,R.id.textView1,items);
lv.setAdapter(adp);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
startActivity(new Intent(getApplicationContext(), player.class).putExtra("pos", position).putExtra("songlist",mySongs));
}
});
}
public ArrayList<File> findSongs(File root){
ArrayList al = new ArrayList<File>();
File[] files = root.listFiles();
for(File singleFile : files){
if (singleFile.isDirectory()&& !singleFile.isHidden()) {
al.addAll(findSongs(singleFile));
}
else {
if (singleFile.getName().endsWith(".mp3") || singleFile.getName().endsWith(".wav")) {
al.add(singleFile);
}
}
}
return al;
}
public void toast(String text) {
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
#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 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);
}
}
above code gives me a list of mp3 files when we click on one of the songs it takes to player.ja and songs are played
and
player.java
import java.io.File;
import java.util.ArrayList;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.SeekBar;
public class player extends ActionBarActivity implements View.OnClickListener{
static MediaPlayer mp;
ArrayList<File> mySongs;
int position;
Uri u;
SeekBar sb;
Button btPv, btFB,btplay,btFF, btNxt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
btFB = (Button) findViewById(R.id.btFB);
btPv = (Button) findViewById(R.id.btPv);
btplay = (Button) findViewById(R.id.btplay);
btFF = (Button) findViewById(R.id.btFF);
btNxt = (Button) findViewById(R.id.btNxt);
btFB.setOnClickListener(this) ;
btPv.setOnClickListener(this);
btplay.setOnClickListener(this);
btFF.setOnClickListener(this);
btNxt.setOnClickListener(this);
//
if(mp != null)
{
mp.stop();
mp.release();
}
Intent i = getIntent();
Bundle b = i.getExtras();
mySongs = (ArrayList) b.getParcelableArrayList("songlist");
position = b.getInt("pos", 0);
u = Uri.parse(mySongs.get(position).toString());
mp = MediaPlayer.create(getApplicationContext(), u).start();
mp.start();
}
#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 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);
}
#Override
public void onClick(View v)
{
int id = v.getId();
switch (id) {
case R.id.btplay:
if(mp.isPlaying())
{btplay.setText("play");
mp.pause();
}
else
{ btplay.setText("play");
mp.start();
}
break;
case R.id.btFF:
mp.seekTo(mp.getCurrentPosition()+ 5000);
break;
case R.id.btFB:
mp.seekTo(mp.getCurrentPosition()- 5000);
break;
case R.id.btNxt:
mp.stop();
mp.release();
position = (position+1)%mySongs.size();
u = Uri.parse(mySongs.get(position).toString());
mp = MediaPlayer.create(getApplicationContext(), u);
mp.start();
break;
case R.id.btPv :
mp.stop();
mp.release();
// Important
position = (position-1<0) ? mySongs.size()-1: position-1;
/*if(position-1 < 0)
{
position = mySongs.size()-1;
}
else
{
position= position-1;
}*/
u = Uri.parse(mySongs.get(position).toString());
mp = MediaPlayer.create(getApplicationContext(), u);
mp.start();
default:
break;
}
}
}
While the above code works perfectly fine for few songs which are mp3 but it does not work for other songs which are also mp3 . I have tried this on an actual device the app would just crash if the song has some problem
the mp3 files which give me problems work perfectly on other media players and even the default android media player
What exactly could be the issue?
you need to call mediaPlayer.prepare() or mediaPlayer.prepareAsync() before you call , mediaPlayer.start(), also if the application crash, post your log so we can know the problem.
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I have been looking around for the answer but no luck on this. to resume
the id of my button is correct
Activity names are correct, but for some reason the Bundle is returning null and crashing my App, This is my Main Activity, please help.
Check the OnClick Listener I think I am using the correct form
Main Activity
package com.example.carlos.application1;
import android.widget.Button;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "MESSAGE";
private ListView obj;
DBHelper mydb;
int status = 0;
Button hp_1, HP2, fo, previous, home, next,MZ,Control,show;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mydb = new DBHelper(this);
ArrayList array_list = mydb.getAllCotacts();
ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);
obj = (ListView)findViewById(R.id.listView1);
obj.setAdapter(arrayAdapter);
obj.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
int id_To_Search = arg2 + 1;
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new Intent(getApplicationContext(), DisplayValues.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});
hp_1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
status = 1;
Bundle bundle = new Bundle();
bundle.putInt("status", status);
Intent intent = new Intent(MainActivity.this, DisplayValues.class);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
super.onOptionsItemSelected(item);
switch(item.getItemId())
{
case R.id.item1:Bundle dataBundle = new Bundle();
dataBundle.putInt("id", 0);
Intent intent = new Intent(getApplicationContext(),DisplayValues.class);
intent.putExtras(dataBundle);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public boolean onKeyDown(int keycode, KeyEvent event) {
if (keycode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
}
return super.onKeyDown(keycode, event);
}
}
you haven't assigned anything to hp_1 so its value is NULL and that is the problem. You need to do the following:
hp_1 = (Button) findViewById(...);
hp_1.setOnClickListener(new View.OnClickListener() {
...
enter code hereThis is my First Activity::> I need:
1) select an option
2) click send an image (from drawable) should go to the second activity but it does not only from 2nd to 1st works fine all my xml and manifest files are correct (I double checked)
package com.example.damianlopez.myassignment;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button toBob;
private ImageView burgerImage;
private RadioGroup radioGroupChoice;
private RadioButton radBurger;
private RadioButton radScare;
private RadioButton radSick;
private final int MY_SELECTION = 111;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.activity_main);
//get radio Buttons
//radioGroupChoice = (RadioGroup) findViewById(R.id.radGroupChoice);
radBurger = (RadioButton)findViewById(R.id.radBurger);
radScare = (RadioButton)findViewById(R.id.radScare);
radSick = (RadioButton)findViewById(R.id.radSick);
//display app icon
ActionBar ab = getSupportActionBar();
ab.setDisplayShowHomeEnabled(true);
ab.setIcon(R.mipmap.ic_launcher);
//getting Intent
Intent getIntent = getIntent();
Bundle myBundle2 = getIntent.getExtras();
setResult(Activity.RESULT_OK, getIntent);
//
}catch(Exception e ){
}
}//End onCreate()
public void onClickButton(View v){
try {
//get radio gruop id's
radioGroupChoice = (RadioGroup) findViewById(R.id.radGroupChoice);
int mySelection = radioGroupChoice.getCheckedRadioButtonId();
if(radioGroupChoice.getCheckedRadioButtonId() == -1){
Toast.makeText(getApplicationContext(), "Please make a selection", Toast.LENGTH_LONG).show();
}else {
//creating new Intent
Intent msgToBob = new Intent(MainActivity.this, Main22Activity.class);
Bundle myBundle = new Bundle();
myBundle.putInt("imageChoice", mySelection);
msgToBob.putExtras(myBundle);
startActivityForResult(msgToBob, MY_SELECTION);
}
}catch(Exception e ){
}
}//End of onClick()
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent myData) {
super.onActivityResult(requestCode, resultCode, myData);
Log.i("OnActivityResult() call", "***********");
radioGroupChoice = (RadioGroup) findViewById(R.id.radGroupChoice);
int selected = radioGroupChoice.getCheckedRadioButtonId();
//get Images
burgerImage = (ImageView) findViewById(R.id.pumpkin_burger);
if(radioGroupChoice.getCheckedRadioButtonId() == -1){
Toast.makeText(getApplicationContext(), "Please make a selection", Toast.LENGTH_LONG).show();
}
else {
switch (requestCode) {
case MY_SELECTION: {
if (resultCode == Activity.RESULT_OK) {
if (radBurger.isChecked()) {
burgerImage.setImageResource(R.drawable.pumpkin_burger);
} else if (radScare.isChecked()) {
burgerImage.setImageResource(R.drawable.pumpkin_scared);
} else if (radSick.isChecked()) {
burgerImage.setImageResource(R.drawable.pumpkin_sick);
};
}
break;
}//end case
}//end switch
}//end else if
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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();``
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
This is my Second Activity===>
package com.example.damianlopez.myassignment;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class Main22Activity extends AppCompatActivity {
private RadioGroup radioGroupChoice2;
private RadioButton radBurger;
private RadioButton radScare;
private RadioButton radSick;
private Button toAlice;
private ImageView currentImage;
private ImageView scareImage;
private ImageView sickImage;
private final int MY_SELECTION2 = 222;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main22);
//display app icon
ActionBar ab = getSupportActionBar();
ab.setDisplayShowHomeEnabled(true);
ab.setIcon(R.mipmap.ic_launcher);
//Buttons
radioGroupChoice2 = (RadioGroup)findViewById(R.id.radGroupChoice);
//toAlice = (Button) findViewById(R.id.sndToAlice);
//get the intent called
Intent myLocalIntent = getIntent();
Bundle myBundle = myLocalIntent.getExtras();
myLocalIntent.putExtras(myBundle);
setResult(Activity.RESULT_OK, myLocalIntent);
}
public void onClickButton2(View v){
try {
radioGroupChoice2 = (RadioGroup) findViewById(R.id.radGroupChoice);
if (radioGroupChoice2.getCheckedRadioButtonId() == -1) {
Toast.makeText(getApplicationContext(), "Please make a selection", Toast.LENGTH_LONG).show();
} else {
Intent myIntent2 = new Intent();
Bundle myBundle2 = new Bundle();
int myImage = radioGroupChoice2.getCheckedRadioButtonId();
myBundle2.putInt("selectedImage", myImage);
myIntent2.putExtras(myBundle2);
setResult(Activity.RESULT_OK, myIntent2);
}
}catch(Exception e) {
}
finish();
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main22, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
package com.example.assignmentone;
import java.util.ArrayList;
import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.method.DigitsKeyListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AlphabetIndexer;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText et;
static TableLayout tl;
Intent intent;
Button btn2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText) findViewById(R.id.et);
//et2=(EditText) findViewById(R.id.et2);
tl=(TableLayout) findViewById(R.id.tl);
btn2=(Button) findViewById(R.id.btn2);
}
public void get(View v)
{
startActivity(intent);
}
public void doo(View v)
{
if(et.getText().toString().isEmpty()||Integer.parseInt(et.getText().toString())==0)
{
Toast.makeText(this, "Please Enter Valid Row #", Toast.LENGTH_SHORT).show();
//Toast.makeText(this, et2.getInputType()+"", Toast.LENGTH_SHORT).show();
}else {
btn2.setEnabled(true);
power(Integer.parseInt(et.getText().toString()));
}
}
public void power(int r){
//r=Integer.parseInt(et.getText().toString());
//ArrayList<View>[][] table1 = new ArrayList[r][4];
tl.removeAllViews();
for(int i=0;i<=r;i++)
{
TableRow tRow=new TableRow(this);
tRow.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
for(int j=0;j<4;j++)
{
if(i==0)
{
TextView tv=new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
if(j==0)
{
tv.setKeyListener(DigitsKeyListener.getInstance(" abcdef:-."));
tv.setText("Name");
tRow.addView(tv);
}
else if(j==1)
{
tv.setText("Subject 1");
tRow.addView(tv);
}
else if(j==2)
{
tv.setText("Subject 2");
tRow.addView(tv);
}
else if( j==3)
{
tv.setText("Subjct 3");
tRow.addView(tv);
}
}
else if(i>0)
{
EditText temp=new EditText(this);
temp.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
if(j==0)
{
temp.setInputType(1);
tRow.addView(temp);
}else if(j==1)
{
temp.setInputType(4098);
tRow.addView(temp);
}
else if(j==2)
{
temp.setInputType(4098);
tRow.addView(temp);
}
else if( j==3)
{
temp.setInputType(4098);
tRow.addView(temp);
}
}
}
tl.addView(tRow);
}
}
#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 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);
}
}
You can use putExtra and getExtra to send and receive data from activity to another. With this method you can send strings Serializable objects ... etc.
see this example for sending Strings and this example for sending Objects
as i said, how do i direct to the next activity after successfully login? im kinda stuck for a while now, and it is necessary for this to run. im practicing my android skills awhile back. so i am using android developer studio for my practices of developing android. this is my code. hope you guys can help.
`package com.mikesaclolo.pinasarap;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class MainActivity extends Activity {
Button b1,b2;
EditText ed1,ed2;
TextView tx1;
int counter = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
ed1=(EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
b2=(Button)findViewById(R.id.button2);
tx1=(TextView)findViewById(R.id.textView3);
tx1.setVisibility(View.GONE);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(ed1.getText().toString().equals("admin") &&
ed2.getText().toString().equals("admin")) {
Toast.makeText(getApplicationContext(), "Redirecting...",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "Wrong Credentials",Toast.LENGTH_SHORT).show();
tx1.setVisibility(View.VISIBLE);
tx1.setBackgroundColor(Color.RED);
counter--;
tx1.setText(Integer.toString(counter));
if (counter == 0) {
b1.setEnabled(false);
}
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}`
start activity using Intent :
if(ed1.getText().toString().equals("admin") &&
ed2.getText().toString().equals("admin")) {
Toast.makeText(getApplicationContext(), "Redirecting...",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(MainActivity.this,LoginActivity.class); // redirecting to LoginActivity.
startActivity(intent);
}
I have an application that extends ActionBarActivity, but I'm using ListView and I need to implement OnItemClickListener and I'm not sure how to do this without extending ListActivity. Also I want to ask you: Moreover I have a button and I need to listen if the button is clicked or if an item in the list is clicked. I'm not pretty sure how to do this.
So I would really appreciate if you help me :)
Here is my code:
package com.src.vicnote;
import java.io.File;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
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.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
Button newButton;
String path = Environment.getExternalStorageDirectory().toString()+"/VICNote";
String lastOfPath;
File f = new File(path);
File files[] = f.listFiles();
String[] theNamesOfFiles = new String[files.length + 1];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
theNamesOfFiles[0] = "Create new note";
for(int i = 1; i < theNamesOfFiles.length; i++) {
lastOfPath = files[i-1].toString().split("/")[files[i-1].toString().split("/").length-1];
theNamesOfFiles[i] = lastOfPath.replace(".txt","");
Log.d("Files", "String: " + theNamesOfFiles[i]);
}
ListView lv = (ListView) findViewById(R.id.notesList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
theNamesOfFiles);
lv.setAdapter(adapter);
newButton = (Button) findViewById(R.id.buttonNew);
newButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent newNote = new Intent(MainActivity.this, NewNoteActivity.class);
startActivity(newNote);
}
});
}
#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 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);
}
}
You can implement the OnItemClickListener interface and override onItemClick
lv.setAdapter(adapter);
lv.setOnItemClickListener( new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(MainActivity.this.this, "List View row Clicked at"+position,Toast.LENGTH_SHORT).show();
}
});
If the button belongs to activity_main.xml then what you have
newButton.setOnClickListener(new View.OnClickListener()
is right
You can set item click listener for ListView using this code.
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
// it is used to get the clicked string
String theNamesOfFile = adapter.getItem(position);
}
});
I guess it can help you.