如何使ListView可单击并在单击后显示一些文本? [英] How do I make the ListView clickable and show some text once it gets clicked?
问题描述
我正在练习ListView,并且已在一个活动中放置了一些列表项.现在,我有3项清单(三行).我希望一旦用户单击其中一个行(现在是我的第一行),他/他应该能够看到一些有关此行的详细信息.该文本将显示在活动底部的ListView之外的某个地方,我该怎么做? 像这样:
I am practicing ListView and I have placed a few List Items on an activity. Right now I have 3 items of list (in three rows). I want that once a user click one of the row (let's say it's my first row for now) s/he should be able to see some details text about it. That text will be displayed somewhere at at the bottom of the activity , outside the ListView.How could I do that? Like this:
我正在处理此代码:
MainActivity.java:
MainActivity.java:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] list = {"About Company", "Home Page", "Contact"};
Adapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
ListView listView = (ListView) findViewById(R.id.listId);
listView.setAdapter((ListAdapter) adapter);
listView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String listPosition = String.valueOf(parent.getItemAtPosition(position));
/*after clicking the first row of the list show the following info :
NAME: Some name
ADDRESS: Somewhere
*/
}
}
);
}
}
activity_main.xml:
activity_main.xml:
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listId"
android:background="#f79727"
android:textFilterEnabled="false"
android:layout_marginBottom="300dp" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignBottom="@+id/listId"
android:layout_centerHorizontal="true"
android:layout_marginBottom="66dp" />
推荐答案
我认为您需要良好的代码工作示例和教程.很好的链接是[将ArrayAdapter与ListView一起使用] [1]
I think you need good code working samples and a tutorial. A good link is [Using an ArrayAdapter with ListView][1]
[1]: https://github.com/codepath/android_guides/wiki/使用-ArrayAdapter-with-ListView . 您要求填充 Listview ,查看扩展 ArrayAdapter 的代码. 由于要在底部写文本,因此可以在res/layout/ item_user.xml 中的 LinearLayout 之外创建TextView.那是一种简单的方法.尝试一些代码,看看会得到什么.您必须中途参加,因为您可以在这里学到很多东西.
[1]: https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView . You asked to populate the Listview, look at the code extending the ArrayAdapter. Since you want to write text on the bottom, you can create a TextView outside the LinearLayout in the res/layout/item_user.xml. That is one simple way. Try some code and see what you get. You'll have to participate half way since you can learn a lot here.
这篇关于如何使ListView可单击并在单击后显示一些文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!