Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Horizontalscrollview:imageView从其他活动返回时消失_Java_Android_Imageview_Android Linearlayout_Horizontalscrollview - Fatal编程技术网

Java Horizontalscrollview:imageView从其他活动返回时消失

Java Horizontalscrollview:imageView从其他活动返回时消失,java,android,imageview,android-linearlayout,horizontalscrollview,Java,Android,Imageview,Android Linearlayout,Horizontalscrollview,我正在开发我的第一个应用程序,但ImageView和Horizontalscrollview有一个问题:我想收集用户在Horizontalscrollview中添加图像所达到的所有目标(目标达到-->显示新图像) 它们正确地显示,一切似乎都正常,但当我打开另一个活动,然后我回来时,它们就消失了。我试图使布局无效,但什么也没有,我是不是遗漏了什么 提前谢谢你 这是我的XML文件 <!-- language: lang-xml --> <?xml version="1.0" en

我正在开发我的第一个应用程序,但ImageView和Horizontalscrollview有一个问题:我想收集用户在Horizontalscrollview中添加图像所达到的所有目标(目标达到-->显示新图像)

它们正确地显示,一切似乎都正常,但当我打开另一个活动,然后我回来时,它们就消失了。我试图使布局无效,但什么也没有,我是不是遗漏了什么

提前谢谢你

这是我的XML文件

<!-- language: lang-xml -->
<?xml version="1.0" encoding="utf-8"?>
                <androidx.coordinatorlayout.widget.CoordinatorLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/layout3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".Main3Activity">

                <HorizontalScrollView
                    android:id="@+id/horizontal_scroll"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="300dp">

                    <LinearLayout
                        android:id="@+id/linear"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal" >

                    </LinearLayout>

                </HorizontalScrollView>

            </androidx.coordinatorlayout.widget.CoordinatorLayout> 

这是我的Java代码 我用于检查目标是否完成的函数:

<!-- language: lang-java -->   
 protected void checkGoal(){

            int progress = calculateProgress();

            if(goalPref.getInt("Goal",0)!=0){

                if(progress >= goalPref.getInt("Goal", 0)) {
                    int id = layout.getChildCount();
                    addNewImage(id); // <-- here I add the image

                    question.setText(R.string.setgoal);

                    updateGoalPref(0);

                    if (list.size() != 0) {
                        list = new ArrayList<>();
                    }

                    updateProgress();


                }

            }
        }

受保护的void checkGoal(){
int progress=calculateProgress();
如果(goalPref.getInt(“Goal”,0)!=0){
如果(进度>=goalPref.getInt(“目标”,0)){
int id=layout.getChildCount();
addNewImage(id);//
受保护的void addNewImage(int id){
ImageView ImageView=新的ImageView(此);
imageView.setId(id);
设置填充(2,2,2,2);
imageView.setBackgroundResource(R.drawable.check);
imageView.setScaleType(imageView.ScaleType.FIT_XY);
layout.addView(imageView,id);
}

当您转到其他活动并返回时,应在onResume中调用
addNewImage
,以再次显示图像:

@Override
    protected void onResume() {
        super.onResume();
        addNewImage(id);
    }  


如果活动中的目标数量有限,为什么不用XML创建这些ImageView并用java设置它们的可见性?

因为我很固执,所以我设法找到了另一种方法,这就是我所做的:使用我发布的代码,我添加了一个计数器来跟踪用户达到了多少目标,并
onResume()
I显示与计数器值一样多的图像

通过这种方式,用户可以立即看到达到目标的效果,并且活动不会损失任何东西

然后,我实现它来显示目标的描述,用这些图像作为背景更改TextView中的ImageView。我没有使用计数器,而是将要显示的字符串保存在ArrayList中,并将其大小用作计数器

希望它能对别人有用

<!-- language: lang-java -->
 protected void checkGoal(){

        int progress = calculateProgress();

        if(goalPref.getInt("Goal",0)!=0){

            if(progress >= goalPref.getInt("Goal", 0)) {

                String newString = goalPref.getInt("Goal", 0) + "€, " + MainActivity.currentDate();
                goalsCompleted.add(newString);
                updateGoalCompletedList();

                //Here I clean the layout and display all the TextViews
                layout.removeAllViews();
                for(int i=goalsCompleted.size()-1; i>0; i--){
                    addNewTextView(i);
                }

                question.setText(R.string.setgoal);

                updateGoalPref(0);

                if (list.size() != 0) {
                    list = new ArrayList<>();
                }
                updateProgress();
            }
        }
    }

//The function used to display the TextViews
 protected void addNewTextView(int id){

        TextView textView = new TextView(this);
        textView.setBackgroundResource(R.drawable.check1);
        textView.setId(id);
        textView.setPadding(25,260,0,0);
        textView.setTextSize(12);
        textView.setText(goalsCompleted.get(id));
        layout.addView(textView);
    }

//The function used to update and save the array
  public void updateGoalCompletedList(){

        Gson gson3 = new Gson();
        jsonItems3 = gson3.toJson(goalsCompleted);
        SharedPreferences.Editor listEd = goalsCompletedPref.edit();
        listEd.putString("GoalsCompleted", jsonItems3).apply();

    }

//The function used to resume the array, you can call it on onResume() or onCreate()
  public void resumeGoalsCompleted(){

        Gson gson3 = new Gson();
        String savedList = goalsCompletedPref.getString("GoalsCompleted", "");
        goalsCompleted = gson3.fromJson(savedList, new TypeToken<ArrayList<String>>() {
        }.getType());

        if(goalsCompleted==null)
            goalsCompleted = new ArrayList<>();

        //From the most recent to the least recent
        for(int i=goalsCompleted.size()-1; i>0; i--){
            addNewTextView(i);
        }
    }

受保护的void checkGoal(){
int progress=calculateProgress();
如果(goalPref.getInt(“Goal”,0)!=0){
如果(进度>=goalPref.getInt(“目标”,0)){
字符串newString=goalPref.getInt(“目标”,0)+“€,”+MainActivity.currentDate();
goalsCompleted.add(新闻字符串);
UpdateGalCompletedList();
//在这里,我清理布局并显示所有文本视图
layout.removeallview();
对于(int i=goalsCompleted.size()-1;i>0;i--){
addNewTextView(i);
}
question.setText(R.string.setgoal);
updategalpref(0);
如果(list.size()!=0){
列表=新的ArrayList();
}
updateProgress();
}
}
}
//用于显示文本视图的函数
受保护的void addNewTextView(int-id){
TextView TextView=新的TextView(此);
textView.setBackgroundResource(R.drawable.check1);
setId(id);
设置填充(25260,0,0);
textView.setTextSize(12);
setText(goalsCompleted.get(id));
layout.addView(textView);
}
//用于更新和保存阵列的函数
public void updategalCompletedList(){
Gson gson3=新的Gson();
jsonItems3=gson3.toJson(goalsCompleted);
SharedReferences.Editor listEd=goalsCompletedPref.edit();
listEd.putString(“GoalsCompleted”,jsonims3.apply();
}
//用于恢复数组的函数,可以在onResume()或onCreate()上调用
公共无效resumeGoalsCompleted(){
Gson gson3=新的Gson();
String savedList=goalsCompletedPref.getString(“GoalsCompleted”,”);
goalsCompleted=gson3.fromJson(savedList,newTypeToken()){
}.getType());
如果(goalsCompleted==null)
goalsCompleted=新建ArrayList();
//从最近到最近
对于(int i=goalsCompleted.size()-1;i>0;i--){
addNewTextView(i);
}
}

您好,为什么不改用recyclerview?您好Miriana,谢谢您的建议,但我选择的API级别不支持它,所以我正在尝试另一种方法。我可以知道您选择了哪种API级别吗?因为据我所知,recyclerview是向后兼容的API级别24是的,我找到了!谢谢这不是个坏主意,但目标并没有限制,addNewImage(id)会一个接一个地添加目标
<!-- language: lang-java -->
 protected void checkGoal(){

        int progress = calculateProgress();

        if(goalPref.getInt("Goal",0)!=0){

            if(progress >= goalPref.getInt("Goal", 0)) {

                String newString = goalPref.getInt("Goal", 0) + "€, " + MainActivity.currentDate();
                goalsCompleted.add(newString);
                updateGoalCompletedList();

                //Here I clean the layout and display all the TextViews
                layout.removeAllViews();
                for(int i=goalsCompleted.size()-1; i>0; i--){
                    addNewTextView(i);
                }

                question.setText(R.string.setgoal);

                updateGoalPref(0);

                if (list.size() != 0) {
                    list = new ArrayList<>();
                }
                updateProgress();
            }
        }
    }

//The function used to display the TextViews
 protected void addNewTextView(int id){

        TextView textView = new TextView(this);
        textView.setBackgroundResource(R.drawable.check1);
        textView.setId(id);
        textView.setPadding(25,260,0,0);
        textView.setTextSize(12);
        textView.setText(goalsCompleted.get(id));
        layout.addView(textView);
    }

//The function used to update and save the array
  public void updateGoalCompletedList(){

        Gson gson3 = new Gson();
        jsonItems3 = gson3.toJson(goalsCompleted);
        SharedPreferences.Editor listEd = goalsCompletedPref.edit();
        listEd.putString("GoalsCompleted", jsonItems3).apply();

    }

//The function used to resume the array, you can call it on onResume() or onCreate()
  public void resumeGoalsCompleted(){

        Gson gson3 = new Gson();
        String savedList = goalsCompletedPref.getString("GoalsCompleted", "");
        goalsCompleted = gson3.fromJson(savedList, new TypeToken<ArrayList<String>>() {
        }.getType());

        if(goalsCompleted==null)
            goalsCompleted = new ArrayList<>();

        //From the most recent to the least recent
        for(int i=goalsCompleted.size()-1; i>0; i--){
            addNewTextView(i);
        }
    }