我在strings.xml
中有列表项或一系列项目。
我想在单击某个项目时启动Activity
但我的数组列表在strings.xml
中定义。请帮忙。
让我们说第一项打开ActivityItem1
,依此类推......
这是我的XML文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="65dp"
android:hint="@string/search"
android:id="@+id/txtsearch"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView">
</ListView>
</LinearLayout>
这个My Strings.xml
<resources>
<array name="allduas">
<item>Item Number 1</item>
<item>Item Number 2</item>
<item>Item Number 3</item>
<item>Item Number 4</item>
<item>Item Number 5</item>
<item>Item Number 6</item>
<item>Item Number 7</item>
<item>Item Number 8</item>
<item>Item Number 9</item>
<item>Item Number 10</item>
<item>Item Number 10</item>
<item>Item Number 12</item>
<item>Item Number 13</item>
<item>Item Number 14</item>
<item>Item Number 15</item>
<item>Item Number 16</item>
<item>Item Number 17</item>
<item>Item Number 18</item>
<item>Item Number 19</item>
<item>Item Number 20</item>
<item>Item Number 21</item>
<item>Item Number 22</item>
<item>Item Number 23</item>
<item>Item Number 24</item>
<item>Item Number 25</item>
<item>Item Number 26</item>
<item>Item Number 27</item>
<item>Item Number 28</item>
<item>Item Number 29</item>
<item>Item Number 30</item>
</array>
</resources>
这是我的java文件
public class AllDuasActivity extends Activity {
public ListView listView;
public String[] items;
public ArrayAdapter<String> adapter;
public AutoCompleteTextView ac;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.allduas);
ac = (AutoCompleteTextView)findViewById(R.id.txtsearch);
items = getResources().getStringArray(R.array.allduas);
listView = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, items);
listView.setAdapter(adapter);
ac.setAdapter(adapter);
}
}
答案 0 :(得分:0)
lv.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
if(position==1)
startActivity(this,Activity1.class);
}
});
试试这个。
答案 1 :(得分:0)
使用ListView
组件非常容易。只需添加OnItemClickListener
,如下所示:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(context, DetailsActviity.class);
intent.putExtra("position", position)
context.startActivity(intent)
}
});
答案 2 :(得分:0)
Class<?> cls = Class.forName(className);
例如,从您的xml文件中获取String className,但它应该是完整的链接,例如com.myapp.MyClass。
然后使用此类名称启动Activity。
答案 3 :(得分:0)
public void onItemClickListener() {
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = null;
switch (position) {
case POSITION_ONE:
intent = new Intent(context, ActivityOne.class);
break;
case POSITION_TWO:
intent = new Intent(context, ActivityTwo.class);
break;
case POSITION_THREE:
intent = new Intent(context, ActivityThree.class);
break;
}
context.startActivity(intent)
}
}
});
}
您可以拥有与活动一样多的切换案例。