l0veisreal
l0veisreal

Reputation: 179

Cannot populate ListView getView is not called

I am trying to populate a listview with input data i take from user.

AddPostPage is where i take data from user.I dont see any error messages and postList is not empty.Just getview is not called and i cant see any entry in listview.Since listview is not in addpostpages activity(its in another layout forum_entries.xml) maybe thats the problem ?

public class AddPostPage extends AppCompatActivity {

    String categoryName;
    private EditText titleInput;
    private EditText bodyInput;
    PostAdapter adapter;
     ListView postlistview;


    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.forum_user_post);
        categoryName = (String) getIntent().getSerializableExtra("incomingcategory");
        View forum_entries = getLayoutInflater().inflate(R.layout.forum_entries, null, false);
         postlistview = (ListView) forum_entries.findViewById(R.id.categoryPostsListView);

        Button add = (Button) findViewById(R.id.addButton);
        titleInput = (EditText) findViewById(R.id.titleInput);
        bodyInput =  (EditText) findViewById(R.id.bodyInput);
        add.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {

                Post post = new Post(titleInput.getText().toString(),bodyInput.getText().toString());

                if(categoryName.equals("CS 201")){
                    List<Post> postList = new ArrayList<>();
                    postList.add(post);
                    Log.d("test",postList.get(0).getTitle()); //working

                    adapter = new PostAdapter(getApplicationContext(), R.layout.post_row, postList);

                    postlistview.setAdapter(adapter);

        adapter.notifyDataSetChanged();



                }






            }
        });
    }


}

This is the PostAdapter:

public class PostAdapter extends ArrayAdapter<Post> {
    private List<Post> postList = new ArrayList<Post>();
    private Context context;

    private int layoutResource;

    public PostAdapter(Context context, int layoutResource, List<Post> posts) {
        super(context, layoutResource, posts);
        this.layoutResource = layoutResource;
        this.postList = posts;
        this.context = context;
        Log.d("adater",posts.get(0).getBody());
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
Log.d("getview","getview"); // not called.
        View view = convertView;

        if (view == null) {
            Log.d("bos","bos");
            LayoutInflater layoutInflater = LayoutInflater.from(getContext());
            view = layoutInflater.inflate(layoutResource, null);
        }

        Post posts = getItem(position);
Log.d("posts",posts.toString());

         Log.d("test","postlar bos degil");

            TextView postTitle = (TextView) view.findViewById(R.id.title);
            TextView postBody = (TextView) view.findViewById(R.id.body);
            postTitle.setText(posts.getTitle());
postBody.setText(posts.getBody());






        return view;
    }




}

Post Class:

public class Post {


    String title;
    String body;

    public Post(String title,String body){

        this.title = title;
        this.body = body;


    }
    public String getTitle() {
        return title;
    }

    public String getBody() {
        return body;
    }


}

post_row.xml :

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="14dp"
        android:layout_marginStart="14dp"
        android:layout_marginTop="20dp"
        android:text="" />

    <TextView
        android:id="@+id/body"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/title"
        android:layout_alignStart="@+id/title"
        android:layout_below="@+id/title"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="30dp"
        android:layout_marginTop="15dp"
        android:text="" />
</RelativeLayout>

forum_entries.xml (listview that i am trying to populate is here)

<RelativeLayout
    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">

    <ListView
        android:id="@+id/categoryPostsListView"
        android:layout_width="339dp"
        android:layout_height="382dp"
        android:layout_below="@+id/categoryName"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="26dp"></ListView>


    <Button
        android:id="@+id/add_entry_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/categoryPostsListView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="17dp"
        android:text="Add Entry"
        />

    <TextView
        android:id="@+id/categoryName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="17dp"
        android:text="TextView"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="16dp" />
</RelativeLayout>

Upvotes: 1

Views: 44

Answers (1)

Levi Moreira
Levi Moreira

Reputation: 12005

You're setting a different layout in your onCreate method that the one where the listview is:

super.onCreate(savedInstanceState);
        setContentView(R.layout.forum_user_post);
        categoryName = (String) getIntent().getSerializableExtra("incomingcategory");
        View forum_entries = getLayoutInflater().inflate(R.layout.forum_entries, null, false);
         postlistview = (ListView) forum_entries.findViewById(R.id.categoryPostsListView);

The layout inflated by the activity is forum_user_post.xml, but your list view is in forum_entries.xml aparently. Your activity had another layout so that's why you were not seeing the listview.

Try setting the correct layout:

setContentView(R.layout.forum_entries);
postlistview = (ListView)findViewById(R.id.categoryPostsListView);

Or simply bring your listview to forum_user_post.xml as it's where most of your views are. If you do that, then use:

setContentView(R.layout.forum_user_post);
postlistview = (ListView)findViewById(R.id.categoryPostsListView);

If you want to keep the list in another layout file, no problem, but you need to include it in the main layout file, use the include tag inside the forum_user_post.xml file:

 <include layout="@layout/forum_entries"/>

Upvotes: 1

Related Questions