简体   繁体   English

如何在Android的自定义列表视图中从TextView获取值

[英]How to get the value from TextView in a custom list view in android

I'm new to android development, and recently I wrote a code to show all saved contacts in an android device. 我是android开发的新手,最近我写了一段代码来显示android设备中所有已保存的联系人。 For this, I used a custom list view which has 2 text views (to display contact name, contact number). 为此,我使用了一个具有2个文本视图的自定义列表视图(以显示联系人姓名,联系人号码)。 It worked but now I want to get the contact name and the number (Toast message) when I click on list items. 它起作用了,但是现在我想在单击列表项时获得联系人姓名和电话号码(Toast消息)。 How to achieve this? 如何实现呢?

 // list item click listener
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(MainActivity.this, "Contact Name : " + contactList.get(i)  , Toast.LENGTH_SHORT).show();
            }
        });

This is the code I wrote in MainActivity. 这是我在MainActivity中编写的代码。

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    ListView list;
    public String Contactname;
    public String Contactnumber;


    // array list
    final ArrayList<Contacts> contactList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        fetchContact(); // call fetch contact method

        // show number of contacts - toast message (testing purpose)
        int count = contactList.size();
        Toast.makeText(this, "Count : " + count , Toast.LENGTH_SHORT).show();


    }

    public void fetchContact()
    {
        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String[] projection ={ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER};
        String selection = null;
        String[]selectionArgs = null;
        String sortOrder =null;

        ContentResolver resolver =getContentResolver();
        Cursor cursor =resolver.query(uri,projection,selection,selectionArgs,sortOrder);

        while(cursor.moveToNext()){
          Contactname =cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
          Contactnumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            Contacts contacts = new Contacts(Contactname,Contactnumber);

            contactList.add(contacts);
        }


        contactAdapter adapter = new contactAdapter(MainActivity.this,R.layout.activity_list_item,contactList);
        list.setAdapter(adapter);

        // list item click listner
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(MainActivity.this, "Contact Name : " + contactList.get(i)  , Toast.LENGTH_SHORT).show();
            }
        });
    }


}

This is the Class I used to save Contact name, number., I stored class to an array object 这是我用来保存联系人姓名,电话号码的类。我将类存储到数组对象中

    public class Contacts {
        // properties
        public String name;
        public String number;

        public Contacts(String name, String number) {
            this.name = name;
            this.number = number;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getNumber() {
            return number;
        }

        public void setNumber(String number) {
            this.number = number;
        }
    }

You can fetch and data from textview Using Interface. 您可以使用Interface从textview获取数据。 For that You have to do Following Steps. 为此,您必须执行以下步骤。

Step1: Create Interface in your contactAdapter.class File.TO create Interface You Can write. 第一步:在你的contactAdapter.class File.TO 创建界面中创建界面,您可以写。

public interface contactClickListener{
         void onContactClick(List<contact> currentContact);

} }

Step2: Then after Create an Object of Interface using below statment. 步骤2:然后在使用以下语句创建接口对象之后。

contactClickListener listener;

Step3: In contactAdapter OnBindMethod First fetch Current ContactInfo Using Below Statment. 步骤3:在contactAdapter中, OnBindMethod首先使用以下语句获取当前ContactInfo

Contacts currentContact=contactList.get(position);

Step 4: Then after On Contact adapater Implement OnClick event for Textview and inside onclick Event call your interface. 步骤4:然后在On Contact适配器上为Textview实施OnClick事件并在onclick事件内部调用界面。

listener.onContactClick(currentContact); listener.onContactClick(currentContact);

Step 5: Currently Your have three parameter in contactAdapter so now add One Parameter in contactAdapter class. 步骤5:当前,您在contactAdapter中有三个参数,因此现在在contactAdapter类中添加一个参数。

       contactAdapater(Existingparameter1 p1,Existingparameter2 p2,Existingparameter3 p3,contactClickListener listener){
this.listener=listener;
}

Step6: Then after Goto Your activity When You Create and call Contact Adapter. 第6步:然后在“创建和调用联系适配器时转到您的活动”之后。 Implement Contact adapter method their using below Statement. 使用下面的语句实现联系人适配器方法。

public class MainActivity extends AppCompatActivity implements contactadapter.OnContactClickListener{

} }

Step 7: Then after change Contactadapter in activity to implement interface. 步骤7:然后在活动中更改Contactadapter以实现接口。

contactAdapter adapter = new contactAdapter(MainActivity.this,R.layout.activity_list_item,contactList);
    list.setAdapter(adapter);

Step8: OncontactClick method is genrated in activity where You can get all details ov textview and your contacts model class. 步骤8: OncontactClick方法是在活动中生成的,您可以在其中获取textview和联系人模型类的所有详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android 如何从列表视图中获取 textview 字符串值 - Android how can i get the textview string value from list view 如何从视图中获取 TextView 和按钮列表 - How to get the TextView and Button list from a view 如何使用自定义适配器在Android列表视图中添加ImageView / TextView? - How to add ImageView/TextView in a Android List view with custom adapter? 如何从Android的父视图中的自定义子视图中获取价值 - how to get value from custom child view in parent view in android 如何从按钮上的列表视图自定义适配器获取所有textview数据 - How to get All textview data from List View Custom Adapter on Button Click 从textview上的列表视图中获取Selected项目,单击android - Get the Selected item from a list view on textview click in android Android如何从自定义创建的视图中获取TextView参考ID? - Android how to get a TextView reference id from within a custom created view? 如何从Android中的列表视图中的listView项隐藏textView - How to Hide textView From listView item from list view in android 如何从自定义列表视图获取组合框的价值 - How to get value of combobox from custom list view 如何使用TextView显示从列表视图中选择的项目的值 - How to display the value of an item selected from a list view using textview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM