0

I was creating the list view like picture below

my picture

If it there is a possibility of creating new code with another way, i would satisify But unfortunately my app shows Project has stopped Here is my code below (it is a first time that i am writing my code to stack ow.) Thank you beforehand! Here in introduction_info_home.java i have provided with myLabels String array and as usual list view.

java>introduction_info_home.java

public class introduction_info_home extends Fragment {
    String[] myLabels={"Introduction to IUT","Scholarships"};
    ListView listView;

public introduction_info_home() {
        // Required empty public constructor
    }

     @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View intro_info_home=inflater.inflate(R.layout.fragment_introduction_info_home, container, false);

        listView=(ListView)intro_info_home.findViewById(R.id.myCustomInfoList);
        MyAdapter myAdapter=new MyAdapter(intro_info_home.getContext(),myLabels);
        listView.setAdapter(myAdapter);
        return intro_info_home;
    }
    class MyAdapter extends ArrayAdapter<String> implements View.OnClickListener{
        Context myContext;
        String myTitles[];
        public MyAdapter( Context myContext, String[] myTitles) {
            super(myContext,R.layout.row, myTitles);
            this.myContext = myContext;
            this.myTitles = myTitles;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            LayoutInflater inflater = (LayoutInflater) view.getContext().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View myView= inflater.inflate(R.layout.row, null,false);
            TextView txtListChild = (TextView) myView.findViewById(R.id.text1);
            if(i<myTitles.length) txtListChild.setText(myTitles[i]);
            if(i==0){
                myView.setBackgroundColor(Color.BLUE);
            }else if(i==1){
                myView.setBackgroundColor(Color.RED);
            }else if(i==2){
                myView.setBackgroundColor(Color.BLUE);
            }else if(i==3){
                myView.setBackgroundColor(Color.RED);
            }else if(i==4){
                myView.setBackgroundColor(Color.BLUE);
            }else{
                myView.setBackgroundColor(Color.CYAN);
            }
            return myView;
        }

        @Override
        public void onClick(View view) {

        }
    }
}

layout>fragment_introduction_info_home.xml

<FrameLayout 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/intro_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    tools:context="com.example.bekzod.inhacontest.introduction_info_home">
    <ListView
        android:id="@+id/myCustomInfoList"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</FrameLayout>

layout>row.xml

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

    android:background="#0073ff">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="32dp"
            android:id="@+id/text1"
            android:gravity="center"
            android:textColor="#ffffff"
            android:textSize="32sp"
            android:text="Medium text"
            />
</LinearLayout>
ColdFire
  • 6,764
  • 6
  • 35
  • 51
  • 1
    post your logcat. there might be exception – Jyoti JK Mar 22 '18 at 10:28
  • Have you tried debugging ? Where does your app crash ? before you load the fragment or after ? – Sam Mar 22 '18 at 10:35
  • before adding listview all this above stuff it all worked perfect – Begzod Erkinov Mar 22 '18 at 10:39
  • And one thing to mention that this fragment is onto another fragment just i am using fragment transform to swap two fragments – Begzod Erkinov Mar 22 '18 at 10:42
  • I started working with android studio 2 weeks ago and still i am having troubleshots, maybe i need more recommended resources. It is not for making money but to make my dreaming app – Begzod Erkinov Mar 22 '18 at 10:48
  • Maybe View myView= inflater.inflate(R.layout.row, null,false); can be error – Begzod Erkinov Mar 22 '18 at 10:49
  • The `View` parameter in `getView()` is going to be null the first few times it's called. Use your `Context mContext` field to get the `LayoutInflater`; e.g., `LayoutInflater inflater = LayoutInflater.from(mContext);`. Also, if that layout doesn't scroll, using a `ListView` is kinda pointless. Just put your items in a vertical `LinearLayout`. Lastly, please have a look at the links now at the top of your question for help in determining and resolving these issues yourself. – Mike M. Mar 22 '18 at 10:50
  • Thank you all. When I changed from FrameLayout to LinearLayout all are worked. Thanks much!!! – Begzod Erkinov Mar 22 '18 at 11:00

0 Answers0