如何从listview android的主列表中获取所选项目的文本
Posted
技术标签:
【中文标题】如何从listview android的主列表中获取所选项目的文本【英文标题】:How to get text of selected item from the main list in listview android 【发布时间】:2016-01-31 14:37:39 【问题描述】:在这种情况下,当我长按列表上下文菜单中的任何名称时,它会再次弹出并要求选择其中一个选项。
我想要的是生成在主列表视图中选择的名称的祝酒词。附上图片以便更好地理解。例如,当我按下 David Hicks 博士时,应该会出现他的名字。
Image1 Image2 有人可以帮我解决这个问题吗
package com.example.tamukcompscfaculty;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ArrayAdapter;
public class MainActivity extends ListActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] Professors = getResources().getStringArray(R.array.Professors);
ArrayAdapter<String> ProgAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Professors);
setListAdapter(ProgAdapter);
registerForContextMenu(getListView());
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
if(v==getListView())
MenuInflater menuInflater=getMenuInflater();
menu.setHeaderTitle(R.string.More);
menuInflater.inflate(R.menu.details, menu);
super.onCreateContextMenu(menu, v, menuInfo);
Xml 文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.tamukcompscfaculty.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_
android:layout_
android:layout_alignLeft="@+id/profs"
android:layout_alignParentTop="true"
android:layout_marginTop="33dp"
android:text="@string/FacultyText"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold|italic" />
<ListView
android:id="@android:id/list"
android:layout_
android:layout_
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginLeft="-25dp" >
</ListView>
<ImageView
android:id="@+id/imageView1"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/textView1"
android:scaleType="fitXY"
android:src="@drawable/engineering" />
</RelativeLayout>
item_data.xml 是 Values 文件夹中的一个文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name = "Professors">
<item >Dr. David Hicks</item>
<item >Dr. Mohammad S. Khan</item>
<item >Dr. Young Lee</item>
<item >Dr. Mais Nijim</item>
<item >Dr. Ashraf Yaseen</item>
<item >Ms. Jeong Yang</item>
<item >Dr. Abdelrahman Elleithy</item>
<item >Dr. Wei-Da Hao</item>
<item >Dr. Syed Iqbal Omar</item>
<item >Dr. Muhammad Aurangzeb</item>
<item >Dr. Gahangir Hossain</item>
</string-array>
</resources>
字符串.Xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TamukCompScFaculty</string>
<string name="action_settings">Settings</string>
<string name="FacultyText"><u>Faculty Office Hours Details</u></string>
<string name="Office_Hours">Check Office Hours</string>
<string name="Email">Email Id</string>
<string name="Phonenumber">Phone Number</string>
<string name="More"><u>Click below to check the details</u></string>
<string name="title_activity_start">StartActivity</string>
<string name="hello_world">Hello world!</string>
</resources>
【问题讨论】:
"附上图片便于理解"在哪里? 【参考方案1】:首先,创建您的Professors
成员变量,然后覆盖onListItemClick(ListView l, View v, int position, long id)
,您可以在其中获取单击列表项的位置。所以你的代码将是:
public class MainActivity extends ListActivity
private String[] Professors;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Professors = getResources().getStringArray(R.array.Professors);
ArrayAdapter<String> ProgAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Professors);
setListAdapter(ProgAdapter);
registerForContextMenu(getListView());
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
if(v==getListView())
MenuInflater menuInflater=getMenuInflater();
menu.setHeaderTitle(R.string.More);
menuInflater.inflate(R.menu.details, menu);
//get the position of row clicked
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
int position = info.position;
Toast.makeText(MainActivity.this, Professors[position], Toast.LENGTH_SHORT).show();
super.onCreateContextMenu(menu, v, menuInfo);
【讨论】:
@Ajay 在 onCreated 方法中添加这一行:getListView().setOnItemClickListener(this);我会更新我的答案。 @Ajay 查看更新后的答案。这应该适合你。 @Ajay 如果有帮助,请随时接受我的回答。 :)【参考方案2】:你应该实现 OnItemClickListener 然后重写方法 onItemClick 并且不要忘记在你的 OnCreated 方法中注册监听器。
public class MainActivity extends ListActivity implements OnItemClickListener
private String[] Professors;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Professors = getResources().getStringArray(R.array.Professors);
ArrayAdapter<String> ProgAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, Professors);
setListAdapter(ProgAdapter);
getListView().setOnItemClickListener(this)
registerForContextMenu(getListView());
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
if(v==getListView())
MenuInflater menuInflater=getMenuInflater();
menu.setHeaderTitle(R.string.More);
menuInflater.inflate(R.menu.details, menu);
super.onCreateContextMenu(menu, v, menuInfo);
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
super.onListItemClick(l, v, position, id);
Toast.makeText(MainActivity.this, Professors[position], Toast.LENGTH_SHORT).show();
【讨论】:
【参考方案3】:试试这个:
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] Professors = getResources().getStringArray(R.array.Professors);
ArrayAdapter<String> ProgAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Professors);
setListAdapter(ProgAdapter);
registerForContextMenu(getListView());
getListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
Toast.makeText(MainActivity.this, Professors[position], Toast.LENGTH_SHORT).show();
);
【讨论】:
以上是关于如何从listview android的主列表中获取所选项目的文本的主要内容,如果未能解决你的问题,请参考以下文章
我如何从firebase检索数据到android listview顶部[重复]
如何使用 listview 项目 android 对 listview 进行排序?
当您在 android listview 中滑动任何列表项时想要调用