简体   繁体   中英

How to align the textView to right or left based on the values from database

i am new to android programing. i am developing a android mobile application where data will be coming from mysql database and on the based on the logged in user, i want to shift the textview to the right and rest values on the left. For now i am only trying to move the "rahul" values on the right.

JSON Values:

[{
    "user": "varun",
    "message": "hello word, my first message "
}, {
    "user": "varun",
    "message": "hello word, my first message "
}, {
    "user": "rahul",
    "message": "hello world"
}, {
    "user": "rahul",
    "message": "world: its cool"
}, {
    "user": "rahul",
    "message": "awesom"
}, {
    "user": "rahul",
    "message": "ranom"
}, {
    "user": "rahul",
    "message": "randimagain"
}, {
    "user": "rahi",
    "message": "is it me again"
}, {
    "user": "rahull",
    "message": "hi rahul"
}, {
    "user": "rahul",
    "message": "world, am bacj"
}, {
    "user": "rahul",
    "message": "hello parents"
}, {
    "user": "",
    "message": ""
}, {
    "user": "",
    "message": ""
}, {
    "user": "",
    "message": ""
}, {
    "user": "",
    "message": ""
}, {
    "user": "",
    "message": ""
}, {
    "user": "varun",
    "message": "google world"
}, {
    "user": "rahul",
    "message": "ollaaa"
}, {
    "user": "rahul",
    "message": "ollaaa"
}, {
    "user": "rahul",
    "message": "where are others"
}, {
    "user": "rahul",
    "message": "where are others"
}, {
    "user": "varun",
    "message": "oil spil"
}, {
    "user": "varun",
    "message": "phone"
}, {
    "user": "vatun",
    "message": "tttt"
}, {
    "user": "varun",
    "message": "poiuytrew"
}, {
    "user": "rahui",
    "message": "yregh"
}, {
    "user": "varun",
    "message": "huiokgddfv"
}, {
    "user": "rahi",
    "message": "poiutghklnbfd"
}, {
    "user": "rahui",
    "message": "qwerty"
}, {
    "user": "varub",
    "message": "hi eat pineapple"
}, {
    "user": "varun",
    "message": "hi prabh"
}, {
    "user": "rahul",
    "message": "hi world"
}, {
    "user": "ggjdjjf",
    "message": "yhvdhhj"
}, {
    "user": "tgggg",
    "message": "hhhh"
}, {
    "user": "rahul",
    "message": "lqvrl"
}, {
    "user": "hhhh",
    "message": "ghh"
}, {
    "user": "tff",
    "message": "gg"
}, {
    "user": "hfdhn",
    "message": "hnnnme"
}, {
    "user": "popo",
    "message": "plpl"
}, {
    "user": "rahulpop",
    "message": "op music"
}, {
    "user": "rtrt",
    "message": "tyty"
}, {
    "user": "hlpo",
    "message": "opop"
}, {
    "user": "hlpoghhj",
    "message": "opop"
}, {
    "user": "tt",
    "message": "hhh"
}, {
    "user": "ttere",
    "message": "hhh"
}, {
    "user": "gytgjyg",
    "message": "lajdhd"
}, {
    "user": "topo",
    "message": "ptpt"
}, {
    "user": "jgj",
    "message": "rtr"
}, {
    "user": "ry",
    "message": "yr"
}, {
    "user": "ry",
    "message": "yr"
}, {
    "user": "y",
    "message": "q"
}, {
    "user": "u",
    "message": "t"
}, {
    "user": "tt",
    "message": "pp"
}, {
    "user": "q",
    "message": "z"
}, {
    "user": "w",
    "message": "v"
}, {
    "user": "e",
    "message": "e."
}, {
    "user": "ui",
    "message": "ui"
}, {
    "user": "yi",
    "message": "yi"
}, {
    "user": "gg",
    "message": "g"
}, {
    "user": "e",
    "message": "e"
}, {
    "user": "h",
    "message": "hh"
}, {
    "user": "j",
    "message": "j"
}, {
    "user": "j",
    "message": "k"
}, {
    "user": "v",
    "message": "v"
}, {
    "user": "ty",
    "message": "ewr"
}] 

currently what is happeing, all the data is coming on the left side in a listview. Looking for a solution where i can move the "rahul" username values to the right like a chat application.

COde for displaying values:

package com.weavearound.schools.weavearound;

import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.weavearound.schools.weavearound.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class ListViewNew extends AppCompatActivity {

    EditText username, messsage;
    Button insert, show;
    RequestQueue requestQueue1;
    String insertUrl = "http://www.schools.weavearound.com/chatdb_api/insert.php";
    TextView value;

    ArrayAdapter<String> adapter;
    ArrayList<String> items;

    //ArrayAdapter<String> adapter1;
    //ArrayList<String> items1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        username = (EditText) findViewById(R.id.editText3);
        messsage = (EditText) findViewById(R.id.editText2);
        insert = (Button) findViewById(R.id.button);
        show = (Button) findViewById(R.id.button2);



        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blabla);
        ListView listView = (ListView) findViewById(R.id.listv);



        items = new ArrayList<String>();
        adapter = new ArrayAdapter(this, R.layout.item_layout, R.id.txt, items);
        listView.setAdapter(adapter);

        //items1 = new ArrayList<String>();
        //adapter1 = new ArrayAdapter(this, R.layout.item_layout, R.id.txtright, items1);

        //listView.setAdapter(adapter1);

    }
        //listView.setBackgroundResource(R.drawable.border);


    public void onStart() {
        super.onStart();
        // Create request queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        //  Create json array request
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, "http://www.schools.weavearound.com/chatdb_api/showback.php", new Response.Listener<JSONArray>() {
            public void onResponse(JSONArray jsonArray) {

                String data = "";
                String message = "";
                String result="";
                String value ="";
                // Successfully download json
                // So parse it and populate the listview

                final String TAG = events.class.getSimpleName();
                Log.d(TAG, "showJSON: \n" + jsonArray);

                for (int i = 0; i < jsonArray.length(); i++) {

                    try {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);


                    data = jsonObject.getString("user");
                    //items.add(jsonObject.getString("user"));
                    //message+= items.add(jsonObject.getString("message"));
                    message = jsonObject.getString("message");
                    result = data + "\n"+ message;

                      items.add(result);

                   // Log.d(TAG, "username: \n" + (data.equals("rahul")));



                     //   if (data.equals("rahul"))
                       // {

//                            items.add(result);







  //                      }





                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }


                adapter.notifyDataSetChanged();
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("Error", "Unable to parse json array");
            }
        });
        // add json array request to the request queue
        requestQueue.add(jsonArrayRequest);



    }
}

You'll need to create a custom adapter for your listview. In your getView() method you check for the username and either assign the gravity to left/right or assign the username to the proper TextView .

Here is a seperate SO questions the walks you through the steps of making a custom adapter.

Custom Adapter for List View

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM