2014-04-23 56 views
0

我在我的應用程序中有一個列表視圖,它填充了手機聯繫人,我想要獲取所選聯繫人的數據,如果用戶按下該聯繫人,它將播放與所選項目相關的聲音。 我該怎麼做。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); 
     } 
    } 

} 

}

+0

試圖創建列表視圖onItemClickListener更換活動。例如lv.setOnItemClickListener(新的OnItemClickListener等)。 – Lunchbox

回答

7

改寫爲你實現OnItemClickListener

@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 

} 

更新答案

一個bean類的活動裏面的方法合作ntacts

public class ContactBean { 

    String mName, mNumber; 

    public ContactBean(String name, String number) { 
    mName = name; 
    mNumber = number; 
    } 

    public String getName() { 
    return mName; 
    } 

    public String getNumber() { 
    return mNumber; 
    } 

    public void setName(String name) { 
    this.mName = name; 
    } 

    public void setNumber(String number) { 
    this.mNumber = number; 
    } 

} 

聯繫人列表視圖

public class MyAdapter extends BaseAdapter{ 

private Activity mActivity; 
private List<ContactBean> mContacts; 

public MyAdapter(Activity activity, List<ContactBean> contacts) { 
    mActivity = activity; 
    mContacts = contacts; 
} 

@Override 
public int getCount() { 
    return mContacts.size(); 
} 

@Override 
public Object getItem(int arg0) { 
    return mContacts.get(arg0); 
} 

@Override 
public long getItemId(int arg0) { 
    return arg0; 
} 

@Override 
public View getView(int index, View convertView, ViewGroup parent) { 
View view=convertView; 
    if(convertView==null) 
     view = mActivity.getLayoutInflater().inflate(R.layout.row, null); 

    TextView name= (TextView) view.findViewById(R.id.textView1); 
    TextView number= (TextView) view.findViewById(R.id.textView2); 
    //cb = (CheckBox) vi.findViewById(R.id.checkBox1); 
    name.setText("Name :"+ mContacts.get(position)); 
    number.setText("Phone No :"+ mContacts.get(position)); 

return view; 
} 

} 

然後將適配器與此

 

    public class Display extends Activity implements OnItemClickListener { 

     final String MEDIA_PATH = new String("/sdcard/AudioRecorder"); 
     private ArrayList> songsList = new ArrayList>(); 
     private String mp3Pattern = ".mp4"; 
     // String phoneNumber; 
     MyAdapter ma; 
     Button select; 
     MainActivitySound music; 
     Cursor phones; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.display); 

     ListView lv = (ListView) findViewById(android.R.id.list); 

     ma = new MyAdapter(this, getAllContacts(this.getContentResolver())); 

     lv.setAdapter(ma); 
     lv.setOnItemClickListener(this); 
     lv.setItemsCanFocus(false); 
     lv.setTextFilterEnabled(true); 
    } 

    @Override 
    public void onItemClick(AdapterView parent, View view, int index, long arg3) { 
     // gets the contact from adapter 
     ContactBean contact = (ContactBean) ma.getItem(index); 
     Toast.makeText(Display.this, contact.getName(), 1000).show(); 

    } 

    private void play(String phoneNumber) { 
     try { 
      MediaPlayer mp = new MediaPlayer(); 
      mp.setDataSource("/sdcard/AudioRecorder/566.mp4"); 
      mp.prepare(); 
      mp.start(); 
     } catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 

    } 

    public List getAllContacts(ContentResolver cr) { 

     List list = new ArrayList(); 
     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)); 
      list.add(new ContactBean(name, phoneNumber)); 
     } 

     phones.close(); 
     return list; 
    } 

    public ArrayList> 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 songMap = new HashMap(); 
      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); 
     } 
     } 

    } 
+0

我試圖做到這一點,但它只是給索引,但我想要的索引的文本(電話號碼)。 – user3563538

+0

您可以請發佈您嘗試過的代碼嗎? – Nikhil

+0

我使用你的確切代碼它的工作,但它給了那個位置,但我想在選定的文本視圖上的數據。 – user3563538

0
getListView.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    { 
    if(position==1) 
    { 
     final MediaPlayer player = new MediaPlayer(); 
     try 
     { 
      player.setDataSource("SD card path"); 
     } 
     catch (IllegalArgumentException e) 
     { 
      e.printStackTrace(); 
     } 
     catch (SecurityException e) 
     { 
      e.printStackTrace(); 
     } 
     catch(IllegalStateException e) 
     { 
      e.printStackTrace(); 
     } 
    } 
    else 
    { 
    //do your stuff 
    } 
    } 
    });