在ListView中的每个名称下面重复TextView标题和Button

时间:2012-09-20 07:16:09

标签: android android-layout listview button textview

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

ListView of the names with Heading & Button

不仅如此,textview也位于列表下方的列表和按钮之上。这很古怪。 我不确定如何使列表上方的TextView和按钮仅在列表下方。

哦,是的,请看看我的java和xml布局代码谢谢 -
contacts_list.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();    
    } 
}

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

1 个答案:

答案 0 :(得分: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);