当前位置: 首页 > 知识库问答 >
问题:

如何在扩展ListView的另一个片段中将ArrayList数据设置到ListView?

龙永福
2023-03-14

如何在扩展ListView的另一个片段中将ArrayList数据设置到ListView中?

我们将非常感谢各位专家的帮助。

定义ArrayList接受int、string、string。我从两个EditText获取用户输入并将它们添加到此ArrayList。当用户重新输入时,它应该继续将其添加到ArrayList。

目标是从用户那里获取所有条目,并在下一个片段的ListView中显示它。但只有两个字符串从ArrayList转换为字符串数组,用作ArrayAdapter的数据源。因此重写了diarylogs类中的toSting方法。我可以在sysout时从ArrayList中看到值。但是,一旦我设置了ListAdapter,应用程序就会崩溃。无法理解我做错了什么。

更新

Class MainActivity extends Activity

//Static variable
public static ArrayList<DiaryLogs> test;
public static int Monday=0;

分类日志

public class DiaryLogs {

    String timeEntry, entryEntered;
    int day;

    // single constructor that takes an integer and two string
    public DiaryLogs(int day, String timeEntry, String entryEntered) {
        super();
        this.day = day;
        this.timeEntry = timeEntry;
        this.entryEntered = entryEntered;

    }

    public String getTimeEntry() {
        return timeEntry;
    }

    public void setTimeEntry(String timeEntry) {
        this.timeEntry = timeEntry;
    }

    public String getEntryEntered() {
        return entryEntered;
    }

    public void setEntryEntered(String entryEntered) {
        this.entryEntered = entryEntered;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    public String toString() {

        return timeEntry + "\n" + entryEntered;


    }
}

课程Monday\u片段

public class Monday_fragment extends Fragment {

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

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

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        currentDateTime();
        super.onViewCreated(view, savedInstanceState);

    }

    public void currentDateTime() {
        EditText timeText = (EditText) getView().findViewById(
                R.id.dateTimeEText);
        SimpleDateFormat df = new SimpleDateFormat("d/M/yyyy:H:m");
        String dateTime = df.format(Calendar.getInstance().getTime());
        timeText.setText(dateTime);
    }

    @Override
    public void onStart() {

        Button saveBtn = (Button) getView()
                .findViewById(R.id.saveDiaryEntryBtn);
        saveBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                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();

                MainActivity.test = new ArrayList<DiaryLogs>();

                MainActivity.test.add(new DiaryLogs(MainActivity.Monday,
                        timeEntry, entryEntered));

            }

        }

        );

        Button showBtn = (Button) getView().findViewById(
                R.id.showDiaryEntriesBtn);
        showBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager
                        .beginTransaction();
                Monday_list_fragment mlf = new Monday_list_fragment();
                fragmentTransaction.replace(android.R.id.content, mlf);
                fragmentTransaction.commit();

            }
        });

        super.onStart();
    }

}

班级Monday_list_fragment

public class Monday_list_fragment extends ListFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

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

    }

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

        super.onViewCreated(view, savedInstanceState);


    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

System.out.println(MainActivity.test); //test: if value can be seen

        ArrayAdapter<DiaryLogs> arrayAdapter = new ArrayAdapter<DiaryLogs>(
                getActivity(), 
                android.R.layout.simple_list_item_1, MainActivity.test
                );
        setListAdapter(arrayAdapter); //once i set adapter it crashes

    }

}

布局XML文件:monday_list_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@id/android:list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:smoothScrollbar="true" >
    </ListView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <Button
            android:id="@+id/returnBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/return_mon_Btn" 
            android:onClick="monClick">
        </Button>
    </LinearLayout>
</RelativeLayout>

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

共有1个答案

朱丰
2023-03-14

如果您有一个像ArrayList这样的数组,您不需要将其解压缩并提取您想要的字符串并将其添加到另一个数组中,只是为了将其输入到列表视图中。只需覆盖DiaryLogs类中的toString()方法并使用您选择的字符串返回它(例如timeEntry

// class DiaryLogs
class DiaryLogs {
    ...
    @Override
    public toString() {
        return dateTime + " - " + entryName;
    }
    ...
}

// in your activity
ArrayList<DiaryLogs> diaryLogs = new ArrayList<DiaryLogs>();
diaryLogs.add(new DiaryLogs(1, "Apr. 14, 2014", "Entry 1"));
diaryLogs.add(new DiaryLogs(1, "Apr. 14, 2014", "Entry 2"));
diaryLogs.add(new DiaryLogs(1, "Apr. 14, 2014", "Entry 3"));

yourListView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, diaryLogs));

yourListView显然是您正在使用的listview。

 类似资料:
  • 我试图使用另一个片段中的数据填充我的Listview。 我可以从另一个片段获取数据,但当我尝试创建listview对象时,它返回null。 结果,该应用程序正在崩溃。 我从一个片段中获取用户的数据,然后从另一个片段中调用一个方法来传递数据。我正在第二个方法的poplist()方法中创建listview对象和数组适配器。但是,由于空指针异常,应用程序正在崩溃。请帮忙。 我尝试了以下方法,但不起作用

  • 我知道的一种方法是通过活动。我们可以将数据从一个片段发送到另一个活动,再从一个活动发送到另一个片段。还有其他方法吗?

  • 我有两个活动,在活动1中有碎片,在活动2中有碎片 fragment1和fragment2扩展自FragmentActivity1,activity2扩展自AppCompatActivity 我想将字符串值从fragment1发送到fragment2,而不获取空值。我希望你能理解这个问题。谢谢

  • 在FragmentOne.class扩展android.support.v4.app.

  • 我需要将数据从片段发送到另一个活动 我在Homeactive下的LoadsFraank中使用此代码 在另一个活动(LoadActivity)中接收数据 但是意图没有附加条件 请看下面的截图

  • 我想将数据从回收视图传递到另一个片段中,第一个适配器用于显示项目,第二个适配器用于篮子片段,希望将选定的项目放入。 我要从中获取数据的适配器 这是我要将数据放入的篮子片段的适配器 现在,我可以用来在它们之间传递数据。