我有一个包含不同 xml 布局的 ListView ,每个布局都有不同的 View 。我想知道如何在 onItemClick 方法内从用户点击的项目内的 View 中获取数据。
我的 ListView 由多个布局组成,因此在调用 onItemClick 方法时我无法确定按下了哪个布局。例如, ListView 中布局中的一项是:
// list_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/lblSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:padding="6dp"
android:layout_marginRight="4dp"
android:text=""
android:textColor="#FFFFFF"
android:textSize="18sp" />
<TextView
android:id="@+id/lblMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="6dp"
android:layout_marginLeft="4dp"
android:text=""
android:textColor="#FFFFFF"
android:textSize="18sp" />
</RelativeLayout>
ListView 中的另一个布局可能是:
// list_item_name.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/lblName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="4dp"
android:padding="4dp"
android:text="TextView"
android:textColor="#FFFFFF"
android:textSize="28sp" />
</RelativeLayout>
所以我不知道如何为 ListView 中的任何布局获取所有这些 View 的数据。
我之前的尝试是这样的:
public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
Object obj = adapter.getItem(position);
String value = obj.toString();
}
但是 value 在这种情况下,返回一个包含该项目内所有 View 的所有值的字符串,例如:{caption=MobileNumber, value=07000123456} < 我不能对这个字符串做任何事情,除非我尝试使用="">substring 将它分开,考虑到这些值可能是由用户创建的,这充其量是不可靠的。
这是我正在使用的适配器(感谢 Jeff Sharkey ,我没有创建这个适配器):
public class SeparatedListAdapter extends BaseAdapter {
public final Map<String,Adapter> sections = new LinkedHashMap<String,Adapter>();
public final ArrayAdapter<String> headers;
public final static int TYPE_SECTION_HEADER = 0;
public SeparatedListAdapter(Context context) {
headers = new ArrayAdapter<String>(context, R.layout.list_header);
}
public void addSection(String section, Adapter adapter) {
this.headers.add(section);
this.sections.put(section, adapter);
}
public void addSectionNoHeader(String section, Adapter adapter) {
this.sections.put(section, adapter);
}
public Object getItem(int position) {
for(Object section : this.sections.keySet()) {
Adapter adapter = sections.get(section);
int size = adapter.getCount() + 1;
// check if position inside this section
if(position == 0) return section;
if(position < size) return adapter.getItem(position - 1);
// otherwise jump into next section
position -= size;
}
return null;
}
public int getCount() {
// total together all sections, plus one for each section header
int total = 0;
for(Adapter adapter : this.sections.values())
total += adapter.getCount() + 1;
return total;
}
public int getViewTypeCount() {
// assume that headers count as one, then total all sections
int total = 1;
for(Adapter adapter : this.sections.values())
total += adapter.getViewTypeCount();
return total;
}
public int getItemViewType(int position) {
int type = 1;
for(Object section : this.sections.keySet()) {
Adapter adapter = sections.get(section);
int size = adapter.getCount() + 1;
// check if position inside this section
if(position == 0) return TYPE_SECTION_HEADER;
if(position < size) return type + adapter.getItemViewType(position - 1);
// otherwise jump into next section
position -= size;
type += adapter.getViewTypeCount();
}
return -1;
}
public boolean areAllItemsSelectable() {
return false;
}
public boolean isEnabled(int position) {
return (getItemViewType(position) != TYPE_SECTION_HEADER);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int sectionnum = 0;
for(Object section : this.sections.keySet()) {
Adapter adapter = sections.get(section);
int size = adapter.getCount() + 1;
// check if position inside this section
if(position == 0) return headers.getView(sectionnum, convertView, parent);
if(position < size) return adapter.getView(position - 1, convertView, parent);
// otherwise jump into next section
position -= size;
sectionnum++;
}
return null;
}
@Override
public long getItemId(int position) {
return position;
}
}
我正在寻找一种方法来提取项目内所有 View 的值。但被点击的项目可能是任何布局之一这一事实让我感到困惑。有什么想法吗?
这是我如何在代码中使用此适配器的示例:
SeparatedListAdapter adapter;
Customer cust;
ListView lvDetails;
public Map<String,?> createDoubleItem(String title, String caption) {
Map<String,String> item = new HashMap<String,String>();
item.put(ITEM_VALUE, title);
item.put(ITEM_CAPTION, caption);
return item;
}
public Map<String,?> createSingleItem(String name) {
Map<String,String> item = new HashMap<String,String>();
item.put(ITEM_VALUE, name);
return item;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_customer);
Intent intent = getIntent();
cust = Db.Functions.getCustomer(intent.getStringExtra("uuid"));
lvDetails = (ListView) findViewById(R.id.custdetails);
adapter = new SeparatedListAdapter(this);
lvDetails.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
Gen.popup("Item clicked");
}
});
// Name
List<Map<String,?>> name = new LinkedList<Map<String,?>>();
if (cust.firstName == null && cust.lastName == null)
name.add(createSingleItem("Unnamed Customer"));
else
name.add(createSingleItem(
((cust.title != null) ? cust.title + " " : "") +
((cust.firstName != null) ? cust.firstName + " " : "") +
((cust.lastName != null) ? cust.lastName : "")));
adapter.addSection("Name", new SimpleAdapter(this, name, R.layout.list_item_name,
new String[] { ITEM_VALUE },
new int[] { R.id.lblName }));
// Phones
if ((cust.mobileNumber != null || cust.homeNumber != null || cust.workNumber != null) && SHOW_PHONES)
{
List<Map<String,?>> phone = new LinkedList<Map<String,?>>();
if (cust.mobileNumber != null)
phone.add(createDoubleItem(cust.mobileNumber, "Mobile"));
if (cust.homeNumber != null)
phone.add(createDoubleItem(cust.homeNumber, "Home"));
if (cust.workNumber != null)
phone.add(createDoubleItem(cust.workNumber, "Work"));
adapter.addSection("Phone", new SimpleAdapter(this, phone, R.layout.list_item,
new String[] { ITEM_VALUE, ITEM_CAPTION },
new int[] { R.id.lblSub, R.id.lblMain }));
}
lvDetails.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
最佳答案
这是一个简单的方法 - 将其粘贴到每个 xml View 中:
android:onClick="someFunction"
然后在这个 someFunction 中做:
public void someFunction(View v){
switch (v.getId()){
case R.id.lblSub:
// This layout was clicked, do something about it
break;
case R.id.lblMain:
// This layout was clicked, do something about it
break;
}
关于android - 当列表中有不同的布局时从列表项中检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18125984/
是否有类似“RVMuse1”或“RVMuselist[0]”之类的内容而不是键入整个版本号。在任何时候,我们都会看到一个可能包含5个或更多ruby的列表,我们可以轻松地键入一个数字而不是X.X.X。这也有助于rvmgemset。 最佳答案 这在RVM2.0中是可能的=>https://docs.google.com/document/d/1xW9GeEpLOWPcddDg_hOPvK4oeLxJmU3Q5FiCNT7nTAc/edit?usp=sharing-知道链接的任何人都可以发表评论
这可能是个愚蠢的问题。但是,我是一个新手......你怎么能在交互式rubyshell中有多行代码?好像你只能有一条长线。按回车键运行代码。无论如何我可以在不运行代码的情况下跳到下一行吗?再次抱歉,如果这是一个愚蠢的问题。谢谢。 最佳答案 这是一个例子:2.1.2:053>a=1=>12.1.2:054>b=2=>22.1.2:055>a+b=>32.1.2:056>ifa>b#Thecode‘if..."startsthedefinitionoftheconditionalstatement.2.1.2:057?>puts"f
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co
假设我在Ruby中有这个each循环。@list.each{|i|putsiifi>10breakend}我想循环遍历列表直到满足条件。这让我感到“不像Ruby”,因为我是Ruby的新手,是否有Ruby方法可以做到这一点? 最佳答案 您可以使用Enumerable#detect或Enumerable#take_while,取决于您想要的结果。@list.detect{|i|putsii>10}#Returnsthefirstelementgreaterthan10,ornil.正如其他人所指出的,更好的风格是先进行子选择,然后再对其
是否可以为特定(或所有)项目使用多个布局?例如,我有几个项目,我想对其应用两种不同的布局。一个是绿色的,一个是蓝色的(但是)。我想将它们编译到我的输出目录中的两个不同文件夹中(例如v1和v2)。我一直在玩弄规则和编译block,但我不知道这是怎么回事。因为,每个项目在编译过程中只编译一次,我不能告诉nanoc第一次用layout1编译,第二次用layout2编译。我试过这样的东西,但它导致输出文件损坏。compile'*'doifitem.binary?#don’tfilterbinaryitemselsefilter:erblayout'layout1'layout'layout2'
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路
我基本上来自Java背景并且努力理解Ruby中的模运算。(5%3)(-5%3)(5%-3)(-5%-3)Java中的上述操作产生,2个-22个-2但在Ruby中,相同的表达式会产生21个-1-2.Ruby在逻辑上有多擅长这个?模块操作在Ruby中是如何实现的?如果将同一个操作定义为一个web服务,两个服务如何匹配逻辑。 最佳答案 在Java中,模运算的结果与被除数的符号相同。在Ruby中,它与除数的符号相同。remainder()在Ruby中与被除数的符号相同。您可能还想引用modulooperation.
RSpec似乎按顺序匹配方法接收的消息。我不确定如何使以下代码工作:allow(a).toreceive(:f)expect(a).toreceive(:f).with(2)a.f(1)a.f(2)a.f(3)我问的原因是a.f的一些调用是由我的代码的上层控制的,所以我不能对这些方法调用添加期望。 最佳答案 RSpecspy是测试这种情况的一种方式。要监视一个方法,用allowstub,除了方法名称之外没有任何约束,调用该方法,然后expect确切的方法调用。例如:allow(a).toreceive(:f)a.f(2)a.f(1)
我目前正在尝试学习RubyonRails和测试框架RSpec。assigns在此RSpec测试中做什么?describe"GETindex"doit"assignsallmymodelas@mymodel"domymodel=Factory(:mymodel)get:indexassigns(:mymodels).shouldeq([mymodel])endend 最佳答案 assigns只是检查您在Controller中设置的实例变量的值。这里检查@mymodels。 关于ruby-o