Using setOnItemClickListener for items in listview
I have got an app that saves audio on sd card. I've created a listview which retrieves the filesnames from the sdcard. I'm trying to set a listener so when the file name is clicked I can launch another intent that plays that file. when I try to set the listener and pass in a new OnItemClickListener()
eclipse is red underlining it. I understand that I have to implement the onItemclick()
, this is where I think the intent ought to go.
Code:
package com.tecmark;
import java.io.File;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
public class SdGetList extends Activity {
private ListView lv1;
private String[] lv_arr;
private ArrayList<String> arr;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.listlayout);
arr = new ArrayList<String>();
File sdCardRoot = Environment.getExternalStorageDirectory();
Log.i("root on sd =", ""+ sdCardRoot.getPath());
for (File f : sdCardRoot.listFiles()) {
if (f.isFile()){
String name = f.getName();
arr.add(name);
Log.i("arr is empty?******", ""+arr.isEmpty());
开发者_如何学Go }
else {
Log.i("file", " no file");
}
Log.i("arr******", ""+arr.size());
}
lv_arr = new String[arr.size()];
for(int i=0 ; i<arr.size();i++) {
lv_arr[i] = arr.get(i);
}
lv1=(ListView)findViewById(R.id.ListView01);
// By using setAdpater method in listview we an add string array in list.
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
} );
}
}
list.setOnItemClickListener( new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent i = new Intent(ClassName.this,CourtDetailActivity.class);
startActivity(i);
});
}
this is the code is properly working I think you do not import blow Package
import android.widget.AdapterView.OnItemClickListener;
put this line to import section of the application
Check class imports in your java file. In Eclipse: Ctrl + Shift + O
精彩评论