【问题标题】:TextView heading and Button repeated underneath each name in ListView在 ListView 中的每个名称下方重复的 TextView 标题和 Button
【发布时间】:2012-09-20 07:16:09
【问题描述】:

我有一个应用程序,它是从 Android 手机的联系人列表中检索的联系人姓名列表。
我尝试在 TextView 中添加它,它就像 ListView 上方的标题和 TextView 下方的按钮。 TextView 和 Button 在列表中的每个名称下方重复出现。看起来像这样

不仅如此,文本视图在列表上方,按钮也在列表下方。这很奇怪。 我不确定如何使列表上方的 TextView 和按钮仅位于列表下方。

哦,是的,请看一下我的 java 和 xml 布局代码,谢谢 --
联系人列表.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/light_goldenrod"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<TextView
        android:id="@+id/textInform"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Choose a Friend to Create an Event"
        android:textColor="@color/dark_purple"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:stackFromBottom="false"
        android:transcriptMode="normal" />


   <TextView
        android:textColor="@color/dark_purple"
        android:id="@+id/contactName"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

   <Button
       android:id="@+id/btnMain"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:background="@drawable/yellow_btn"
       style="@style/ButtonText"
       android:text="Main Menu" />

</LinearLayout>

ContactsList.java

public class ContactsList extends ListActivity
{
    TextView contactName;
    ListView list;

    final Context context = this;   
    Cursor cursor;
    BuddyDBAdapter buddyDB = new BuddyDBAdapter(this);

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.contacts_list);

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

        Uri allContacts = Uri.parse("content://contacts/people");

        Cursor c = managedQuery(allContacts, null, null, null, null);

        String[] columns = new String[] {ContactsContract.Contacts.DISPLAY_NAME};
        int[] views = new int[]  {R.id.contactName};

        startManagingCursor(c);

        SimpleCursorAdapter friendsAdapter = new SimpleCursorAdapter(this, R.layout.contacts_list, c, columns, views);
        this.setListAdapter(friendsAdapter);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id)
    {
        buddyDB.open();
        long name_id;
        super.onListItemClick(l, v, position, id);

           Cursor cursor = null;

           cursor = (Cursor) l.getItemAtPosition(position);
           Intent intent = new Intent(ContactsList.this, Create_Events.class);
           intent.putExtra("name", cursor.getString(cursor.getColumnIndex(buddyDB.KEY_NAME)));
           startActivity(intent);

        Cursor c = ((SimpleCursorAdapter)l.getAdapter()).getCursor();
        c.moveToPosition(position);

        TextView contactName = (TextView) v.findViewById(R.id.contactName);
        String NameValue = contactName.getText().toString();        
        name_id = buddyDB.insertNames(NameValue);

        buddyDB.close();    
    } 
}

请帮助我。
提供的任何帮助将不胜感激。谢谢=)

【问题讨论】:

标签: android android-layout listview button textview


【解决方案1】:

如下创建一个布局文件,命名为contacts_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/light_goldenrod"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
   <TextView
        android:textColor="@color/dark_purple"
        android:id="@+id/contactName"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

更改以下内容:

SimpleCursorAdapter friendsAdapter = new SimpleCursorAdapter(this, R.layout.contacts_list, c, columns, views);

到:

SimpleCursorAdapter friendsAdapter = new SimpleCursorAdapter(this, R.layout.contacts_item, c, columns, views);

【讨论】:

  • 嗨,我按照你说的做了,但部分奏效了。我的意思是 TextView 和按钮可以工作,但 android 手机的联系人列表中没有名字。我什至尝试过SimpleCursorAdapter friendsAdapter = new SimpleCursorAdapter(this, R.layout.contacts_list, c, columns, views); SimpleCursorAdapter layoutAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, c, columns, views); this.setListAdapter(friendsAdapter); this.setListAdapter(layoutAdapter);,但它仍然显示相同
  • 有关更多信息,我使用了ContentProviderContactsContract.Contacts.DISPLAY_NAME 来作为手机联系人中的姓名。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-12
  • 2021-07-29
  • 2011-11-09
  • 1970-01-01
相关资源
最近更新 更多