如何在android中动态添加文本视图到列表视图?

发布于 2024-12-13 15:47:12 字数 4065 浏览 0 评论 0原文

在此处输入图像描述

我需要将两个文本视图添加到列表视图中,如图所示。我需要的是单击联系人按钮时,姓名和号码类型会自动进入我自己的列表视图。我无法将文本视图添加到列表视图,因为我只能看到手机的联系人列表...请帮助我..提前致谢..在此是代码。

import android.app.ListActivity;

import android.os.Bundle;
import android.provider.ContactsContract;
import android.content.Intent;
import android.database.Cursor;

public class TestActivity extends ListActivity
{



    private static final int PICK_CONTACT = 0;

    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
      super.onCreate(savedInstanceState);       
    //content uri provide directory of people
      Intent intentContact = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 

      startActivityForResult(intentContact, PICK_CONTACT);
    }//onCreate

    public void onActivityResult(int requestCode, int resultCode, Intent intent) 
    {

      if (requestCode ==PICK_CONTACT)
      { 
          getContactInfo(intent);               
      }



    }//onActivityResult

    protected void getContactInfo(Intent intent)
    {
String name;
       Cursor cursor =  managedQuery(intent.getData(), null, null, null, null);      
       while (cursor.moveToNext()) 
       {           
           //contains row id
           String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

             name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 
         //whether contact list atleast have a single contact or not
           String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

           if ( hasPhone.equalsIgnoreCase("1"))
               hasPhone = "true";
           else
               hasPhone = "false" ;

           if (Boolean.parseBoolean(hasPhone)) 
           {
            Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
            while (phones.moveToNext()) 
            {
              String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            }//end
            phones.close();
           }//end

        cursor.close();
       }//end while 


    }//end method
}//end class...........

这是 xml 文件

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


<?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="?android:attr/listPreferredItemHeight"
    android:padding="6dip">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="fill_parent">
        <TextView
            android:id="@+id/toptext"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:gravity="center_vertical"
        />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1" 
            android:id="@+id/bottomtext"
            android:singleLine="true"
            android:ellipsize="marquee"
        />
    </LinearLayout>
</LinearLayout> ...

enter image description here

I need to add two text view to the list view as shown in figure..what I need is that on the click of contact button the name and the type of number get automatically into my own list view..I am not been able to add text view to the list view because of that I am only able to see phone's contact list...kindly help me please..thanks in advance..here is the code.

import android.app.ListActivity;

import android.os.Bundle;
import android.provider.ContactsContract;
import android.content.Intent;
import android.database.Cursor;

public class TestActivity extends ListActivity
{



    private static final int PICK_CONTACT = 0;

    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
      super.onCreate(savedInstanceState);       
    //content uri provide directory of people
      Intent intentContact = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 

      startActivityForResult(intentContact, PICK_CONTACT);
    }//onCreate

    public void onActivityResult(int requestCode, int resultCode, Intent intent) 
    {

      if (requestCode ==PICK_CONTACT)
      { 
          getContactInfo(intent);               
      }



    }//onActivityResult

    protected void getContactInfo(Intent intent)
    {
String name;
       Cursor cursor =  managedQuery(intent.getData(), null, null, null, null);      
       while (cursor.moveToNext()) 
       {           
           //contains row id
           String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

             name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 
         //whether contact list atleast have a single contact or not
           String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

           if ( hasPhone.equalsIgnoreCase("1"))
               hasPhone = "true";
           else
               hasPhone = "false" ;

           if (Boolean.parseBoolean(hasPhone)) 
           {
            Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
            while (phones.moveToNext()) 
            {
              String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            }//end
            phones.close();
           }//end

        cursor.close();
       }//end while 


    }//end method
}//end class...........

this is xml file

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


<?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="?android:attr/listPreferredItemHeight"
    android:padding="6dip">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="fill_parent">
        <TextView
            android:id="@+id/toptext"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:gravity="center_vertical"
        />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1" 
            android:id="@+id/bottomtext"
            android:singleLine="true"
            android:ellipsize="marquee"
        />
    </LinearLayout>
</LinearLayout> ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最冷一天 2024-12-20 15:47:12

为此,您必须制作自定义列表,只需查看本教程
Android 系列:自定义 ListView 项目和适配器
MonoDroid - 为您的 ListView 定制 ListAdapter

For this, you have to make custom list, Just Look at this tutorial
Android Series: Custom ListView items and adapters
or MonoDroid - Custom ListAdapter for your ListView

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文