ListView (placed under fragment activity and shown via a ViewPager) doesn't show all items

91 views Asked by At

this is the first time posting here. So I have a screenwriting app I am creating using Java on Android. It has a simple layout as shown below.layout of the activity

So, when user clicks write, they will be able to write their screenplay using plain text, but following a specific syntax called Fountain. An example is given below.

If user writes the following text...

WRITER (Smiles) I have to do this.

The app will split the text into lines, and add the different lines to list string (listy), and from there, when the user clicks on the read tab, the app will start from position 0 of listy and check if the item is a scene heading, action, or dialogue, and it will format the text based on that. Action and scene heading gravity is "left", while dialogue gravity is "center".

Everything works fine, I don't get any errors. However, there is only one problem.

When the user clicks on read, the app shows the script as shown below.

This is what the user writes.

This is what the user sees after formatting the screenplay.

Problems is that after formatting, the listview does not show all items. Like I mentioned above, I split the plain text into individual lines, and add them to ListString(listy), and then after determining whether or not an item from ListString(listy) is an action, or dialogue, or scene heading, I create a map with the keys 'type' and 'content', type being scene heading, dialogue, or action, and content being the item from ListString(listy).

And then I add the map to ListMap(fountain) and set it as listview data.

The problem I have is that the listview does not show all items. When I scroll, some items become visible while some go invisible. And the scroll bar on the side does not move, it stays at the top as if I am not scrolling at all.

At first I thought it was my phone (Samsung J7 Neo), but I also tested the app on a Samsung S10+, and problem still persists.

Please help me out. What could be the problem?

NB: I am using Sketchware to develop my app, not Android Studio.

This is the XML code for the fragment.

<LinearLayout

    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:dividerHeight="3dp"
        android:choiceMode="none"
        android:layout_weight="1"
        android:divider="@android:color/transparent"/>
</LinearLayout>

And this is the ReadScriptFragmentActivity.java

public class ReadscriptFragmentActivity extends  Fragment  { 
    
    
    private HashMap<String, Object> map = new HashMap<>();
    private String path = "";
    private String SCRIPT = "";
    private double pos = 0;
    private HashMap<String, Object> map2 = new HashMap<>();
    private String item1 = "";
    private boolean isUpperCaseOnly = false;
    private double pos1 = 0;
    private String character = "";
    private String item2 = "";
    private String typeBind = "";
    
    private ArrayList<String> listy = new ArrayList<>();
    private ArrayList<HashMap<String, Object>> fountain = new ArrayList<>();
    private ArrayList<String> listy2 = new ArrayList<>();
    
    private ListView lv;
    
    private SharedPreferences sp;
    private SharedPreferences script;
    private Notification not;
    @NonNull
    @Override
    public View onCreateView(@NonNull LayoutInflater _inflater, @Nullable ViewGroup _container, @Nullable Bundle _savedInstanceState) {
        View _view = _inflater.inflate(R.layout.readscript_fragment, _container, false);
        initialize(_savedInstanceState, _view);
        initializeLogic();
        return _view;
    }
    
    private void initialize(Bundle _savedInstanceState, View _view) {
        
        lv = (ListView) _view.findViewById(R.id.lv);
        sp = getContext().getSharedPreferences("sp", Activity.MODE_PRIVATE);
        script = getContext().getSharedPreferences("script", Activity.MODE_PRIVATE);
    }
    
    private void initializeLogic() {
        _getScript();
    }
    
    @Override
    public void onActivityResult(int _requestCode, int _resultCode, Intent _data) {
        
        super.onActivityResult(_requestCode, _resultCode, _data);
        
        switch (_requestCode) {
            
            default:
            break;
        }
    }
    
    @Override
    public void onResume() {
        super.onResume();
        _getScript();
    }
    public void _getScript () {
        path = script.getString("path", "");
        if (path.equals("")) {
            SketchwareUtil.showMessage(getContext(), "Error Opening Script!");
        }
        else {
            if (FileUtil.readFile(path).equals("")) {
                SketchwareUtil.showMessage(getContext(), "Error Opening Script!");
            }
            else {
                map = new Gson().fromJson(FileUtil.readFile(path), new TypeToken<HashMap<String, Object>>(){}.getType());
                SCRIPT = map.get("script").toString();
                listy = new ArrayList<String>(Arrays.asList(SCRIPT.split("\n\n")));
                lv.setAdapter(new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, listy));
                pos = 0;
                for(int _repeat35 = 0; _repeat35 < (int)(listy.size()); _repeat35++) {
                    item1 = listy.get((int)(pos));
                    if (item1.contains("\n")) {
                        item2 = item1.substring((int)(0), (int)(item1.indexOf("\n") - 1));
                        if (item2.equals(item2.toUpperCase())) {
                            map2 = new HashMap<>();
                            map2.put("type", "dialogue");
                            map2.put("content", "          ".concat(item1));
                            fountain.add(map2);
                        }
                        else {
                            map2 = new HashMap<>();
                            map2.put("type", "action");
                            map2.put("content", item1);
                            fountain.add(map2);
                        }
                    }
                    else {
                        if (item1.equals(item1.toUpperCase())) {
                            if (item1.startsWith("INT.") || item1.startsWith("EXT.")) {
                                map2 = new HashMap<>();
                                map2.put("type", "scene");
                                map2.put("content", item1);
                                fountain.add(map2);
                            }
                            else {
                                map2 = new HashMap<>();
                                map2.put("type", "action");
                                map2.put("content", item1);
                                fountain.add(map2);
                            }
                        }
                        else {
                            map2 = new HashMap<>();
                            map2.put("type", "action");
                            map2.put("content", item1);
                            fountain.add(map2);
                        }
                    }
                    pos++;
                }
            }
        }
        lv.setAdapter(new LvAdapter(fountain));
    }
    
    
    public class LvAdapter extends BaseAdapter {
        ArrayList<HashMap<String, Object>> _data;
        public LvAdapter(ArrayList<HashMap<String, Object>> _arr) {
            _data = _arr;
        }
        
        @Override
        public int getCount() {
            return _data.size();
        }
        
        @Override
        public HashMap<String, Object> getItem(int _index) {
            return _data.get(_index);
        }
        
        @Override
        public long getItemId(int _index) {
            return _index;
        }
        @Override
        public View getView(final int _position, View _v, ViewGroup _container) {
            LayoutInflater _inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View _view = _v;
            if (_view == null) {
                _view = _inflater.inflate(R.layout.scriptreaditem, null);
            }
            
            final LinearLayout all = (LinearLayout) _view.findViewById(R.id.all);
            final TextView scene = (TextView) _view.findViewById(R.id.scene);
            final TextView action = (TextView) _view.findViewById(R.id.action);
            final TextView dialogue = (TextView) _view.findViewById(R.id.dialogue);
            
            typeBind = _data.get((int)_position).get("type").toString();
            if (typeBind.equals("scene")) {
                action.setVisibility(View.GONE);
                dialogue.setVisibility(View.GONE);
                scene.setText(_data.get((int)_position).get("content").toString());
                scene.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/courier_prime_regular.ttf"), 1);
            }
            if (typeBind.equals("action")) {
                scene.setVisibility(View.GONE);
                dialogue.setVisibility(View.GONE);
                action.setText(_data.get((int)_position).get("content").toString());
                action.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/courier_prime_regular.ttf"), 0);
            }
            if (typeBind.equals("dialogue")) {
                scene.setVisibility(View.GONE);
                action.setVisibility(View.GONE);
                dialogue.setText(_data.get((int)_position).get("content").toString());
                dialogue.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/courier_prime_regular.ttf"), 0);
            }
            
            return _view;
        }
    }
}

Please help. I am not sure what the problem could be. I am not a developer. I learned Java through Sketchware. So please be patient with me. Thank you in advance.

EDIT: I figured out the problem. If you look at the ListView onBindCustomView, there are three if statements that I added to check if an item is a dialogue, scene heading, or action. The problem is that I added separate if statements for each of them. So what I did was put them in an if-else statement, as in if item is scene, then format as scene, else if item is action, format as action, else if item is dialogue, format as dialogue. So in short, I put the other if statements under the "else" section of the previous if statement. (Sorry if my english is bad). Here is the new updated code. public class ReadscriptFragmentActivity extends Fragment {

private HashMap<String, Object> map = new HashMap<>();
private String path = "";
private String SCRIPT = "";
private double pos = 0;
private HashMap<String, Object> map2 = new HashMap<>();
private String item1 = "";
private boolean isUpperCaseOnly = false;
private double pos1 = 0;
private String character = "";
private String item2 = "";
private String typeBind = "";

private ArrayList<String> listy = new ArrayList<>();
private ArrayList<HashMap<String, Object>> fountain = new ArrayList<>();
private ArrayList<String> listy2 = new ArrayList<>();

private ListView lv;

private SharedPreferences sp;
private SharedPreferences script;
private Notification not;
@NonNull
@Override
public View onCreateView(@NonNull LayoutInflater _inflater, @Nullable ViewGroup _container, @Nullable Bundle _savedInstanceState) {
    View _view = _inflater.inflate(R.layout.readscript_fragment, _container, false);
    initialize(_savedInstanceState, _view);
    initializeLogic();
    return _view;
}

private void initialize(Bundle _savedInstanceState, View _view) {
    
    lv = (ListView) _view.findViewById(R.id.lv);
    sp = getContext().getSharedPreferences("sp", Activity.MODE_PRIVATE);
    script = getContext().getSharedPreferences("script", Activity.MODE_PRIVATE);
}

private void initializeLogic() {
    _getScript();
}

@Override
public void onActivityResult(int _requestCode, int _resultCode, Intent _data) {
    
    super.onActivityResult(_requestCode, _resultCode, _data);
    
    switch (_requestCode) {
        
        default:
        break;
    }
}

@Override
public void onResume() {
    super.onResume();
    _getScript();
}
public void _getScript () {
    path = script.getString("path", "");
    if (path.equals("")) {
        SketchwareUtil.showMessage(getContext(), "Error Opening Script!");
    }
    else {
        if (FileUtil.readFile(path).equals("")) {
            SketchwareUtil.showMessage(getContext(), "Error Opening Script!");
        }
        else {
            map = new Gson().fromJson(FileUtil.readFile(path), new TypeToken<HashMap<String, Object>>(){}.getType());
            SCRIPT = map.get("script").toString();
            listy = new ArrayList<String>(Arrays.asList(SCRIPT.split("\n\n")));
            pos = 0;
            for(int _repeat35 = 0; _repeat35 < (int)(listy.size()); _repeat35++) {
                item1 = listy.get((int)(pos));
                if (item1.contains("\n")) {
                    item2 = item1.substring((int)(0), (int)(item1.indexOf("\n") - 1));
                    if (item2.equals(item2.toUpperCase())) {
                        map2 = new HashMap<>();
                        map2.put("type", "dialogue");
                        map2.put("content", "          ".concat(item1));
                        fountain.add(map2);
                    }
                    else {
                        map2 = new HashMap<>();
                        map2.put("type", "action");
                        map2.put("content", item1);
                        fountain.add(map2);
                    }
                }
                else {
                    if (item1.equals(item1.toUpperCase())) {
                        if (item1.startsWith("INT.") || item1.startsWith("EXT.")) {
                            map2 = new HashMap<>();
                            map2.put("type", "scene");
                            map2.put("content", item1);
                            fountain.add(map2);
                        }
                        else {
                            map2 = new HashMap<>();
                            map2.put("type", "action");
                            map2.put("content", item1);
                            fountain.add(map2);
                        }
                    }
                    else {
                        map2 = new HashMap<>();
                        map2.put("type", "action");
                        map2.put("content", item1);
                        fountain.add(map2);
                    }
                }
                pos++;
            }
        }
    }
    lv.setAdapter(new LvAdapter(fountain));
}


public class LvAdapter extends BaseAdapter {
    ArrayList<HashMap<String, Object>> _data;
    public LvAdapter(ArrayList<HashMap<String, Object>> _arr) {
        _data = _arr;
    }
    
    @Override
    public int getCount() {
        return _data.size();
    }
    
    @Override
    public HashMap<String, Object> getItem(int _index) {
        return _data.get(_index);
    }
    
    @Override
    public long getItemId(int _index) {
        return _index;
    }
    @Override
    public View getView(final int _position, View _v, ViewGroup _container) {
        LayoutInflater _inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View _view = _v;
        if (_view == null) {
            _view = _inflater.inflate(R.layout.scriptreaditem, null);
        }
        
        final LinearLayout all = (LinearLayout) _view.findViewById(R.id.all);
        final TextView scene = (TextView) _view.findViewById(R.id.scene);
        final TextView action = (TextView) _view.findViewById(R.id.action);
        final TextView dialogue = (TextView) _view.findViewById(R.id.dialogue);
        
        scene.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/courier_prime_regular.ttf"), 1);
        action.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/courier_prime_regular.ttf"), 0);
        dialogue.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/courier_prime_regular.ttf"), 0);
        typeBind = _data.get((int)_position).get("type").toString();
        if (typeBind.equals("scene")) {
            action.setVisibility(View.GONE);
            dialogue.setVisibility(View.GONE);
            scene.setVisibility(View.VISIBLE);
            scene.setText(_data.get((int)_position).get("content").toString());
        }
        else {
            if (typeBind.equals("action")) {
                scene.setVisibility(View.GONE);
                dialogue.setVisibility(View.GONE);
                action.setVisibility(View.VISIBLE);
                action.setText(_data.get((int)_position).get("content").toString());
            }
            else {
                if (typeBind.equals("dialogue")) {
                    scene.setVisibility(View.GONE);
                    action.setVisibility(View.GONE);
                    dialogue.setVisibility(View.VISIBLE);
                    dialogue.setText(_data.get((int)_position).get("content").toString());
                }
            }
        }
        
        return _view;
    }
}

}

Thanks to those who responded. I guess this question is answered now. Cheers.

0

There are 0 answers