我从webservices获取数据比我必须在recyclerView中显示这些数据...我在asyncTest的AsyncTask中设置我的recyclelerView因为我必须首先获取数据。
问题是我的活动中没有View,每次收到此消息...
这是我的Main_activity ......
public class Store_Items_Display extends AppCompatActivity
{
public String s;
private RecyclerView recyclerView;
private DataAdapter dataAdapter;
private ArrayList<String> arrayList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store__items__display);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recyclerView = (RecyclerView) findViewById(R.id.recycleView);
dataAdapter = new DataAdapter(arrayList);
Log.i("ArrayList size :::", String.valueOf(arrayList.size()));
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(dataAdapter);
new Connection().execute();
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if (bundle != null)
{
s = (String) bundle.get("userName");
Log.i("Oncreate UserName ::: ", "Hamza");
}
else
{
Log.i("Data in intent ::;", "is Null");
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
Button submitButton = (Button) findViewById(R.id.button8);
submitButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(Store_Items_Display.this, "Submitted", Toast.LENGTH_SHORT).show();
}
});
}
public boolean onOptionsItemSelected(MenuItem item)
{
Intent myIntent = new Intent(getApplicationContext(), StoreFirstActivity.class);
startActivityForResult(myIntent, 0);
return true;
}
class Connection extends AsyncTask<Void, Void, Void>
{
StringBuilder result = new StringBuilder();
final String TAG = "Items in ArrayList :::";
@Override
protected void onPreExecute()
{
super.onPreExecute();
Toast.makeText(Store_Items_Display.this, "Welcome", Toast.LENGTH_LONG);
}
@Override
protected Void doInBackground(Void... params)
{
try
{
Log.i("User Name ::: ", "Hamza");
URL url = new URL("http://10.0.3.2/FixItApp/GetData.php?user_name=khan"); // need changes here...
HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(httpUrlConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null)
{
result.append(line);
}
in.close();
reader.close();
if (!result.toString().equals(""))
{
JSONObject jsonObject = new JSONObject(result.toString());
JSONArray jsonArray = new JSONArray(jsonObject.getString("user_data"));
for (int i = 0; i < jsonArray.length(); i++)
{
String serial = jsonArray.getJSONObject(i).getString("serial_number");
String items = jsonArray.getJSONObject(i).getString("items");
String brand = jsonArray.getJSONObject(i).getString("brand");
String price = jsonArray.getJSONObject(i).getString("price");
arrayList.add(serial);
arrayList.add(items);
arrayList.add(brand);
arrayList.add(price);
Log.i(TAG, serial);
Log.i(TAG, arrayList.toString());
Log.i("ArrayList Size :::", String.valueOf(arrayList.size()));
}
}
else
{
Toast.makeText(Store_Items_Display.this, "No data found of this name", Toast.LENGTH_LONG);
}
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid)
{
super.onPostExecute(aVoid);
dataAdapter.notifyDataSetChanged();
}
}
}
这是我的DataAdapter类......
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.MyViewHolder>
{
private ArrayList<String> dataList;
public DataAdapter(ArrayList<String> arrayList)
{
this.dataList = arrayList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.data_view, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position)
{
String items = dataList.get(position);
holder.serial.setText(items);
}
@Override
public int getItemCount()
{
return dataList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder
{
public TextView serial;
public TextView items;
public TextView brand;
public TextView price;
public MyViewHolder(View itemView)
{
super(itemView);
serial = (TextView) itemView.findViewById(R.id.serial);
items = (TextView) itemView.findViewById(R.id.items);
brand = (TextView) itemView.findViewById(R.id.brand);
price = (TextView) itemView.findViewById(R.id.price);
}
}
}
答案 0 :(得分:1)
代码在某些方面很糟糕。如果您每次获取数据时都设置了新适配器,那么notifyDataSetChanged()
呼叫就没有意义了。但事实上你应该做的是替换数据,而不是适配器,然后只需在现有适配器上调用dataAdapter.notifyDataSetChanged();
,让它知道数据已经改变。