1

I'm trying to create listview programatically in android.

My goal is to create a dynamic view where if i click one button it will change the content inside the layout from listview into another view or vice versa.

Everything seems good i successfully put the listview inside a linear layout from the java file and i did that by creating a custom layout for the cell.

The problem is it shows the listview but it does not add the cell. I know that it place the listview because the background become green when I run it but it does not show up the cell of the listview.

Am i missing something?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:backgroundTint="@color/colorPrimary">

    <TextView
        android:id="@+id/listContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello"/>

</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
    LinearLayout mainView,sideView;
    ListView notelist;
    String[] Names = {"great","good","average","great","good","average"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainView = (LinearLayout)findViewById(R.id.mainView);

        notelist = new ListView(this);
        notelist.setBackgroundColor(Color.GREEN);
        CustomAdapter customAdapter = new CustomAdapter();
        notelist.setAdapter(customAdapter);
        notelist.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));

        mainView.addView(notelist);

    }

    class CustomAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return Names.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = getLayoutInflater().inflate(R.layout.list_vertical,null);

            TextView contentText = (TextView) view.findViewById(R.id.listContent);
            contentText.setText(Names[position]);

            return null;
        }
    }

}

main xml

<?xml version="1.0" encoding="utf-8"?>
<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="horizontal"
    tools:context=".MainActivity">



    <LinearLayout
        android:id="@+id/sideView"
        android:layout_weight=".20"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <ImageButton
            android:id="@+id/oneBtn"
            android:layout_width="@dimen/ButtonSize"
            android:layout_height="@dimen/ButtonSize"
            android:background="@android:color/transparent"
            android:src="@drawable/noct"
           />
        <ImageButton
            android:id="@+id/twoBtn"
            android:layout_width="@dimen/ButtonSize"
            android:layout_height="@dimen/ButtonSize"
            android:background="@android:color/transparent"
            android:src="@drawable/note"
            />
        <ImageButton
            android:id="@+id/threeBtn"
            android:layout_width="@dimen/ButtonSize"
            android:layout_height="@dimen/ButtonSize"
            android:background="@android:color/transparent"
            android:src="@drawable/journal"
            />
        <ImageButton
            android:id="@+id/fourBtn"
            android:layout_width="@dimen/ButtonSize"
            android:layout_height="@dimen/ButtonSize"
            android:background="@android:color/transparent"
            android:src="@drawable/mic"/>
        <ImageButton
            android:id="@+id/fiveBtn"
            android:layout_width="@dimen/ButtonSize"
            android:layout_height="@dimen/ButtonSize"
            android:background="@android:color/transparent"
            android:src="@drawable/add"
            />
        <ImageButton
            android:id="@+id/sixBtn"
            android:layout_width="@dimen/ButtonSize"
            android:layout_height="@dimen/ButtonSize"
            android:background="@android:color/transparent"
            android:src="@drawable/pencil"
            />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/mainView"
        android:layout_weight=".80"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical">

    </LinearLayout>
</LinearLayout>
2
  • adapter is not initialized.Please check and try again.......... Commented Oct 17, 2018 at 1:19
  • sorry i am new to java, how do i initialize adapter? Commented Oct 17, 2018 at 1:21

2 Answers 2

1

Do not return null in getView

public View getView(int position, View convertView, ViewGroup parent) { 
      View view = getLayoutInflater().inflate(R.layout.list_vertical,null); 
      TextView contentText = (TextView) view.findViewById(R.id.listContent);
     contentText.setText(Names[position]);
      return view; 
}
Sign up to request clarification or add additional context in comments.

Comments

0
public View getView(int position, View convertView, ViewGroup parent) { 
  convertView = getLayoutInflater().inflate(R.layout.list_vertical,null); 
  TextView contentText = (TextView) convertView.findViewById(R.id.listContent);
 contentText.setText(Names[position]);
  return convertView; 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.