Android,自定义 ListAdapter 获取 TextView-Text
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1724937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android, Custom ListAdapter get TextView-Text
提问by Ripei
I coded an own Adapter and added it to my ListActivity via an ListView. The reason why I wrote an own Adapter is, that i had to make some layout changes to the list-entrys. In every entry of the list i've got 3 TextViews.
我编写了一个自己的适配器并通过 ListView 将它添加到我的 ListActivity。我编写自己的适配器的原因是,我必须对列表条目进行一些布局更改。在列表的每个条目中,我都有 3 个 TextView。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<TextView
android:id="@+id/myNr"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_marginRight="15dip"
android:text="id"
android:textSize="25dip"
android:background="#333333"
android:gravity="center_horizontal"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="@+id/editor"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Editor: " />
<TextView
android:id="@+id/date"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Date: " />
</LinearLayout>
The next thing i did, was to implement a "onListItemClick-Methode". Afterwards i implemented a onListItemLongClick - Listener with the following code:
我做的下一件事是实现“onListItemClick-Methode”。之后,我使用以下代码实现了一个 onListItemLongClick - 侦听器:
in onCreate of the Activity i added:
在我添加的 Activity 的 onCreate 中:
registerForContextMenu(getListView());
then i added the following methode:
然后我添加了以下方法:
Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo){
AdapterView.AdapterContextMenuInfo info;
info = (AdapterView.AdapterContextMenuInfo) menuInfo;
long id = getListAdapter().getItemId(info.position);
}
where "id" is the index of the item in the list. I now want to get the Text of the texview with the id="myNr of this ListItem. Is there any way get this Text?
其中“id”是列表中项目的索引。我现在想用这个 ListItem 的 id="myNr 来获取 texview 的文本。有什么办法可以得到这个文本?
采纳答案by Ripei
Hey Guys I found another way to set an ItemLongClickListener. Therefore i also found a way to get the Text i am displaying.
嘿伙计们,我找到了另一种设置 ItemLongClickListener 的方法。因此,我还找到了一种获取正在显示的文本的方法。
ListView lv = getListView();
lv.setOnItemLongClickListener(new OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int row, long arg3) {
String[] tmp = (String[]) arg0.getItemAtPosition(row);
//tmp[0] ist the Text of the first TextView displayed by the clicked ListItem
return true;
}});
回答by dxh
view
is the list item, so you can do
view
是列表项,所以你可以做
String s =(String) ((TextView) view.findViewById(R.id.myNr)).getText();
回答by Subhajit banerjee
just take reference of of the text view if you extending a BaseAdapterlike
只取文本视图的参考,如果你伸出的BaseAdapter像
Listener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
TextView tx =(TextView)view.findViewById(R.id.text);
String s = tx.getText().toString();
Log.d(TAG, "string : "+s);
回答by Nik NexAndrX
I found none of the above solutions to work. As an alternativeyou can:
我发现上述解决方案都不起作用。作为替代方案,您可以:
getListView().setOnItemLongClickListener(new OnItemLongClickListener()
{
public boolean onItemLongClick(AdapterView<?> arg0, View v,int position, long id)
{
Cursor c = (Cursor) arg0.getItemAtPosition(position);
String tableValue = c.getString(1);
return true;
}
});
回答by Malek Hijazi
The best solution I found was :
我发现的最佳解决方案是:
@Override
public void onCreateContextMenu(ContextMenu menu, View view,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, view, menuInfo);
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
ViewGroup vg = (ViewGroup) view;
View children = vg.getChildAt(info.position);
TextView child = (TextView) children.findViewById(R.id.text1);
String s= child.getText().toString();
menu.setHeaderTitle(s);
menu.add(0, view.getId(), 0, "Serve");
menu.add(0, view.getId(), 0, "View");
}
回答by Eric Mill
If you're looking for the TextView at the position of the item the person long-pressed, you can get it out using something like this:
如果您要在用户长按的项目位置寻找 TextView,您可以使用以下方法将其取出:
String s = ((TextView) view.getItemAtPosition(info.position)).getText();
回答by Ripei
thanks for your help but I am afraid it still doesnt work. if i add the line you suggested, it underlines "getItemAtPosition" and says: --The method getItemAtPosition(int) is undefined for the type View--
感谢您的帮助,但恐怕它仍然不起作用。如果我添加您建议的行,它会在“getItemAtPosition”下划线并说:--对于视图类型,方法 getItemAtPosition(int) 是未定义的--
He suggests to cast the view to a AdapterView, so it looks the following:
他建议将视图转换为 AdapterView,因此它看起来如下所示:
String s = ((TextView) ((AdapterView<ListAdapter>) view).getItemAtPosition(info.position)).getText();
When i run this and make a long click on the item, i recieve an "ClassCastException"
当我运行它并长按该项目时,我收到“ClassCastException”
回答by Ripei
Yes i also thought so. But when i implement this i just get the Text of the first TextView in the List. That means I am able to get the text of the desired TextView but it is always the text of the first list-entry. Isn't there an option to adress the longClickedList-entry?
是的,我也是这么认为的。但是当我实现这个时,我只得到列表中第一个 TextView 的文本。这意味着我能够获取所需 TextView 的文本,但它始终是第一个列表条目的文本。没有解决 longClickedList 条目的选项吗?
回答by Vansi
In this method you will get the item related to your list item.
在此方法中,您将获得与列表项相关的项。
@Override
public boolean onContextItemSelected(MenuItem aItem) {
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) aItem.getMenuInfo();
Object entity = (Object) todoList.getAdapter().getItem(menuInfo.position);
switch (aItem.getItemId()) {}
}
回答by Girish
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String title = (String) ((TextView)view.findViewById(R.id.title)).getText();
String artist=(String)((TextView)view.findViewById(R.id.artist)).getText();
//System.out.println(s);
Toast.makeText(CustomizedListView.this,"Title = "+title+"\n"+"Artist = "+artist, Toast.LENGTH_SHORT).show();
}
});