我是android编程的新手。我正在开发一个Android移动应用程序,其中数据将来自mysql数据库,并且基于登录用户,我想将textview向右移动,并在左侧移动值。现在我只想在右边移动“rahul”值。
JSON值:
[{
"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"
}]
目前正在发生的事情,所有数据都在列表视图的左侧。寻找一个解决方案,我可以将“rahul”用户名值移动到右侧,就像聊天应用程序一样。
显示值的CODE:
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);
}
}
答案 0 :(得分:1)
您需要为列表视图创建自定义适配器。在getView()
方法中,您需要检查用户名,并将重力分配给左/右,或将用户名分配给正确的TextView
。
这是一个单独的SO问题,引导您完成制作自定义适配器的步骤。
{{3}}