[英]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.