2014-03-04 37 views
0

我想用複選框創建聯繫人列表。所以我必須選擇聯繫人發送羣組消息;我試過這個代碼,但它不起作用... 你能向我建議如何進一步獲取聯繫人列表。帶複選框的聯繫人列表,用於選擇羣組消息

MainActivity.java

package com.example.cont1; 

import java.util.ArrayList; 

import com.example.cont.MainActivity; 
import com.example.listo1.Demo; 
import com.example.listo1.R; 

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.provider.ContactsContract; 
import android.app.Activity; 
import android.app.ListActivity; 
import android.app.ProgressDialog; 
import android.content.ContentResolver; 
import android.database.Cursor; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

public class MainActivity extends ListActivity { 
    StringBuilder sb= new StringBuilder(); 
    String str; 
    ListView tx; 
    private Contact selectedAdapter; 
    private ArrayList<String> list; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

    class ast extends AsyncTask<String, Void, String> { 
     String name, phone; 
     ProgressDialog pd; 

     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 
      super.onPreExecute(); 

      pd = ProgressDialog.show(MainActivity.this, "", "loading"); 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 
      pd.cancel(); 
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, str); 
      setListAdapter(adapter); 
      getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
      //textDetail.setText(sb); 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      // TODO Auto-generated method stub 

      ContentResolver cr = getContentResolver(); 
      Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
      String phone = null; 

      if (cur.getCount() > 0) { 
       Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); 
       while (phones.moveToNext()) { 
        name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
        phone = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
        sb.append("\n " + name + "\n" + phone); 
        sb.append("\n............................\n"); 
        str=sb.toString(); 
       } 

       phones.close(); 
      } 

      return null; 
     } 
    } 
} 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     Toast.makeText(this, String.valueOf(getListView().getCheckedItemCount()), Toast.LENGTH_LONG).show(); 
     return true; 
    } 
} 

Contact.java

package com.example.cont1; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class Contact extends ArrayAdapter { 
    private MainActivity list; 
    // used to keep selected position in ListView 
    private int selectedPos = -1; // init value for not-selected 
    private Context context; 
    private String[] values; 

    public Contact(Context context, String[] values) { 
     super(context, R.layout.activity_main, values); 
     this.context = context; 
     this.values = values; 
    } 

    public void setSelectedPosition(int pos){ 
     selectedPos = pos; 
     // inform the view of this change 
     notifyDataSetChanged(); 
    } 

    public int getSelectedPosition(){ 
     return selectedPos; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     LayoutInflater vi = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = vi.inflate(R.layout.activity_main, null); 

     // get text view 
     TextView label = (TextView)v.findViewById(R.id.textView1); 
     ImageView btn=(ImageView)v.findViewById(R.id.icon); 

     if (convertView == null) {    
      v = vi.inflate(R.layout.activity_main, parent, false); 
     } 
     else 
      v = convertView; 

     TextView text1 = (TextView) v.findViewById(R.id.textView1); 
     text1.setText(values[position]); 
     LinearLayout layout_item = (LinearLayout) v.findViewById(R.id.linear); 
     //Set the background and text color 
     if (position % 2 == 0) { 
      layout_item.setBackgroundColor(context.getResources().getColor(R.color.black)); 
      text1.setTextColor(context.getResources().getColor(R.color.white)); 
     } else { 
      layout_item.setBackgroundColor(context.getResources().getColor(R.color.white)); 
      text1.setTextColor(context.getResources().getColor(R.color.black)); 
     }  

     return v; 
    } 
} 

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity"> 

    <ScrollView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

      <LinearLayout 
       android:id="@+id/linear" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 

       <TextView 
        android:id="@+id/textView1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentTop="true" 
        android:layout_margin="10dp"/> 

       <ImageView 
        android:id="@+id/icon" 
        android:layout_width="22px" 
        android:layout_height="22px" 
        android:layout_marginLeft="4px" 
        android:layout_marginRight="10px" 
        android:layout_marginTop="4px"> 
       </ImageView> 
      </LinearLayout> 
    </ScrollView> 
</LinearLayout> 

rowLayout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <ListView 
     android:id="@+id/list" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent"/> 

</LinearLayout> 
+0

現在的問題是什麼? –

+0

說大量的代碼「不起作用」並不能告訴我們問題是什麼。請更新問題以解釋代碼現在做什麼,你想要做什麼,以及你已經嘗試過什麼其他的事情,使它做你想做的事情。問題中的代碼太多,我們無法嘗試確定問題所在。 – AdrianHHH

+0

我在textview中檢索姓名和電話號碼。但我想在列表視圖中獲取這些號碼,選擇多個號碼,並一次發送郵件給那些選定的號碼。所以我的主要動機是現在選擇來自listview的多個聯繫人。 – Himani

回答

0

看來,這是相當複雜與異步任務做到這一點,但這裏是一個片段從https://stackoverflow.com/a/17878191/2545832來,但多一點完整的(但仍然相當簡單),這是工作(我測試它):

主要活動:

import java.util.ArrayList; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.ContentResolver; 
import android.content.Context; 
import android.database.Cursor; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.provider.ContactsContract; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.CheckBox; 
import android.widget.ListView; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

    ListView lvCallList; 
    ProgressDialog pd; 
    ArrayList<String> aa = new ArrayList<String>(); 
    ArrayList<String> num= new ArrayList<String>(); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     lvCallList = (ListView) findViewById(R.id.list); 

     new AsyncTask<Void, Void, Void>() 
     { 
      @Override 
      protected void onPreExecute() 
      { 
       pd = ProgressDialog.show(MainActivity.this, 
         "Loading..", "Please Wait", true, false); 
      }// End of onPreExecute method 

      @Override 
      protected Void doInBackground(Void... params) 
      { 
       getContacts(); 

       return null; 
      }// End of doInBackground method 

      @Override 
      protected void onPostExecute(Void result) 
      { 
       pd.dismiss(); 
       CustomAdapter cus = new CustomAdapter(MainActivity.this); 
       // ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,aa); 
      lvCallList.setAdapter(cus); 

      }//End of onPostExecute method 
     }.execute((Void[]) null); 
    } 
     private void getContacts() 
     { 
      ContentResolver cr = getContentResolver(); 
      Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null); 
      if (cur.getCount() > 0) 
      { 
       while (cur.moveToNext()) 
       { 
        String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 
        String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

        aa.add(name); 
        if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
        { 
         Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
               null, 
               ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
               new String[]{id}, 
               null); 
         while (pCur.moveToNext()) 
         { 
          String phoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
          num.add(phoneNumber); 
         } 
         pCur.close(); 
        } 
       } 
      } 
     } 

     public class CustomAdapter extends BaseAdapter 
     { 
      /* 
      * Variables Declaration section 
      */ 
      private Context mContext; 

      public CustomAdapter(Context context) 
      { 
       mContext = context; 
      }//End of CustomAdapter constructor 

      public int getCount() 
      { 
       return aa.size(); 
      }//End of getCount method 

      public Object getItem(int position) 
      { 
       return position; 
      }//End of getItem method 

      public long getItemId(int position) 
      { 
       return position; 
      }//End of getItemId method 

      public View getView(int position, View convertView, ViewGroup parent) 
      { 
       ViewHolder holder; 
       final int pos = position; 

       if (convertView == null) 
       { 
        holder = new ViewHolder(); 

        convertView = LayoutInflater.from(mContext).inflate(R.layout.display_contact, null); 
        holder.textviewName = (TextView) convertView.findViewById(R.id.textView1); 
        holder.textviewNumber = (TextView) convertView.findViewById(R.id.textView2); 
        holder.checkbox = (CheckBox) convertView.findViewById(R.id.checkBox1); 

        convertView.setTag(holder); 
       }//End of if condition 
       else 
       { 
        holder = (ViewHolder) convertView.getTag(); 
       }//End of else 

       holder.checkbox.setId(position); 
       holder.textviewName.setId(position); 
       holder.textviewNumber.setId(position); 


       holder.textviewName.setText(aa.get(position)); 
       holder.textviewNumber.setText("No. "+num.get(position)); 

       holder.id = position; 

       return convertView; 
      }//End of getView method 
     }//End of CustomAdapter instance inner class 

     static class ViewHolder 
     { 
      TextView textviewName; 
      TextView textviewNumber; 
      CheckBox checkbox; 
      int id; 
     } 

} 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
    <ListView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/list">  

    </ListView> 

display_contact.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <TextView android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView1"/> 
    <TextView android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView2"/> 
    <CheckBox android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/checkBox1"/> 

</LinearLayout> 

我在你的代碼,你可以選擇很多的項目,它不能是你的問題要從此列表中選擇多個聯繫人看到。您有兩種選擇:每個複選框上的偵聽器或列表上的長時間點擊偵聽器。

祝你好運:)

+0

感謝您的幫助..但這[lvCallList.setAdapter(cus);]返回null異常......爲什麼呢,請讓我理解它 – Himani

+0

有兩種解決方案。一個很簡單:你沒有清理你的項目,R.java不能正常工作(在Eclipse中清理,去Project> Clean)。另一種可能是你沒有正確複製activity_main.xml(或不正確)。 此外,如果您在設備上進行測試時沒有聯繫人,則必須謹慎,我沒有嘗試過這種可能性(因此創建任何假聯繫人) –

+0

謝謝,但是我在手機上運行此應用程序,並且還每當我運行它時,我都會清理項目。 – Himani

相關問題