0

I want to show the latest post in the top row. How can I do this on android. I am using mysql- php as webservis.

  I'm trying to make twitter clonu. last post, needs to be seen in the top row.

Github Project

https://github.com/prensmiskin/vollley-one/tree/master/volleydenemetwo/app/src/main/java/com/example/volleydenemetwo

public void kisiekle(){
        String url = "https://goldgym.pro/akis.php";
        final String akiss = akis.getText().toString().trim();


        StringRequest istek = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.e("Cevap",response);

                try{
                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray movies = jsonObject.getJSONArray("employees");




                    StringBuilder sb = new StringBuilder();

                  for (int i=0; i < movies.length();i++){
                        JSONObject b = movies.getJSONObject(i);

                       String akis_ad = b.getString("akis");


                      akisone.append(akis_ad + ","  + "\n\n");





                    }

web Service / mysql - php
twit https://github.com/prensmiskin/vollley-one/blob/master/akis.php
login https://github.com/prensmiskin/vollley-one/blob/master/login.php register
https://github.com/prensmiskin/vollley-one/blob/master/myphp.php

screenshot https://user-images.githubusercontent.com/40228440/66035919-bb89fd00-e514-11e9-8151-ebba56fc7670.png

  • This sounds more of a conceptual question. On first load you should load set of records sorted by latest datetime, after that you need some PubSub like event driven service eg; Websocket client(web, android activity etc) will get notified on arrival of new message, you subscribe to topic asynchronously and push it to top of UI. Are you getting any particular issue/error ? – NarendraSoni Oct 02 '19 at 10:25
  • How to write text in android. ? The answer would be to use textview if I asked this question. Can you give me a concrete answer to my question? – Oğuzhan GÜMÜŞ Oct 02 '19 at 10:55

2 Answers2

1

If you mean that it should be sorted newest (top) -> oldest (bottom). You can sort your collection based on the date of the post. Here is a stackoverflow question about it.

If you mean only show it in some top bar and not after that. Sort the list as you want it (or if its pre-sorted no need). Take the first item, do you displaying and remove it after:

public void displayItems(List<Posts> posts) {
    Collections.sort(posts, new Comparator<Post>() {
        @Override
        public int compare(Post o1, Post o2) {
            //You could also make Post implement Comparable. Then the Comparator is not needed.
            return o1.getDatePosted().compareTo(o2.getDatePosted())
        }
    });
    Post firstItem = posts.get(0);
    posts.remove(0);
    // Continue doing magic
}

I find it a bit difficult to figure out what you are asking specifically as to me the code seems unrelated to the question you ask. Couple of tips:

  1. Look into GSON for JSON (de)serialization (or the newer moshi)
  2. Read the java style guide (Google or Oracle) (as someone with a bit of java experience under my belt and pretty much no php, I find you code a bit difficult to read)
  3. When making a post for Android its nice to know you minSdk as that can make a big deal
  4. Also when asking about Android it would be nice to know more about the layout you use.
1

I solved it by making an improvement to the web service side.

$statement = $conn->prepare("SELECT akis FROM tabletwo ORDER BY id DESC");

I know my question was related to android but solved in this way.