我如何使用与意图的ListView? [英] How do I use Intents with ListView?
问题描述
我知道有很多的解决方案来解决这个问题。
I know there are many solutions to solve this problem.
但它不是同一案件作为。
But it's not same case as that.
这是我的Java code。
This is my java code.
package com.progme.sejong_bus;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class BUSActivity extends Activity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bus);
String []data =
{"공통공지사항","101번 버스정보","102번 버스정보","103번 버스정보","104번 버스정보",
"105번 버스정보","106번 버스정보","107번 버스정보","108번 버스정보","109번 버스정보",
"110번 버스정보","111번 버스정보","112번 버스정보","113번 버스정보","114번 버스정보",
"115번 버스정보","116번 버스정보","117번 버스정보"};
listView = (ListView)findViewById(R.id.listview1);
ArrayAdapter<?> adapter = new ArrayAdapter<Object>(getApplicationContext(), android.R.layout.simple_list_item_1, data);
listView.setAdapter(adapter);
}
}
这是我的XML code
and this is my XML code
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#000000" >
<ListView
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="top"
android:layout_weight="1"
android:cacheColorHint="#000000"
android:dividerHeight="1dp"
android:divider="#FFFFFF" >
</ListView>
</LinearLayout>
我想意图每个列表..但我怎么能选择每一个列表?
I want intent to each list.. but how can i select each list?
和什么code,我必须使用?
And what code that i have to use?
编辑>
我要见我的应用程序正在这样的形象。
I want to see my app is working like this image.
每个列表链接的每一页。
Each list is link for each page.
和我不能这样做,因为我有code像这样的工作不知道。(?)
And I can't do this because i have no idea with code for to work like this.(?)
反正..我有什么做的?
Anyway.. what I have to do??
推荐答案
所以首先你需要使用一个数组来获取列表项在您当前的电流
字符串[]在你的主Java code =数据可以用这个来代替
SO first you need to use an array to get the list items where your current current string []data= in your main java code can be replaced with this
// Listview Data
String[] List_items = getResources().getStringArray(R.array.List_items);
创建值一个新的XML文件,并粘贴此code IV刚打电话名单Lise_items
只是无论你在你的列表视图喜欢更换(列表项)
create a new xml file in values and paste this code iv just called the list Lise_items just replace the (list item) with whatever you like in your list view
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="List_items">
<item>list item </item>
<item>list item</item>
<item>list item</item>
<item>list item</item>
<item>list item</item>
</string-array>
</resources>
通过添加的onclick监听器
by added the onclick listener
/// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
/// selected item
String List_items = ((TextView) view).getText().toString();
/// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), SingleListItem.class);
/// sending data to new activity
i.putExtra("List_items", List_items);
startActivity(i);
}
});
}
}
您将需要创建在单击查看
You will need to create another xml file and java file for the view on click
例如:
list_item_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/List_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dip"
android:textStyle="bold"
android:padding="10dip"
android:textColor="#ffffff"/>
</LinearLayout>
Listitemselected.java
package com.progme.sejong_bus;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Listitemselected extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.list_item_view);
TextView txtList_items = (TextView) findViewById(R.id.List_items);
Intent i = getIntent();
// getting attached intent data
String List_items = i.getStringExtra("List_items");
// displaying selected List name
txtList_items.setText(List_items);
}
}
您必须创建因为这是在这么当每个项目点击它会去例如:
You will have to create a new intent for each item that is in the list so when each item is clicked it will go to a new activity for example
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Intent a;
switch(position){
case 1:
a = new Intent("Items 1 page");
break;
case 2:
a = new Intent("items 2 page");
break;
if(null!=a)
startActivity(a);
}
}
这篇关于我如何使用与意图的ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!