为什么我的ListView onListItemClick不会将ListView作为文本值返回?

时间:2011-08-03 13:59:23

标签: android

如果这是我的全部新闻,我道歉。我是android编程的新手,我很沮丧!我已经看过并尝试了很多这样的例子,但它们似乎都产生了相同的结果。

底线是......我有联系人姓名的列表视图。当我点击一个名字时,我希望吐司重复他们的名字。但是我没有给他们的名字烘烤,而是继续获得像“android.content.ContentResolver$CursorWrapperInner@40571e90”这样的价值,我希望看到“约翰史密斯”

import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class ContactListOne extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_list_one);
        populateContactListOne();
   }

    private Cursor getContacts()
    {
        // Run query
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME
        };
        return managedQuery(uri, projection, null, null, null);
    }

    private void populateContactListOne() {
        Cursor cursor = getContacts();
        //Build adapter with contact info

        String[] fields = new String[] {

                ContactsContract.Contacts.DISPLAY_NAME
        };
        int[] to = new int[]{

                R.id.contactname
        };

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_list_one_rows, cursor, fields, to);

        setListAdapter(adapter);

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);

        String keyword =  getListAdapter().getItem(position).toString();

        Toast.makeText(this, "You selected: " + keyword,Toast.LENGTH_LONG).show();
    }
}

以下是我的2 xml布局:

contact_list_one:     

    <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:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:id="@android:id/list"

                  />



    </LinearLayout>

contact_list_one_rows:

<?xml version="1.0" encoding="utf-8"?>



                   <TextView 
                  xmlns:android="http://schemas.android.com/apk/res/android"
                  android:textAppearance="?android:attr/textAppearanceMedium"
                  android:id="@+id/contactname"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:padding="7dip"
                  >
                  </TextView>

----------------------------------更新------------ ------------------------------

我不确定这是多么干净......或者为什么它的工作方式不同。但是我在网络上找到了一个例子,它使用了旧版本的android过时了......但它似乎有用。以下是我改变代码的方式,如果有人可以向我解释为什么这种方式不同我会喜欢这个解释:)

import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;




public class ContactListOne extends ListActivity {

    private static final int NEW_RULE_CONTACT=1;

    private SimpleCursorAdapter adapter;

    private RulesDbAdapter mDbHelper;
    private ListView wtf;




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_list_one);
        mDbHelper = new RulesDbAdapter(this);



        mDbHelper.open();

        // Obtain handles to UI objects



      Cursor cursor = getContacts();
        //Build adapter with contact info



        String[] fields = new String[] {

                ContactsContract.Contacts.DISPLAY_NAME
        };
        int[] to = new int[]{

                R.id.contactname
        };

        adapter = new SimpleCursorAdapter(this, R.layout.contact_list_one_rows, cursor, fields, to);

        setListAdapter(adapter);

        mDbHelper.close();
    }


    private Cursor getContacts()
    {
        // Run query
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME
        };
        String notInList = mDbHelper.createContactNotInList();



        String selection =  ContactsContract.Contacts.DISPLAY_NAME + " != '' AND " + ContactsContract.Contacts.LOOKUP_KEY + notInList ;


        String[] selectionArgs = null;
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";


        return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
    }



    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);


        Cursor cursor = (Cursor) adapter.getItem(position);
        String selection = cursor.getString(1);
        Toast.makeText(this, selection,Toast.LENGTH_LONG).show();   



    }



}

3 个答案:

答案 0 :(得分:0)

试试这个 -

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String selection = l.getItemAtPosition(position).toString();
Toast.makeText(this, selection, Toast.LENGTH_LONG).show();
}

答案 1 :(得分:0)

试试这个:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
     // TODO Auto-generated method stub
     super.onListItemClick(l, v, position, id);
     String keyword =  ((TextView)v.findViewById(R.id.contactname)).getText();
     Toast.makeText(this, "You selected: " + keyword,Toast.LENGTH_LONG).show();       } 

答案 2 :(得分:0)

什么是android显示器,你实际上是在打印整个光标行,而不仅仅是你感兴趣的列。而且,作为java中的大多数对象,默认情况下,此对象的字符串表示形式,即toString返回的内容,是JVM中对象的内存地址。

尝试使用光标的方法public void setStringConversionColumn (int stringConversionColumn)和参数1或2(我永远不会记住列是否与jdbc一样为0)。然后要获取此值的字符串,可以使用

CharSequence selection = convertToString(cursor);

- 更新

//somewhere in onCreate
cursor.setStringConversionColumn ( 0 or 1 );

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
  super.onListItemClick(l, v, position, id);
  //for that you will need to define cursor as a data member
  CharSequence selection = convertToString(cursor);
  Toast.makeText(this, selection, Toast.LENGTH_LONG).show();
}

此致  斯特凡