gpt4 book ai didi

java - 我如何在 ListView 中打开 Activity 数组类名

转载 作者:行者123 更新时间:2023-11-30 04:59:29 25 4
gpt4 key购买 nike

我有 n 个类项目自动生成,就像窗口中的 java 我想在我的应用程序中使用这个类,我如何在 ListView 中按名称打开类 Activity ? android可以通过工具来完成

  import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private static final Class[] CLASSES = new Class[]{
a.class,
a1.class,
a2.class,
a3.class,
a4.class,
a5.class,
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = findViewById(R.id.list_view);

MyArrayAdapter adapter = new MyArrayAdapter(this, android.R.layout.simple_list_item_1, CLASSES);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}

}

最佳答案

是的,很简单

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
private static final Class[] CLASSES = new Class[]{
a.class,
a1.class,
a2.class,
a3.class,
a4.class,
a5.class
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = findViewById(R.id.list_view);

MyArrayAdapter adapter = new MyArrayAdapter(this, android.R.layout.simple_list_item_1, CLASSES);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Class clicked = CLASSES[position];
startActivity(new Intent(this, clicked));
}

private static class MyArrayAdapter extends ArrayAdapter<Class> {
private Context mContext;
private Class[] mClasses;

private MyArrayAdapter(Context context, int resource, Class[] objects) {
super(context, resource, objects);
mContext = context;
mClasses = objects;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;

if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(android.R.layout.simple_list_item_1, null);
}
((TextView) view.findViewById(android.R.id.text1)).setText(mClasses[position].getSimpleName());

return view;
}
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
style="@style/CustomRelativeLayout"
xmlns:android="http://schemas.android.com/apk/res/android">

<ImageView
android:id="@+id/logo"
style="@style/CustomLogoImageView"
android:contentDescription="@string/app_name"/>

<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/logo"
android:scrollbars="none"/>

</RelativeLayout>

关于java - 我如何在 ListView 中打开 Activity 数组类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58485519/

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