gpt4 book ai didi

android - 如何根据 ListView 上的文本从自定义 ListView 打开不同的布局?

转载 作者:行者123 更新时间:2023-11-30 01:21:19 24 4
gpt4 key购买 nike

enter image description here 我有一个自定义 ListView ,它的 onItemClick 方法在下面

if (bean.getResType().equals("Music"))
{
ListView listView;
View view = getView();
if (view != null) {
listView = (ListView) view.findViewById(R.id.list4);


CustomListViewAdapterMindMaps adapter = new CustomListViewAdapter(getContext(),
R.layout.list, mList);
listView.setAdapter(adapter);


listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {


Intent intent = new Intent(getContext(), Main_Activity_For_Music.class);


//start the second activity
startActivity(intent);

}


});
}else if (bean.getResType().equals("Video")) {

ListView listView2;
View view2 = getView();
if (view2 != null) {

listView2 = (ListView) view.findViewById(R.id.list4);


CustomListViewAdapter adapter = new CustomListViewAdapter(getContext(),
R.layout.list, mList);
listView2.setAdapter(adapter);


listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {




Intent intent = new Intent(getActivity(), Main_Activity_For_Video.class);

startActivity(intent);

}


});

}

在此 ListView 中,我显示了不同类型的列表项,例如音乐和视频。因此,如果用户按下音乐项目,它会打开一个新布局,如果选择视频,它会打开视频布局。但无论我选择什么,它总是会打开第一个布局,例如音乐布局。

最佳答案

String resource;
resource = bean.getResType();

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

if(resource.equals("Music"){
Intent intent1 = new Intent(getContext(), Main_Activity_For_Music.class);


//start the second activity
startActivity(intent1);
}else if(resource.equals("Video")
{
Intent intent2 = new Intent(getActivity(), Main_Activity_For_Video.class);

startActivity(intent2);
}

}

或者你也可以使用 switch case

switch(bean.getResType()){
case "Music":
Intent intent1 = new Intent(getContext(), Main_Activity_For_Music.class);


//start the second activity
startActivity(intent1);
break;
case "Video":
Intent intent2 = new Intent(getContext(), Main_Activity_For_Video.class);


//start the second activity
startActivity(intent1);
break;

}

关于android - 如何根据 ListView 上的文本从自定义 ListView 打开不同的布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37071240/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com