我在我的應用程序中有一個列表視圖,它填充了手機聯繫人,我想要獲取所選聯繫人的數據,如果用戶按下該聯繫人,它將播放與所選項目相關的聲音。 我該怎麼做。Android listview onitemselected
這裏是我的代碼:
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.Typeface;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Display extends Activity implements OnItemClickListener{
final String MEDIA_PATH = new String("/sdcard/AudioRecorder");
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
private String mp3Pattern = ".mp4";
//String phoneNumber;
List<String> name1 = new ArrayList<String>();
List<String> phno1 = new ArrayList<String>();
MyAdapter ma ;
Button select;
MainActivitySound music;
Cursor phones ;
TextView tv1,tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
getAllContacts(this.getContentResolver());
// View popupView = mInflater.inflate(R.layout.display, null);
ListView lv= (ListView)findViewById(android.R.id.list);
ma = new MyAdapter();
lv.setAdapter(ma);
lv.setOnItemClickListener(this);
lv.setItemsCanFocus(false);
lv.setTextFilterEnabled(true);
@Override
public void onItemClick(AdapterView<?> parent, View view, int index, long arg3) {
Object obj = ma.getItem(index); // ma is your adapter as you mentioned. obj will be the contact associoated with the selected row in the list
String phoneNumber=obj.toString();
// String phoneNumber=i.toString();
//String s=String.valueOf(index);
Toast.makeText(Display.this, phoneNumber,1000).show();
}
private void play(String phoneNumber) {
// TODO Auto-generated method stub
try{
MediaPlayer mp=new MediaPlayer();
mp.setDataSource("/sdcard/AudioRecorder/566.mp4");
mp.prepare();
mp.start();
}catch(Exception e)
{e.printStackTrace();}
}
public void getAllContacts(ContentResolver cr) {
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//music.play(phoneNumber);
System.out.println(".................."+phoneNumber);
name1.add(name);
phno1.add(phoneNumber);
//play(phoneNumber);
}
phones.close();
}
class MyAdapter extends BaseAdapter //implements CompoundButton.OnCheckedChangeListener
{ //private SparseBooleanArray mCheckStates;
LayoutInflater mInflater;
//TextView tv1,tv;
// CheckBox cb;
MyAdapter()
{
// mCheckStates = new SparseBooleanArray(name1.size());
mInflater = (LayoutInflater)Display.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return name1.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-genferated method stub
return tv1.getText();
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
// @Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = mInflater.inflate(R.layout.row, null);
TextView tv= (TextView) vi.findViewById(R.id.textView1);
tv1= (TextView) vi.findViewById(R.id.textView2);
//cb = (CheckBox) vi.findViewById(R.id.checkBox1);
tv.setText("Name :"+ name1.get(position));
tv1.setText("Phone No :"+ phno1.get(position));
tv1.getText();
// tv.setTypeface(tvFont);
// tv.setTextColor(Color.BLUE);
// cb.setTag(position);
// cb.setChecked(mCheckStates.get(position, false));
// cb.setOnCheckedChangeListener(this);
return vi;
}
public void getPhone(final int position,View convertView) {
// TODO Auto-generated method stub
View vi=convertView;
tv1= (TextView) vi.findViewById(R.id.textView2);
tv1.setText("Phone No :"+ phno1.get(position));
String s= (String) tv1.getText();
}
public ArrayList<HashMap<String, String>> getPlayList() {
System.out.println(MEDIA_PATH);
if (MEDIA_PATH != null) {
File home = new File(MEDIA_PATH);
File[] listFiles = home.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
System.out.println(file.getAbsolutePath());
if (file.isDirectory()) {
scanDirectory(file);
} else {
addSongToList(file);
}
}
}
}
// return songs list array
return songsList;
}
private void scanDirectory(File directory) {
if (directory != null) {
File[] listFiles = directory.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
if (file.isDirectory()) {
scanDirectory(file);
} else {
addSongToList(file);
}
}
}
}
}
private void addSongToList(File song) {
if (song.getName().endsWith(mp3Pattern)) {
HashMap<String, String> songMap = new HashMap<String, String>();
songMap.put("songTitle",
song.getName().substring(0, (song.getName().length() - 4)));
songMap.put("songPath", song.getPath());
System.out.println(song.getPath());
// Adding each song to SongList
songsList.add(songMap);
}
}
}
}
試圖創建列表視圖onItemClickListener更換活動。例如lv.setOnItemClickListener(新的OnItemClickListener等)。 – Lunchbox