【发布时间】:2014-04-11 16:22:00
【问题描述】:
我有 1 个列表视图,5 行,所以当我运行代码时,我得到了 5 个 item_Titel、5 个 item_Datum、5 个 item_Intro 和 5 个 item_Button。但我不知道如何找到 R.Id。从每个项目,每一行。当您使用松散的文本视图和按钮时,您可以在 XML 中调用它,例如:@id/"clicked_button1"、@id/"clicked_button2'、@id/"clicked_button3" 等。
然后我可以使用开关盒并填充它, 示例:
public class OnClick implements OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.clicked_Button1:
Url = 1;
break;
case R.id.clicked_Button2:
Url = 2;
break;
case R.id.clicked_Button2:
Url = 3;
break;
但是现在我使用的是列表视图.. 但是我怎样才能找出哪个 R.id."item_Button?!!!" 我现在需要用吗?
我希望我的问题/解释现在更清楚一点! ;-)
谢谢你帮助我!
添加了 XML item_view:
<?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/item_Titel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:textColor="#FFFFFF"
android:textSize="20dp" />
<TextView
android:id="@+id/item_Datum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/item_Titel"
android:layout_marginBottom="5dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:textColor="#FFFFFF"
android:textSize="13dp" />
<TextView
android:id="@+id/item_Intro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/item_Datum"
android:layout_marginBottom="5dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:textColor="#FFFFFF"
android:textSize="15dp" />
<Button
android:id="@+id/item_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/item_Intro"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:layout_marginBottom="10dp"
android:layout_marginRight="3dp"
android:background="@drawable/mybutton"
android:padding="9dp"
android:textColor="#FFFFFF"
android:textSize="10dp" />
</RelativeLayout>
添加了 XML activity_main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="@drawable/tmx_background_portrait"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/TVLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_marginBottom="5dp"
android:textSize="20dp"
android:textColor="#FFFFFF" />
<ListView
android:id="@+id/PostListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none" >
</ListView>
</RelativeLayout>
这是我的 MainActivity 的一部分,我尝试读取和控制我的按钮:
public class OnClick implements OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.item_Button???!!!:
Url = 1;
break;
/*
case R.id.item_Button???!!!:
Url = 2;
break;
case R.id.item_Button???!!!:
Url = 3;
break;
*/
}
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
}
这里是 ListAdapter 部分和 getView 部分:
private class MyListAdapter extends ArrayAdapter<Post> {
public MyListAdapter() {
super(MainActivity.this, R.layout.item_view, myPost);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (itemView == null) {
itemView = getLayoutInflater().inflate(R.layout.item_view,
parent, false);
}
Post currentPost = myPost.get(position);
// Titel:
TextView TitelText = (TextView)
itemView.findViewById(R.id.item_Titel);
TitelText.setText(Html.fromHtml(currentPost.getTitel()));
// Datum:
TextView DatumText = (TextView)
itemView.findViewById(R.id.item_Datum);
DatumText.setText(Html.fromHtml(currentPost.getDatum()));
// Intro:
TextView IntroText = (TextView)
itemView.findViewById(R.id.item_Intro);
IntroText.setText(Html.fromHtml(currentPost.getIntro()));
// Button:
Button ButtonNumber = (Button)
itemView.findViewById(R.id.item_Button);
ButtonNumber.setText(Html.fromHtml(currentPost.getButton()));
ButtonNumber.setOnClickListener(new OnClick());
return itemView;
}
}
【问题讨论】:
-
为什么ID是随机的?它们没有在您的 XML 布局中定义吗?如果它们是在您的 java 中创建的,则在创建它们时保留对它们的引用。如果您的问题是关于查找名为 OnClick 的按钮(您已将相同的 OnClickListener 分配给许多按钮),那么您可以使用作为参数传入的“视图”并将其转换回按钮。我不太确定这个问题在问什么,但这可能会有所帮助。
-
你有 5 个 Listview 项目,每个项目都包含一个 Button,还是每个 ListView 项目都包含 5 个 Button?
-
把你的XML和一些代码贴出来就很容易理解了。
-
你需要有一个自定义的适配器类,你可以在你的按钮中设置
onClickListener。看看this例子 -
您应该创建一个项目类和一个自定义视图充气机。看这里:link
标签: java android xml listview button