gpt4 book ai didi

java - 仅从自定义数组列表中获取字符串并使用字符串创建字符串数组

转载 作者:行者123 更新时间:2023-11-30 02:57:41 25 4
gpt4 key购买 nike

目标:

使用这些字段创建一个新类 (diaryLogs) 来保存值,并使用一个构造函数接受一个 int(0 Mon、1 Tue、2 Wed 等)和两个字符串作为日期时间和日记条目以初始化实例的对象。由于将在同一天创建多个条目,因此使用 ArrayList < diaryLogs > 来存储这些值。由于此值将在 fragment 之间共享,因此在 MainActivity 类中将该变量声明为静态变量。使用 ArrayList 的 .add() 方法将新的 diaryLog 添加到 setOnClickListener() 方法中的 ArrayList。对其他日记页面 fragment 重复此操作。

monday_list fragment 由一个 ListView 和一个用于返回到 Monday_fragment 页面的按钮组成。在 monday_list xml 文件中创建这些对象。创建一个扩展 ListFragment 类的 Monday_list_fragment。在 onCreateView() 方法中 inflate() monday_list 资源。

在 onCreate() 方法中,您需要提供一个字符串数组作为用于填充页面上 ListView 的 setListAdapter() 方法中列表的来源。这些字符串位于 MainActivity 中的静态 ListArray 变量中。但是,它们不是所需的形式,您必须解压缩 diaryLog 元素才能获得所需的字符串。为此,您必须创建一个足够大的字符串数组来容纳要显示的字符串。由于它由日期/时间字符串和后跟日记条目字符串组成,因此每个 diaryLog 元素将有两个这样的字符串。使用 Iterator 遍历您的 ListArray。将日期字符串和日记字符串复制到本地字符串中。然后将这个本地字符串作为setListAdapter()的相关参数,让ListView显示需要的字符串。

问题:

如何仅从自定义数组列表中获取字符串并使用这些字符串创建字符串数组。所以稍后这些可以膨胀到 ListView 。除非我的完整方法是错误的。

我的类及其构造函数

public DiaryLogs(int day, String timeEntry, String entryEntered) {
super();
this.day = day;
this.timeEntry = timeEntry;
this.entryEntered = entryEntered;
}

MainActivity.java 中的静态变量

public static int Monday=0;
public static int Tuesday=1;
public static int Wednesday=2;
public static int Thursday=3;
public static int Friday=4;
public static String timeEntry;
public static String entryEntered;
public static ArrayList<DiaryLogs> logs;
public static String[] entry;

Monday_fragment.java 中的 OnClick 方法

public void onClick(View v) {   

EditText timeText = (EditText) getView().findViewById(R.id.dateTimeEText);

EditText entryText = (EditText) getView().findViewById(R.id.diaryEntryEText);
String timeEntry = timeText.getText().toString();

String entryEntered = entryText.getText().toString();
DiaryLogs dLogs = new DiaryLogs(MainActivity.Monday, timeEntry, entryEntered);
MainActivity.logs = new ArrayList<DiaryLogs>();
MainActivity.logs.add(dLogs);

更新

在使用迭代器时,在 Monday_fragment 的 onCreate 方法中我可以看到该值,但在 Monday_listz_fragment 中它给出了运行时错误。

Monday_list_fragment 类:

public class Monday_list_fragment extends ListFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

return inflater
.inflate(R.layout.monday_list_fragment, container, false);

}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

// setListAdapter(new ArrayAdapter<String>(getActivity(),
// android.R.layout.simple_list_item_1, data ));

super.onViewCreated(view, savedInstanceState);

}

@Override
public void onCreate(Bundle savedInstanceState) {
Iterator it = MainActivity.logs.iterator();
while (it.hasNext()) {
Object element = it.next();

for (int j = 0; j < MainActivity.logs.size(); j++) {
String[] text = { MainActivity.logs.get(j).getTimeEntry(),
MainActivity.logs.get(j).getEntryEntered() };

System.out.println(text[0] + "\n" + text[1]);

}

}

super.onCreate(savedInstanceState);

}

}

目的是获取字符串的字符串数组 {MainActivity.logs.get(j).getTimeEntry(),MainActivity.logs.get(j).getEntryEntered()}所以我可以将它用作 setListadapter 的数据/数组源。

LOGCAT 错误:

04-11 23:45:53.992: E/AndroidRuntime(10006): FATAL EXCEPTION: main
04-11 23:45:53.992: E/AndroidRuntime(10006): Process: com.example.s0217980_diary, PID: 10006
04-11 23:45:53.992: E/AndroidRuntime(10006): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.s0217980_diary/com.example.s0217980_diary.MainActivity}: android.view.InflateException: Binary XML file line #41: Error inflating class fragment
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.os.Handler.dispatchMessage(Handler.java:102)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.os.Looper.loop(Looper.java:136)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-11 23:45:53.992: E/AndroidRuntime(10006): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 23:45:53.992: E/AndroidRuntime(10006): at java.lang.reflect.Method.invoke(Method.java:515)
04-11 23:45:53.992: E/AndroidRuntime(10006): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-11 23:45:53.992: E/AndroidRuntime(10006): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-11 23:45:53.992: E/AndroidRuntime(10006): at dalvik.system.NativeStart.main(Native Method)
04-11 23:45:53.992: E/AndroidRuntime(10006): Caused by: android.view.InflateException: Binary XML file line #41: Error inflating class fragment
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
04-11 23:45:53.992: E/AndroidRuntime(10006): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.Activity.setContentView(Activity.java:1929)
04-11 23:45:53.992: E/AndroidRuntime(10006): at com.example.s0217980_diary.MainActivity.onCreate(MainActivity.java:52)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.Activity.performCreate(Activity.java:5231)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-11 23:45:53.992: E/AndroidRuntime(10006): ... 11 more
04-11 23:45:53.992: E/AndroidRuntime(10006): Caused by: java.lang.NullPointerException
04-11 23:45:53.992: E/AndroidRuntime(10006): at com.example.s0217980_diary.Monday_list_fragment.onCreate(Monday_list_fragment.java:45)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.Fragment.performCreate(Fragment.java:1678)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:859)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.Activity.onCreateView(Activity.java:4786)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
04-11 23:45:53.992: E/AndroidRuntime(10006): ... 21 more
04-11 23:48:11.294: D/dalvikvm(10069): GC_FOR_ALLOC freed 71K, 6% free 3267K/3448K, paused 16ms, total 24ms
04-11 23:48:11.364: D/AndroidRuntime(10069): Shutting down VM
04-11 23:48:11.364: W/dalvikvm(10069): threadid=1: thread exiting with uncaught exception (group=0xb0c9db20)
04-11 23:48:11.374: E/AndroidRuntime(10069): FATAL EXCEPTION: main
04-11 23:48:11.374: E/AndroidRuntime(10069): Process: com.example.s0217980_diary, PID: 10069
04-11 23:48:11.374: E/AndroidRuntime(10069): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.s0217980_diary/com.example.s0217980_diary.MainActivity}: android.view.InflateException: Binary XML file line #41: Error inflating class fragment
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.os.Handler.dispatchMessage(Handler.java:102)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.os.Looper.loop(Looper.java:136)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-11 23:48:11.374: E/AndroidRuntime(10069): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 23:48:11.374: E/AndroidRuntime(10069): at java.lang.reflect.Method.invoke(Method.java:515)
04-11 23:48:11.374: E/AndroidRuntime(10069): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-11 23:48:11.374: E/AndroidRuntime(10069): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-11 23:48:11.374: E/AndroidRuntime(10069): at dalvik.system.NativeStart.main(Native Method)
04-11 23:48:11.374: E/AndroidRuntime(10069): Caused by: android.view.InflateException: Binary XML file line #41: Error inflating class fragment
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
04-11 23:48:11.374: E/AndroidRuntime(10069): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.Activity.setContentView(Activity.java:1929)
04-11 23:48:11.374: E/AndroidRuntime(10069): at com.example.s0217980_diary.MainActivity.onCreate(MainActivity.java:52)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.Activity.performCreate(Activity.java:5231)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-11 23:48:11.374: E/AndroidRuntime(10069): ... 11 more
04-11 23:48:11.374: E/AndroidRuntime(10069): Caused by: java.lang.NullPointerException
04-11 23:48:11.374: E/AndroidRuntime(10069): at com.example.s0217980_diary.Monday_list_fragment.onCreate(Monday_list_fragment.java:45)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.Fragment.performCreate(Fragment.java:1678)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:859)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.Activity.onCreateView(Activity.java:4786)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
04-11 23:48:11.374: E/AndroidRuntime(10069): ... 21 more

最佳答案

我不确定这是否是您要找的:

  • 创建一个与列表一样大的字符串数组
  • 遍历每个列表条目并将其日期字符串(在示例中称为 getDateString())和条目字符串(称为 getEntry())连接起来,并将它们存储在对应的数组索引

代码:

entry = new String[logs.size()];

for (int i = 0; i < logs.size(); i++) {
entry[i] = logs.get(i).getDateString() + " " + logs.get(i).getEntry();
}

关于java - 仅从自定义数组列表中获取字符串并使用字符串创建字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23024663/

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