Android 列表中的最后一项<;对象>;在RecyclerView项的特定文本视图中重复
我使用对象类(Android 列表中的最后一项<;对象>;在RecyclerView项的特定文本视图中重复,android,json,dataset,android-recyclerview,Android,Json,Dataset,Android Recyclerview,我使用对象类(publicstaticlist res;)设置从服务器获取json数据的数据。应用程序的流程如下所示 步骤1:(MainActivity.class) 从服务器获取数据 第二步: private class TabNameSync extends AsyncTask<Void, Void, String> { String BASE_URL = Config.DATA_URL; ProgressDialog nDialog;
publicstaticlist res;
)设置从服务器获取json数据的数据。应用程序的流程如下所示
步骤1:(MainActivity.class)
从服务器获取数据
第二步:
private class TabNameSync extends AsyncTask<Void, Void, String> {
String BASE_URL = Config.DATA_URL;
ProgressDialog nDialog;
HashMap<String, String> hashMapPost = new HashMap<>();
@Override
protected void onPreExecute() {
nDialog = new ProgressDialog(MainActivity.this);
nDialog.setMessage("Loading...");
nDialog.setIndeterminate(true);
nDialog.setCancelable(true);
nDialog.show();
}
@Override
protected String doInBackground(Void... params) {
HttpURLConnection con = null;
InputStream is = null;
StringBuffer buffer;
String bufferVariable = null;
hashMapPost.put("tag", "onload");
hashMapPost.put("lat", "18.71851808");
hashMapPost.put("log", "97.74131474");
try {
con = (HttpURLConnection) (new URL(BASE_URL)).openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestMethod("POST");
con.connect();
OutputStream os = con.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(commonUtil.getPostDataString(hashMapPost));
writer.flush();
writer.close();
os.close();
buffer = new StringBuffer();
int responseCode = con.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
is = con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null)
buffer.append(line).append("\r\n");
is.close();
}
con.disconnect();
bufferVariable = buffer.toString();
return buffer.toString();
} catch (Throwable t) {
t.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (Throwable t) {
}
try {
if (con != null) {
con.disconnect();
}
} catch (Throwable t) {
}
if (!bufferVariable.equalsIgnoreCase(" ")) {
try {
JSONArray jArray = new JSONArray(bufferVariable);
int jsonLength = jArray.length();
for (int j = 0; j < jArray.length(); j++) {
Restaurant_Beam item;
JSONObject jsonObj = jArray.getJSONObject(j);
item = new Restaurant_Beam();
item.setIG_LIKECOUNT(jsonObj.getInt(Config.LIKE));
item.setIG_DELIVERY(jsonObj.getInt(Config.DELIVERYTIME));
item.setIG_PRODUCTID(jsonObj.getString(Config.PRODUCTID));
item.setIG_SALES_PRICE(jsonObj.getString(Config.SALESPRICE));
item.setIG_VOUCHER_ID(jsonObj.getString(Config.VOUCHERID));
item.setIG_VOUCHEROFFER(jsonObj.getString(Config.VOUCHEROFFER));
item.setIG_CATEGORY_ID(jsonObj.getString(Config.CATEID));
item.setIG_IMAGEURL(jsonObj.getString(Config.IMGID));
item.setIG_PRODUCTNAME(jsonObj.getString(Config.PRODUCTNAME));
item.setIG_CATEGORYNAME(jsonObj.getString(Config.CATENAME));
Restaurant.add(item);
listSuperHeroes = Restaurant;
strTabName = jsonObj.getString("Cate Name");
String strProductID = jsonObj.getString("Pro_Id");
String strProductName = jsonObj.getString("Product_Name");
String strSalesPrice = jsonObj.getString("Sales Price");
String strVoucherId = jsonObj.getString("Voucher Id");
String strVoucherOffer = jsonObj.getString("voucher Offer");
String strCatId = jsonObj.getString("Cat Id");
String strImage = jsonObj.getString("Image");
String strLikes = jsonObj.getString("likes");
String strDeliveryTime = jsonObj.getString("deliverytime");
strDbName = jsonObj.getString("dbname");
tabName.add(strTabName);
DbName.add(strDbName);
if (jsonLength == jArray.length()) {
JSONObject jsonObj2 = jArray.getJSONObject(jsonLength - 1);
Config.IMAGE_URL = jsonObj2.getString("url");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
values = new ArrayList<String>();
values = tabName;
HashSet<String> hashSet = new HashSet<String>();
hashSet.addAll(values);
values.clear();
values.addAll(hashSet);
nDialog.dismiss();
// setTime_ToSlide();
viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
SharedPreferences.Editor editor = pref.edit();
editor.putString("FIRST_TAB", values.get(1));
editor.putString("SECOND_TAB", values.get(0));
editor.putString("THIRD_TAB", values.get(2));
editor.commit();
}
}
将所有Json数据设置为List
class。并在MainActivity中将列表
声明为公共静态
第三步:
private class TabNameSync extends AsyncTask<Void, Void, String> {
String BASE_URL = Config.DATA_URL;
ProgressDialog nDialog;
HashMap<String, String> hashMapPost = new HashMap<>();
@Override
protected void onPreExecute() {
nDialog = new ProgressDialog(MainActivity.this);
nDialog.setMessage("Loading...");
nDialog.setIndeterminate(true);
nDialog.setCancelable(true);
nDialog.show();
}
@Override
protected String doInBackground(Void... params) {
HttpURLConnection con = null;
InputStream is = null;
StringBuffer buffer;
String bufferVariable = null;
hashMapPost.put("tag", "onload");
hashMapPost.put("lat", "18.71851808");
hashMapPost.put("log", "97.74131474");
try {
con = (HttpURLConnection) (new URL(BASE_URL)).openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestMethod("POST");
con.connect();
OutputStream os = con.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(commonUtil.getPostDataString(hashMapPost));
writer.flush();
writer.close();
os.close();
buffer = new StringBuffer();
int responseCode = con.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
is = con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null)
buffer.append(line).append("\r\n");
is.close();
}
con.disconnect();
bufferVariable = buffer.toString();
return buffer.toString();
} catch (Throwable t) {
t.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (Throwable t) {
}
try {
if (con != null) {
con.disconnect();
}
} catch (Throwable t) {
}
if (!bufferVariable.equalsIgnoreCase(" ")) {
try {
JSONArray jArray = new JSONArray(bufferVariable);
int jsonLength = jArray.length();
for (int j = 0; j < jArray.length(); j++) {
Restaurant_Beam item;
JSONObject jsonObj = jArray.getJSONObject(j);
item = new Restaurant_Beam();
item.setIG_LIKECOUNT(jsonObj.getInt(Config.LIKE));
item.setIG_DELIVERY(jsonObj.getInt(Config.DELIVERYTIME));
item.setIG_PRODUCTID(jsonObj.getString(Config.PRODUCTID));
item.setIG_SALES_PRICE(jsonObj.getString(Config.SALESPRICE));
item.setIG_VOUCHER_ID(jsonObj.getString(Config.VOUCHERID));
item.setIG_VOUCHEROFFER(jsonObj.getString(Config.VOUCHEROFFER));
item.setIG_CATEGORY_ID(jsonObj.getString(Config.CATEID));
item.setIG_IMAGEURL(jsonObj.getString(Config.IMGID));
item.setIG_PRODUCTNAME(jsonObj.getString(Config.PRODUCTNAME));
item.setIG_CATEGORYNAME(jsonObj.getString(Config.CATENAME));
Restaurant.add(item);
listSuperHeroes = Restaurant;
strTabName = jsonObj.getString("Cate Name");
String strProductID = jsonObj.getString("Pro_Id");
String strProductName = jsonObj.getString("Product_Name");
String strSalesPrice = jsonObj.getString("Sales Price");
String strVoucherId = jsonObj.getString("Voucher Id");
String strVoucherOffer = jsonObj.getString("voucher Offer");
String strCatId = jsonObj.getString("Cat Id");
String strImage = jsonObj.getString("Image");
String strLikes = jsonObj.getString("likes");
String strDeliveryTime = jsonObj.getString("deliverytime");
strDbName = jsonObj.getString("dbname");
tabName.add(strTabName);
DbName.add(strDbName);
if (jsonLength == jArray.length()) {
JSONObject jsonObj2 = jArray.getJSONObject(jsonLength - 1);
Config.IMAGE_URL = jsonObj2.getString("url");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
values = new ArrayList<String>();
values = tabName;
HashSet<String> hashSet = new HashSet<String>();
hashSet.addAll(values);
values.clear();
values.addAll(hashSet);
nDialog.dismiss();
// setTime_ToSlide();
viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
SharedPreferences.Editor editor = pref.edit();
editor.putString("FIRST_TAB", values.get(1));
editor.putString("SECOND_TAB", values.get(0));
editor.putString("THIRD_TAB", values.get(2));
editor.commit();
}
}
这里我需要将List
类值填充到Recyclerview中。由于声明为public static,我在将MainActivity作为父项的片段中设置适配器时直接调用了它
adapter = new CardAdapter(MainActivity.res, context);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
但问题是,在填充RecyclerView时,我只得到最后一项重复到列表的大小。
大小。
MainActivity(异步任务方法):
private class TabNameSync extends AsyncTask<Void, Void, String> {
String BASE_URL = Config.DATA_URL;
ProgressDialog nDialog;
HashMap<String, String> hashMapPost = new HashMap<>();
@Override
protected void onPreExecute() {
nDialog = new ProgressDialog(MainActivity.this);
nDialog.setMessage("Loading...");
nDialog.setIndeterminate(true);
nDialog.setCancelable(true);
nDialog.show();
}
@Override
protected String doInBackground(Void... params) {
HttpURLConnection con = null;
InputStream is = null;
StringBuffer buffer;
String bufferVariable = null;
hashMapPost.put("tag", "onload");
hashMapPost.put("lat", "18.71851808");
hashMapPost.put("log", "97.74131474");
try {
con = (HttpURLConnection) (new URL(BASE_URL)).openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestMethod("POST");
con.connect();
OutputStream os = con.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(commonUtil.getPostDataString(hashMapPost));
writer.flush();
writer.close();
os.close();
buffer = new StringBuffer();
int responseCode = con.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
is = con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null)
buffer.append(line).append("\r\n");
is.close();
}
con.disconnect();
bufferVariable = buffer.toString();
return buffer.toString();
} catch (Throwable t) {
t.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (Throwable t) {
}
try {
if (con != null) {
con.disconnect();
}
} catch (Throwable t) {
}
if (!bufferVariable.equalsIgnoreCase(" ")) {
try {
JSONArray jArray = new JSONArray(bufferVariable);
int jsonLength = jArray.length();
for (int j = 0; j < jArray.length(); j++) {
Restaurant_Beam item;
JSONObject jsonObj = jArray.getJSONObject(j);
item = new Restaurant_Beam();
item.setIG_LIKECOUNT(jsonObj.getInt(Config.LIKE));
item.setIG_DELIVERY(jsonObj.getInt(Config.DELIVERYTIME));
item.setIG_PRODUCTID(jsonObj.getString(Config.PRODUCTID));
item.setIG_SALES_PRICE(jsonObj.getString(Config.SALESPRICE));
item.setIG_VOUCHER_ID(jsonObj.getString(Config.VOUCHERID));
item.setIG_VOUCHEROFFER(jsonObj.getString(Config.VOUCHEROFFER));
item.setIG_CATEGORY_ID(jsonObj.getString(Config.CATEID));
item.setIG_IMAGEURL(jsonObj.getString(Config.IMGID));
item.setIG_PRODUCTNAME(jsonObj.getString(Config.PRODUCTNAME));
item.setIG_CATEGORYNAME(jsonObj.getString(Config.CATENAME));
Restaurant.add(item);
listSuperHeroes = Restaurant;
strTabName = jsonObj.getString("Cate Name");
String strProductID = jsonObj.getString("Pro_Id");
String strProductName = jsonObj.getString("Product_Name");
String strSalesPrice = jsonObj.getString("Sales Price");
String strVoucherId = jsonObj.getString("Voucher Id");
String strVoucherOffer = jsonObj.getString("voucher Offer");
String strCatId = jsonObj.getString("Cat Id");
String strImage = jsonObj.getString("Image");
String strLikes = jsonObj.getString("likes");
String strDeliveryTime = jsonObj.getString("deliverytime");
strDbName = jsonObj.getString("dbname");
tabName.add(strTabName);
DbName.add(strDbName);
if (jsonLength == jArray.length()) {
JSONObject jsonObj2 = jArray.getJSONObject(jsonLength - 1);
Config.IMAGE_URL = jsonObj2.getString("url");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
values = new ArrayList<String>();
values = tabName;
HashSet<String> hashSet = new HashSet<String>();
hashSet.addAll(values);
values.clear();
values.addAll(hashSet);
nDialog.dismiss();
// setTime_ToSlide();
viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
SharedPreferences.Editor editor = pref.edit();
editor.putString("FIRST_TAB", values.get(1));
editor.putString("SECOND_TAB", values.get(0));
editor.putString("THIRD_TAB", values.get(2));
editor.commit();
}
}
私有类TabNameSync扩展异步任务{
字符串BASE\u URL=Config.DATA\u URL;
恩迪亚洛;
HashMap hashMapPost=新HashMap();
@凌驾
受保护的void onPreExecute(){
nDialog=新建进度对话框(MainActivity.this);
nDialog.setMessage(“加载…”);
nDialog.SetUndeterminate(真);
nDialog.setCancelable(真);
nDialog.show();
}
@凌驾
受保护字符串doInBackground(无效…参数){
HttpURLConnection con=null;
InputStream=null;
字符串缓冲区;
字符串缓冲变量=null;
hashMapPost.put(“tag”、“onload”);
hashMapPost.put(“lat”,“18.71851808”);
hashMapPost.put(“log”,“97.74131474”);
试一试{
con=(HttpURLConnection)(新URL(BASE_URL)).openConnection();
con.setDoInput(真);
con.设置输出(真);
con.setRequestMethod(“POST”);
con.connect();
OutputStream os=con.getOutputStream();
BufferedWriter=新的BufferedWriter(
新的OutputStreamWriter(操作系统,“UTF-8”);
write(commonUtil.getPostDataString(hashMapPost));
writer.flush();
writer.close();
os.close();
buffer=新的StringBuffer();
int responseCode=con.getResponseCode();
if(responseCode==HttpsURLConnection.HTTP\u确定){
is=con.getInputStream();
BufferedReader br=新的BufferedReader(新的InputStreamReader(is));
弦线;
而((line=br.readLine())!=null)
buffer.append(line.append(“\r\n”);
is.close();
}
con.disconnect();
bufferVariable=buffer.toString();
返回buffer.toString();
}捕获(可丢弃的t){
t、 printStackTrace();
}最后{
试一试{
如果(is!=null){
is.close();
}
}捕获(可丢弃的t){
}
试一试{
如果(con!=null){
con.disconnect();
}
}捕获(可丢弃的t){
}
if(!bufferVariable.equalsIgnoreCase(“”){
试一试{
JSONArray jArray=新的JSONArray(缓冲变量);
int jsonLength=jArray.length();
对于(int j=0;jpublic static String ProductName;
public String ProductName;