Создание Listview программно без xml: не отображается ни одна ячейка - PullRequest
0 голосов
/ 17 октября 2018

Я пытаюсь создать просмотр списка программно в Android.

Моя цель - создать динамическое представление, в котором, если я нажму одну кнопку, это изменит содержимое внутри макета из списка в другое представление или наоборот.

Все выглядит хорошо, я успешно поместил просмотр списка в линейный макет из файла Java, и я сделал это, создав собственный макет для ячейки.

Проблема в том, что он показывает просмотр списка, но этоне добавляет ячейку.Я знаю, что он помещает представление списка, потому что фон становится зеленым, когда я запускаю его, но он не показывает ячейку списка.

Я что-то упустил?

<?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 ]

0 голосов
/ 17 октября 2018
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; 
}
0 голосов
/ 17 октября 2018

Не возвращать ноль в 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; 
}
...