gpt4 book ai didi

java - 我正在尝试使用 ListView 按钮从 cutomlistadapter 打开 Android Activity 。我尽了最大努力但没有结果?

转载 作者:行者123 更新时间:2023-12-02 02:31:13 25 4
gpt4 key购买 nike

我正在尝试使用 ListView 按钮打开 Activity 。此代码没有错误,但 Activity 未打开。我尽可能在谷歌上进行搜索,但没有任何帮助。

这里是MainActivity

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_layout);
listView = (ListView)findViewById(R.id.listView);


final ArrayList<Card> list = new ArrayList<>();
list.add(new com.enteruki.deeprelaxation.Card("drawable://"+R.drawable.uki,"Test one"));
list.add(new com.enteruki.deeprelaxation.Card("drawable://"+R.drawable.beach1,"Beach"));
list.add(new com.enteruki.deeprelaxation.Card("drawable://"+R.drawable.bonfire,"Bon Fire"));
list.add(new com.enteruki.deeprelaxation.Card("drawable://"+R.drawable.forests1,"Forest"));
list.add(new com.enteruki.deeprelaxation.Card("drawable://"+R.drawable.frogs,"Frogs"));
list.add(new com.enteruki.deeprelaxation.Card("drawable://"+R.drawable.heavyrain,"Heavy Rain"));
list.add(new com.enteruki.deeprelaxation.Card("drawable://"+R.drawable.insects,"Insects"));
list.add(new com.enteruki.deeprelaxation.Card("drawable://"+R.drawable.lightning,"Lightning"));
list.add(new com.enteruki.deeprelaxation.Card("drawable://"+R.drawable.underwater,"Under Water"));
list.add(new com.enteruki.deeprelaxation.Card("drawable://"+R.drawable.windstrome,"Wind Storme"));

CustomListAdapter adapter = new CustomListAdapter(this,R.layout.activity_main,list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position==0){
Intent intent = new Intent(view.getContext(), TestoneActivity.class);
startActivityForResult(intent,0);
}
else if (position == 1)
{
Intent intent = new Intent(view.getContext(),BeachActivity.class);
startActivityForResult(intent,1);
}
}
});

}
}

最佳答案

您应该使用您的 Activity 上下文创建 Intent 。因此,您需要更改以下内容:

Intent intent = new Intent(view.getContext(), TestoneActivity.class);
startActivityForResult(intent,0);

至:

Intent intent = new Intent(MainActivity.this, TestoneActivity.class);
startActivityForResult(intent,0);

关于java - 我正在尝试使用 ListView 按钮从 cutomlistadapter 打开 Android Activity 。我尽了最大努力但没有结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47067390/

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