我创建了简单的json数据传递用户的名字和姓氏,但是我只显示了一条记录但是我已经将for intent放入for循环这次多次活动创建和上面的循环put意图这次只显示一个创建。
列表大小是数据库表值的大小
像这样
FirstName LastName
Android corejava
Android corejava
Android corejava
Android corejava
**我的Json活动**
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fn = (EditText) findViewById(R.id.fn);
ln = (EditText) findViewById(R.id.ln);
insert = (Button) findViewById(R.id.insert1);
show = (Button) findViewById(R.id.button2);
requestQueue = Volley.newRequestQueue(getApplicationContext());
show.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
class GetDataJSON extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(
new BasicHttpParams());
HttpPost httppost = new HttpPost(
"http://192.168.2.5/Demo/lgin.php");
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient
.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream, "UTF-8"),
8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
} finally {
try {
if (inputStream != null)
inputStream.close();
} catch (Exception squish) {
}
}
return result;
}
@Override
protected void onPostExecute(String result) {
JSONObject jsonObj = null;
JSONArray jArray = null;
ArrayList<Model> data = new ArrayList<>();
try {
jsonObj = new JSONObject(result);
jArray = jsonObj.getJSONArray("userdetail");
Model model = new Model();
for (int i = 0; i < jArray.length(); i++) {
JSONObject jObject = null;
jObject = jArray.getJSONObject(i);
model.setName(jObject.getString("user"));
model.setLastname(jObject.getString("pass"));
Log.d(getClass().getName(), "User = " + model.getName() + "Pas = " + model.getLastname());
data.add(model);
}
Intent intent = new Intent(getApplicationContext(), ViewData.class);
intent.putExtra("data",new Gson().toJson(data));
startActivity(intent);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
});
insert.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
StringRequest request = new StringRequest(Request.Method.POST, insertURl, new Response.Listener<String>() {
@Override
public void onResponse(String arg0) {
// TODO Auto-generated method stub
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
}) {
@Override
protected Map<String, String> getParams()
throws AuthFailureError {
// TODO Auto-generated method stub
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("user", fn.getText().toString());
parameters.put("pass", ln.getText().toString());
Log.d(getClass().getName(), "View =" + fn.getText().toString() + "LastNAme = " + ln.getText().toString());
return parameters;
}
};
requestQueue.add(request);
}
});
}
我的Recyclerview类
public class ViewData extends Activity {
private AdapterFish mAdapter;
private List<Model> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.container_fish);
String dataString = getIntent().getStringExtra("data");
ArrayList<Model> list = new Gson().fromJson(dataString, new TypeToken<ArrayList<Model>>() {
}.getType());
mAdapter = new AdapterFish(ViewData.this, (ArrayList<Model>) list);
RecyclerView mRVFishPrice = (RecyclerView) findViewById(R.id.fishPriceList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
mRVFishPrice.setLayoutManager(mLayoutManager);
mRVFishPrice.setAdapter(mAdapter);
}
适配器
public class AdapterFish extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private LayoutInflater inflater;
ArrayList<Model> data;
Model current;
int currentPos = 0;
// create constructor to innitilize context and data sent from MainActivity
public AdapterFish(Context context, ArrayList<Model> data) {
this.context = context;
inflater = LayoutInflater.from(context);
this.data = data;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.customlayout, parent, false);
MyHolder holder = new MyHolder(view);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
MyHolder myHolder = (MyHolder) holder;
Model model = data.get(position);
myHolder.tvFirstName.setText(model.getName());
myHolder.tvLastName.setText(model.getLastname());
}
@Override
public int getItemCount() {
return data.size();
}
class MyHolder extends RecyclerView.ViewHolder {
TextView tvFirstName;
TextView tvLastName;
// create constructor to get widget reference
public MyHolder(View itemView) {
super(itemView);
tvFirstName = (TextView) itemView.findViewById(R.id.tvFirstName);
tvLastName = (TextView) itemView.findViewById(R.id.tvLastName);
}
}
}
答案 0 :(得分:0)
使用Gson将数组列表转换为String,然后将其传递给Intent。
Intent intent = new Intent(getApplicationContext(), ViewData.class);
intent.putExtra("data",new Gson().toJson(data));
startActivity(intent)
将字符串转换回列表
String dataString = getIntent().getStringExtra("data") ;
ArrayList<Model> list = new Gson().fromJson(dataString ,new TypeToken<ArrayList<Model>>(){}.getType());
在Gradle
compile 'com.google.code.gson:gson:2.7'
答案 1 :(得分:0)
如果要在Log Cat中获取数据,请使用以下方法传递ArrayList。
Intent intent = new Intent(YourCurrentActivity.this, ViewData.class);
intent.putStringArrayListExtra("data", data);
startActivity(intent);
在您的ViewData活动中,您需要执行
ArrayList<Model> list = new ArrayList<>();
list = getIntent().getStringArrayListExtra("data");
将列表传递给您的RecyclerView适配器类
mAdapter = new AdapterFish(ViewData.this, (ArrayList<Model>) list);
答案 2 :(得分:0)
试试这个:
Model model ;
for (int i = 0; i < jArray.length(); i++) {
model = new Model();
JSONObject jObject = null;
jObject = jArray.getJSONObject(i);
model.setName(jObject.getString("user"));
model.setLastname(jObject.getString("pass"));
Log.d(getClass().getName(), "User = " + model.getName() + "Pas = " + model.getLastname());
data.add(model);
}
或
Gson gson = new Gson();
data = gson.fromJson(response.toString(), new TypeToken<ArrayList<Model>>() {
}.getType());