从加载contactsList的AsyncTask的在ListView布局改变ImageView的资源 [英] change ImageView Resource in a ListView Layout from an AsyncTask that load contactsList

查看:222
本文介绍了从加载contactsList的AsyncTask的在ListView布局改变ImageView的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变(符合条件)的ImageView的资源,从我的AsyncTask的一个ListView布局加载一个联系人列表,并把它放到列表视图中FindPeopleFragment.java

我该怎么办呢?

抱歉,如果我的英语不好!!!!

FindPeopleFragment.java:

 进口的java.io.File;
进口java.io.FileOutputStream中;进口android.app.Fragment;
进口android.app.ProgressDialog;
进口android.database.Cursor;
进口android.database.MatrixCursor;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.net.Uri;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.provider.ContactsContract;
进口android.support.v4.widget.SimpleCursorAdapter;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ListView;公共类FindPeopleFragment扩展片段{公共FindPeopleFragment(){}    SimpleCursorAdapter mAdapter;
    MatrixCursor mMatrixCursor;    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
            捆绑savedInstanceState){
        查看rootView = inflater.inflate(R.layout.fragment_find_people,集装箱,FALSE);     //从联系人内容提供商的联系人存储在此光标
        mMatrixCursor =新MatrixCursor(新的String [] {_id,姓名,图片,详细信息});        //适配器ListView中设定的数据
        mAdapter =新SimpleCursorAdapter(getActivity()。getBaseContext()
            R.layout.lv_layout,
            空值,
            新的String [] {姓名,图片,详细信息},
            新的INT [] {R.id.tv_name,R.id.iv_photo,R.id.tv_details},0);        //获取参考列表视图
        ListView控件lstContacts =(ListView控件)rootView.findViewById(R.id.listview);        //设置适配器列表视图
        lstContacts.setAdapter(mAdapter);        //创建的AsyncTask对象检索和负载的ListView与联系人
        ListViewContactsLoader listViewContactsLoader =新ListViewContactsLoader();        //启动AsyncTask的过程来检索和负载的ListView与联系人
        listViewContactsLoader.execute();
        返回rootView;
    }    / **一个AsyncTask的类来检索和联系人列表视图负载* /
    私有类ListViewContactsLoader扩展的AsyncTask<无效,无效光标> {        //一个进度对话框有纺车,指示该应用程序的当前状态的用户
        ProgressDialog对话框= ProgressDialog.show(getActivity(),请等待,检索联系人......,真);        @覆盖
        保护光标doInBackground(虚空...... PARAMS){
            乌里contactsUri = ContactsContract.Contacts.CONTENT_URI;            //查询表ContactsContract.Contacts检索所有联系人
            光标contactsCursor = getActivity()。getContentResolver()查询(contactsUri,NULL,NULL,NULL,
            ContactsContract.Contacts.DISPLAY_NAME +ASC);            如果(contactsCursor.moveToFirst()){
                做{
                    长的ContactID = contactsCursor.getLong(contactsCursor.getColumnIndex(_ ID));                    乌里dataUri = ContactsContract.Data.CONTENT_URI;                    //查询表ContactsContract.Data检索像个别项目
                    //家庭电话,移动电话,工作电子邮件等对应于各接触
                    光标dataCursor = getActivity()。getContentResolver()查询(dataUri,空,
                                        ContactsContract.Data.CONTACT_ID +=+的ContactID,
                                        NULL,NULL);                    字符串显示名=;
                    字符串nickname =;
                    字符串HOMEPHONE =;
                    串mobilePhone =;
                    串办公电话=;
                    字符串的OtherPhone =;
                    串photoPath =+ R.drawable.blank;
                    字节[] photoByte = NULL;
                    串homeEmail =;
                    串workEmail =;
                    串的companyName =;
                    字符串title =;                    如果(dataCursor.moveToFirst()){
                        //获取显示名称
                        显示名= dataCursor.getString(da​​taCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
                        做{                            //获取昵称
                            if(dataCursor.getString(dataCursor.getColumnIndex(\"mimetype\")).equals(ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE))
                                绰号= dataCursor.getString(da​​taCursor.getColumnIndex(数据1));                            //获取电话号码
                            if(dataCursor.getString(dataCursor.getColumnIndex(\"mimetype\")).equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)){
                                开关(dataCursor.getInt(dataCursor.getColumnIndex(数据2))){
                                    案例ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                                        HOMEPHONE = dataCursor.getString(da​​taCursor.getColumnIndex(DATA1));
                                        打破;
                                    案例ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                                        mobilePhone = dataCursor.getString(da​​taCursor.getColumnIndex(DATA1));
                                        打破;
                                    案例ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                                        办公电话= dataCursor.getString(da​​taCursor.getColumnIndex(DATA1));
                                        打破;
                                    案例ContactsContract.CommonDataKinds.Phone.TYPE_OTHER:
                                        的OtherPhone = dataCursor.getString(da​​taCursor.getColumnIndex(DATA1));
                                        打破;                                }
                            }                            //获取电子邮件
                            if(dataCursor.getString(dataCursor.getColumnIndex(\"mimetype\")).equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE )){
                                开关(dataCursor.getInt(dataCursor.getColumnIndex(数据2))){
                                    案例ContactsContract.CommonDataKinds.Email.TYPE_HOME:
                                        homeEmail = dataCursor.getString(da​​taCursor.getColumnIndex(DATA1));
                                        打破;
                                    案例ContactsContract.CommonDataKinds.Email.TYPE_WORK:
                                        workEmail = dataCursor.getString(da​​taCursor.getColumnIndex(DATA1));
                                        打破;
                                }
                            }                            //获取组织细节
                            if(dataCursor.getString(dataCursor.getColumnIndex(\"mimetype\")).equals(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)){
                                的companyName = dataCursor.getString(da​​taCursor.getColumnIndex(DATA1));
                                标题= dataCursor.getString(da​​taCursor.getColumnIndex(数据4));
                            }                            //获取照片
                            if(dataCursor.getString(dataCursor.getColumnIndex(\"mimetype\")).equals(ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)){
                                photoByte = dataCursor.getBlob(dataCursor.getColumnIndex(DATA15));                                如果(photoByte!= NULL){
                                    位图位图= BitmapFactory.de codeByteArray的(photoByte,0,photoByte.length);                                    //获取缓存目录
                                    文件cacheDirectory = getActivity()getBaseContext()getCacheDir()。                                    //临时文件存储接触式图像
                                    文件TMPFILE =新的文件(cacheDirectory.getPath()+/wpta_\"+contactId+\".png);                                    //然后,FileOutputStream临时文件
                                    尝试{
                                        FileOutputStream中fOutStream =新的FileOutputStream(TMPFILE);                                        //写位图到临时文件作为PNG文件
                                        bitmap.com preSS(Bitmap.Com pressFormat.PNG,100,fOutStream);                                        //冲洗的FileOutputStream
                                        fOutStream.flush();                                        //关闭的FileOutputStream
                                        fOutStream.close();                                    }赶上(例外五){
                                        e.printStackTrace();
                                    }
                                    photoPath = tmpFile.getPath();
                                }
                            }
                        }而(dataCursor.moveToNext());
                        字符串的详细信息=;                        //串联各种信息单串
                        如果(HOMEPHONE =空&放大器;!&放大器;!homePhone.equals())
                            细节=家庭电话:+ HOMEPHONE +\\ n;
                        如果(mobilePhone =空&放大器;!&放大器;!mobilePhone.equals())
                            细节+ =MobilePhone:+ mobilePhone +\\ n;
                        如果(办公电话=空&放大器;!&放大器;!workPhone.equals())
                            细节+ =办公电话:+办公电话+\\ n;
                        如果(使OtherPhone =空&放大器;!&放大器;!otherPhone.equals())
                            细节+ =的OtherPhone:+的OtherPhone +\\ n;
                        如果(昵称= NULL&放大器;!&安培;!nickName.equals())
                            细节+ =的绰号:+绰号+\\ n;
                        如果(homeEmail =空&放大器;!&放大器;!homeEmail.equals())
                            细节+ =HomeEmail:+ homeEmail +\\ n;
                        如果(workEmail =空&放大器;!&放大器;!workEmail.equals())
                            细节+ =WorkEmail:+ workEmail +\\ n;
                        如果(的companyName =空&放大器;!&放大器;!companyName.equals())
                            细节+ =公司名称:+的companyName +\\ n;
                        如果(标题= NULL&放大器;!&安培;!title.equals())
                            细节+ =标题:+标题+\\ n;                        //添加ID,显示名称,路径,照片和其他细节光标
                        mMatrixCursor.addRow(新的对象[] {Long.toString(的ContactID),显示名,photoPath,细节});                    }
                }而(contactsCursor.moveToNext());
            }
            返回mMatrixCursor;
        }        @覆盖
        保护无效onPostExecute(光标结果){
            //设置包含联系人列表视图光标
            mAdapter.swapCursor(结果);            dialog.dismiss();
        }    }
}

lv_layout.xml:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=横向>    < ImageView的
        机器人:ID =@ + ID / iv_puce
        机器人:layout_height =60dp
        机器人:layout_width =60dp
        机器人:填充=10dp
        机器人:SRC =@绘制/无
        />    < ImageView的
        机器人:ID =@ + ID / iv_photo
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:填充=10dp/>    <的LinearLayout
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=垂直>        <的TextView
            机器人:ID =@ + ID / tv_name
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:填充=4DP
            机器人:TEXTSIZE =15sp/>        <的TextView
            机器人:ID =@ + ID / tv_details
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:填充=4DP
            机器人:TEXTSIZE =12SP/>    < / LinearLayout中>< / LinearLayout中>

我想要做的条件是从列表视图每个联系人相关的电话号码
它consiste得到提供者号码,并将其与(getProvider())比较,并告诉它是数字的提供商
并把他的深褐色图像(从绘制)在ImageView的(iv_puce)

getProvider():

 的String [] preFIX =新的String [] {0610,0611,0613,0615,0616,0618, 0622,0623,
                                        0624,0628,0641,0642,0648,0650,0651,0652,
                                        0653,0654,0655,0658,0659,0661,0662,0666,
                                        0667,0668,0670,0671,0672,0673,0676,0677,
                                        0678,,0612,0614,0617,0619,0620,0644,0645,
                                        0649,0656,0657,0660,0663,0664,0665,0669,0674
                                        ,0675,0679,,0600,0601,0602,0603,0606,0626,
                                        0627,0629,0630,0633,0634,0635,0638,0699,,
                                        0640,0646,0647,,0526,0527,0540,0546,0547,
                                        0533,0534,0550,0553};        的String [] =提供新的String [] {IAM,meditel,inwi,modem_inwi,BAYN};        公共字符串getProvider(字符串NUM){
            // 0642212125
            // + 212642848
            串纳秒;
            弦乐味精=;
            如果(num.indexOf(+)!= - 1){
                纳秒=0+ num.substring(4,7);
            }其他{
                NS = num.substring(0,4);
            }
            INT J = 0;
            布尔发现= FALSE;
            的for(int i = 0; I< prefix.length-1;我++){
                如果(preFIX [Ⅰ] .equals(纳秒)及发现==假){
                    味精=提供商[J]。
                    发现= TRUE;
                    如果(MSG ==inwi){
                        ImageView的Imgview =(ImageView的)findViewById(R.id.imgViewOperator2);
                        Imgview.setImageResource(R.drawable.inwi);
                    }
                    否则,如果(MSG ==meditel){
                        ImageView的Imgview =(ImageView的)findViewById(R.id.imgViewOperator2);
                        Imgview.setImageResource(R.drawable.meditel);
                    }
                    否则,如果(MSG ==BAYN){
                        ImageView的Imgview =(ImageView的)findViewById(R.id.imgViewOperator2);
                        Imgview.setImageResource(R.drawable.bayn);
                    }
                    否则,如果(MSG ==IAM){
                        ImageView的Imgview =(ImageView的)findViewById(R.id.imgViewOperator2);
                        Imgview.setImageResource(R.drawable.iam);
                    }
                    否则,如果(MSG ==modem_inwi){
                        ImageView的Imgview =(ImageView的)findViewById(R.id.imgViewOperator2);
                        Imgview.setImageResource(R.drawable.modem_inwi);
                    }
                    其他{
                        ImageView的Imgview =(ImageView的)findViewById(R.id.imgViewOperator2);
                        Imgview.setImageResource(R.drawable.none);
                    }
                    //document.getElementById(\"src_img\").innerHTML =< IMG SRC =图像/ puces /+供应商[J] +的.png'ALT =''/>中;
                    //突破;
                }
                否则,如果(preFIX [I] ==&放大器;发现== FALSE){
                    J + = 1;
                    //味精= J ++ NS ++供应商[J]。
                }
                否则,如果(我== prefix.length&放大器;发现== FALSE){
                    味精=;
                    //document.getElementById(\"src_img\").innerHTML =< IMG SRC =图像/ puces / none.png'ALT =''/>中;
                }
            };
            返回味精;
        }


解决方案

您需要让ListView的知道您的适配器的数据已经改变。做下面你onPostExecute方法。

  @覆盖
    保护无效onPostExecute(光标结果){
        //设置包含联系人列表视图光标
        mAdapter.swapCursor(结果);        //添加此行
        mAdapter.notifyDataSetChanged();        dialog.dismiss();
    }

祝你好运。

i want to change (with a condition) an ImageView resource in a ListView Layout from My AsyncTask that load a contacts list and put it to the listview in FindPeopleFragment.java

how can i do it ?

sorry if my english is bad !!!!

FindPeopleFragment.java:

import java.io.File;
import java.io.FileOutputStream;

import android.app.Fragment;
import android.app.ProgressDialog;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class FindPeopleFragment extends Fragment  {

public FindPeopleFragment(){}

    SimpleCursorAdapter mAdapter;
    MatrixCursor mMatrixCursor;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {


        View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);



     // The contacts from the contacts content provider is stored in this cursor
        mMatrixCursor = new MatrixCursor(new String[] { "_id","name","photo","details"} );

        // Adapter to set data in the listview
        mAdapter = new SimpleCursorAdapter(getActivity().getBaseContext(),
            R.layout.lv_layout,
            null,
            new String[] { "name","photo","details"},
            new int[] { R.id.tv_name,R.id.iv_photo,R.id.tv_details}, 0);

        // Getting reference to listview
        ListView lstContacts = (ListView) rootView.findViewById(R.id.listview);

        // Setting the adapter to listview
        lstContacts.setAdapter(mAdapter);

        // Creating an AsyncTask object to retrieve and load listview with contacts
        ListViewContactsLoader listViewContactsLoader = new ListViewContactsLoader();

        // Starting the AsyncTask process to retrieve and load listview with contacts
        listViewContactsLoader.execute();




        return rootView;
    }

    /** An AsyncTask class to retrieve and load listview with contacts */
    private class ListViewContactsLoader extends AsyncTask<Void, Void, Cursor>{

        //A Progress dialog with a spinning wheel, to instruct the user about the app's current state
        ProgressDialog dialog = ProgressDialog.show(getActivity(), "Please Wait", "Retrieving Contacts...", true);

        @Override
        protected Cursor doInBackground(Void... params) {
            Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;

            // Querying the table ContactsContract.Contacts to retrieve all the contacts
            Cursor contactsCursor = getActivity().getContentResolver().query(contactsUri, null, null, null,
            ContactsContract.Contacts.DISPLAY_NAME + " ASC ");

            if(contactsCursor.moveToFirst()){
                do{
                    long contactId = contactsCursor.getLong(contactsCursor.getColumnIndex("_ID"));

                    Uri dataUri = ContactsContract.Data.CONTENT_URI;

                    // Querying the table ContactsContract.Data to retrieve individual items like
                    // home phone, mobile phone, work email etc corresponding to each contact
                    Cursor dataCursor = getActivity().getContentResolver().query(dataUri, null,
                                        ContactsContract.Data.CONTACT_ID + "=" + contactId,
                                        null, null);

                    String displayName="";
                    String nickName="";
                    String homePhone="";
                    String mobilePhone="";
                    String workPhone="";
                    String otherPhone="";
                    String photoPath="" + R.drawable.blank;
                    byte[] photoByte=null;
                    String homeEmail="";
                    String workEmail="";
                    String companyName="";
                    String title="";

                    if(dataCursor.moveToFirst()){
                        // Getting Display Name
                        displayName = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME ));
                        do{

                            // Getting NickName
                            if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE))
                                nickName = dataCursor.getString(dataCursor.getColumnIndex("data1"));



                            // Getting Phone numbers
                            if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)){
                                switch(dataCursor.getInt(dataCursor.getColumnIndex("data2"))){
                                    case ContactsContract.CommonDataKinds.Phone.TYPE_HOME :
                                        homePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                                        break;
                                    case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE :
                                        mobilePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                                        break;
                                    case ContactsContract.CommonDataKinds.Phone.TYPE_WORK :
                                        workPhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                                        break;
                                    case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER :
                                        otherPhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                                        break;

                                }
                            }



                            // Getting EMails
                            if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE ) ) {
                                switch(dataCursor.getInt(dataCursor.getColumnIndex("data2"))){
                                    case ContactsContract.CommonDataKinds.Email.TYPE_HOME :
                                        homeEmail = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                                        break;
                                    case ContactsContract.CommonDataKinds.Email.TYPE_WORK :
                                        workEmail = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                                        break;
                                }
                            }

                            // Getting Organization details
                            if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)){
                                companyName = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                                title = dataCursor.getString(dataCursor.getColumnIndex("data4"));
                            }

                            // Getting Photo
                            if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)){
                                photoByte = dataCursor.getBlob(dataCursor.getColumnIndex("data15"));

                                if(photoByte != null) {
                                    Bitmap bitmap = BitmapFactory.decodeByteArray(photoByte, 0, photoByte.length);

                                    // Getting Caching directory
                                    File cacheDirectory = getActivity().getBaseContext().getCacheDir();

                                    // Temporary file to store the contact image
                                    File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"+contactId+".png");

                                    // The FileOutputStream to the temporary file
                                    try {
                                        FileOutputStream fOutStream = new FileOutputStream(tmpFile);

                                        // Writing the bitmap to the temporary file as png file
                                        bitmap.compress(Bitmap.CompressFormat.PNG,100, fOutStream);

                                        // Flush the FileOutputStream
                                        fOutStream.flush();

                                        //Close the FileOutputStream
                                        fOutStream.close();

                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                    photoPath = tmpFile.getPath();
                                }
                            }
                        }while(dataCursor.moveToNext());
                        String details = "";

                        // Concatenating various information to single string
                        if(homePhone != null && !homePhone.equals("") )
                            details = "HomePhone : " + homePhone + "\n";
                        if(mobilePhone != null && !mobilePhone.equals("") )
                            details += "MobilePhone : " + mobilePhone + "\n";
                        if(workPhone != null && !workPhone.equals("") )
                            details += "WorkPhone : " + workPhone + "\n";
                        if(otherPhone != null && !otherPhone.equals("") )
                            details += "OtherPhone : " + otherPhone + "\n";
                        if(nickName != null && !nickName.equals("") )
                            details += "NickName : " + nickName + "\n";
                        if(homeEmail != null && !homeEmail.equals("") )
                            details += "HomeEmail : " + homeEmail + "\n";
                        if(workEmail != null && !workEmail.equals("") )
                            details += "WorkEmail : " + workEmail + "\n";
                        if(companyName != null && !companyName.equals("") )
                            details += "CompanyName : " + companyName + "\n";
                        if(title != null && !title.equals("") )
                            details += "Title : " + title + "\n";

                        // Adding id, display name, path to photo and other details to cursor
                        mMatrixCursor.addRow(new Object[]{ Long.toString(contactId),displayName,photoPath,details});



                    }


                }while(contactsCursor.moveToNext());
            }
            return mMatrixCursor;
        }

        @Override
        protected void onPostExecute(Cursor result) {
            // Setting the cursor containing contacts to listview
            mAdapter.swapCursor(result);

            dialog.dismiss();
        }



    }


}

lv_layout.xml:

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

    <ImageView
        android:id="@+id/iv_puce"
        android:layout_height="60dp"
        android:layout_width="60dp"
        android:padding="10dp"
        android:src="@drawable/none"
        />

    <ImageView
        android:id="@+id/iv_photo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/tv_details"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:textSize="12sp" />

    </LinearLayout>

</LinearLayout>

the condition i want to do is related to the phone number from each contact in the listview it consiste to get the provider number and compare it with (getProvider()) and tell which is the provider of the number and put his puce image (from drawable) in the ImageView (iv_puce)

getProvider():

String[] prefix = new String[]{"0610","0611","0613","0615","0616","0618","0622","0623",
                                        "0624","0628","0641","0642","0648","0650","0651","0652",
                                        "0653","0654","0655","0658","0659","0661","0662","0666",
                                        "0667","0668","0670","0671","0672","0673","0676","0677",
                                        "0678"," ","0612","0614","0617","0619","0620","0644","0645",
                                        "0649","0656","0657","0660","0663","0664","0665","0669","0674"
                                        ,"0675","0679"," ","0600","0601","0602","0603","0606","0626",
                                        "0627","0629","0630","0633","0634","0635","0638","0699"," ",
                                        "0640","0646","0647"," ","0526","0527","0540","0546","0547",
                                        "0533","0534","0550","0553"};

        String[] provider = new String[]{"iam","meditel","inwi","modem_inwi","bayn"};

        public String getProvider(String num){
            //0642212125
            //+212642848
            String ns;
            String msg = "";
            if(num.indexOf("+") != -1){
                ns = "0" +num.substring(4, 7);
            }else{
                ns = num.substring(0,4);
            }
            int j = 0;
            boolean found = false;
            for (int i = 0; i < prefix.length-1; i++) {
                if(prefix[i].equals(ns) & found == false){
                    msg = provider[j] ;
                    found = true;
                    if (msg == "inwi"){
                        ImageView Imgview = (ImageView) findViewById(R.id.imgViewOperator2);
                        Imgview.setImageResource(R.drawable.inwi);
                    }
                    else if (msg == "meditel"){
                        ImageView Imgview = (ImageView) findViewById(R.id.imgViewOperator2);
                        Imgview.setImageResource(R.drawable.meditel);
                    }
                    else if (msg == "bayn"){
                        ImageView Imgview = (ImageView) findViewById(R.id.imgViewOperator2);
                        Imgview.setImageResource(R.drawable.bayn);
                    }
                    else if (msg == "iam"){
                        ImageView Imgview = (ImageView) findViewById(R.id.imgViewOperator2);
                        Imgview.setImageResource(R.drawable.iam);
                    }
                    else if (msg == "modem_inwi"){
                        ImageView Imgview = (ImageView) findViewById(R.id.imgViewOperator2);
                        Imgview.setImageResource(R.drawable.modem_inwi);
                    }
                    else{
                        ImageView Imgview = (ImageView) findViewById(R.id.imgViewOperator2);
                        Imgview.setImageResource(R.drawable.none);
                    }
                    //document.getElementById("src_img").innerHTML = "<img src='images/puces/" + provider[j] + ".png' alt='' />";
                    //              break;
                }
                else if(prefix[i] == " " & found == false){
                    j += 1;
                    //              msg = j + " " + ns + " " + provider[j];
                }
                else if(i == prefix.length & found == false){
                    msg = "";
                    //document.getElementById("src_img").innerHTML = "<img src='images/puces/none.png' alt='' />";
                }
            };
            return msg;
        }

解决方案

You need to let the ListView know that your adapter's data has changed. Do the below in your onPostExecute method.

 @Override
    protected void onPostExecute(Cursor result) {
        // Setting the cursor containing contacts to listview
        mAdapter.swapCursor(result);

        // Add this line
        mAdapter.notifyDataSetChanged();

        dialog.dismiss();
    }

Good luck.

这篇关于从加载contactsList的AsyncTask的在ListView布局改变ImageView的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆