我正在从我通过REST制作的网站上检索数据。我有一个自定义布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_item_custom_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.marvinjason.art.ItemCustomLayout">
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/srl_activityItem">
</android.support.v4.widget.SwipeRefreshLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
app:srcCompat="@color/cardview_dark_background"
android:id="@+id/imageView1"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_height="150dp"
android:layout_width="180dp" />
<ImageView
app:srcCompat="@color/cardview_dark_background"
android:id="@+id/imageView3"
android:layout_marginEnd="11dp"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<ImageView
app:srcCompat="@color/cardview_dark_background"
android:id="@+id/imageView2"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/imageView5"
android:layout_marginEnd="11dp" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/itemName"
android:layout_marginTop="47dp"
android:id="@+id/itemDescription"
android:textSize="16sp" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="176dp"
android:id="@+id/itemName"
android:textSize="20sp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<ImageView
app:srcCompat="@color/cardview_dark_background"
android:id="@+id/imageView4"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignStart="@+id/imageView2" />
<ImageView
app:srcCompat="@color/cardview_dark_background"
android:id="@+id/imageView5"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignTop="@+id/imageView4"
android:layout_alignStart="@+id/imageView3" />
<TextView
android:text="TextView"
android:layout_height="wrap_content"
android:id="@+id/itemPrice"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_alignBaseline="@+id/itemName"
android:layout_alignBottom="@+id/itemName"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</ScrollView>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="105dp"
android:id="@+id/itemSeller"
android:layout_alignParentBottom="true"
android:textSize="16sp" />
我正在做的是从网站链接中检索数据并将它们传递给EditTexts和ImageViews但是会发生什么是数据检索成功(我知道这是因为我有一个片段,它在那里工作正常)但是当我设置内容时它什么也没显示。我的猜测是将Edittexts的设置放在方法中,但我不知道该怎么做。只是附加信息:在开始时,有一个片段类。它包含项目的列表视图,一旦点击它应该显示项目的详细信息(这是这个类)这是我的活动:
public class ItemCustomLayout extends AppCompatActivity {
private SwipeRefreshLayout swipeRefreshLayout;
private JSONArray jsonArray;
private static Item items;
private static boolean isPremium = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_custom_layout);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.srl_activityItem);
final SwipeRefreshLayout.OnRefreshListener onRefreshListener = new SwipeRefreshLayout.OnRefreshListener()
{
@Override
public void onRefresh()
{
if (!Utility.isConnected(ItemCustomLayout.this))
{
Toast.makeText(ItemCustomLayout.this, "No internet connection!", Toast.LENGTH_LONG).show();
}
fetchData();
}
};
swipeRefreshLayout.setOnRefreshListener(onRefreshListener);
swipeRefreshLayout.post(new Runnable()
{
@Override
public void run()
{
swipeRefreshLayout.setRefreshing(true);
onRefreshListener.onRefresh();
}
});
Picasso.with(ItemCustomLayout.this).load(items.imageUrl).into(((ImageView) findViewById(R.id.imageView1)));
Picasso.with(ItemCustomLayout.this).load(items.imageUrl).into(((ImageView) findViewById(R.id.imageView2)));
Picasso.with(ItemCustomLayout.this).load(items.imageUrl).into(((ImageView) findViewById(R.id.imageView3)));
Picasso.with(ItemCustomLayout.this).load(items.imageUrl).into(((ImageView) findViewById(R.id.imageView4)));
Picasso.with(ItemCustomLayout.this).load(items.imageUrl).into(((ImageView) findViewById(R.id.imageView5)));
((TextView) findViewById(R.id.itemName)).setText(items.name);
((TextView) findViewById(R.id.itemPrice)).setText(String.format("Php %.2f", items.price));
((TextView) findViewById(R.id.itemDescription)).setText(items.description);
((TextView) findViewById(R.id.itemSeller)).setText(items.seller);
}
public void fetchData()
{
new AsyncTask(){
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Object doInBackground(Object[] objects) {
RestApi rest = new RestApi(ItemCustomLayout.this);
jsonArray = rest.fetchJSONArray(*link*);
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
try {
items = new Item();
int id = (int) getIntent().getExtras().get("id");
items.imageUrl = jsonArray.getJSONObject(id).getString("image_url");
items.name = jsonArray.getJSONObject(id).getString("name");
items.price = Double.parseDouble(jsonArray.getJSONObject(id).getString("price"));
items.description = jsonArray.getJSONObject(id).getString("description");
items.seller = "Company A";
}
catch (Exception ex)
{
Log.d("Error", ex.toString());
}
swipeRefreshLayout.setRefreshing(false);
}
}.execute();
}
private class Item
{
public int id;
public String name;
public double price;
public String description;
public String seller;
public String imageUrl;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (isPremium) {
getMenuInflater().inflate(R.menu.itemmenupremium, menu);
}
else {
getMenuInflater().inflate(R.menu.itemmenubasic, menu);
}
return true;
}
}
任何帮助将不胜感激!
答案 0 :(得分:2)
你的fetchData()函数是异步运行的,所以当你尝试在onCreate()中填充你的ImageViews和TextViews时,fetchData()可能还没有完成它的任务。
我认为解决方案是在try-block的末尾调用从onPostExecute()填充ImageViews和TextViews的语句。
答案 1 :(得分:0)
使用此
public class ItemCustomLayout extends AppCompatActivity {
private SwipeRefreshLayout swipeRefreshLayout;
private static boolean isPremium = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_custom_layout);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.srl_activityItem);
final SwipeRefreshLayout.OnRefreshListener onRefreshListener = new SwipeRefreshLayout.OnRefreshListener()
{
@Override
public void onRefresh()
{
if (!Utility.isConnected(ItemCustomLayout.this))
{
Toast.makeText(ItemCustomLayout.this, "No internet connection!", Toast.LENGTH_LONG).show();
}
fetchData();
}
};
swipeRefreshLayout.setOnRefreshListener(onRefreshListener);
swipeRefreshLayout.post(new Runnable()
{
@Override
public void run()
{
swipeRefreshLayout.setRefreshing(true);
onRefreshListener.onRefresh();
}
});
}
public void fetchData()
{
new AsyncTask(){
private JSONArray jsonArray;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Object doInBackground(Object[] objects) {
RestApi rest = new RestApi(ItemCustomLayout.this);
jsonArray = rest.fetchJSONArray(*link*);
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
try {
Items items = new Item();
int id = (int) getIntent().getExtras().get("id");
items.imageUrl = jsonArray.getJSONObject(id).getString("image_url");
items.name = jsonArray.getJSONObject(id).getString("name");
items.price = Double.parseDouble(jsonArray.getJSONObject(id).getString("price"));
items.description = jsonArray.getJSONObject(id).getString("description");
items.seller = "Company A";
setData(items);
}
catch (Exception ex)
{
Log.d("Error", ex.toString());
}
swipeRefreshLayout.setRefreshing(false);
}
}.execute();
}
private class Item
{
public int id;
public String name;
public double price;
public String description;
public String seller;
public String imageUrl;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (isPremium) {
getMenuInflater().inflate(R.menu.itemmenupremium, menu);
}
else {
getMenuInflater().inflate(R.menu.itemmenubasic, menu);
}
return true;
}
private void setData(Item items)
{
Picasso.with(ItemCustomLayout.this).load(items.imageUrl).into(((ImageView) findViewById(R.id.imageView1)));
Picasso.with(ItemCustomLayout.this).load(items.imageUrl).into(((ImageView) findViewById(R.id.imageView2)));
Picasso.with(ItemCustomLayout.this).load(items.imageUrl).into(((ImageView) findViewById(R.id.imageView3)));
Picasso.with(ItemCustomLayout.this).load(items.imageUrl).into(((ImageView) findViewById(R.id.imageView4)));
Picasso.with(ItemCustomLayout.this).load(items.imageUrl).into(((ImageView) findViewById(R.id.imageView5)));
((TextView) findViewById(R.id.itemName)).setText(items.name);
((TextView) findViewById(R.id.itemPrice)).setText(String.format("Php %.2f", items.price));
((TextView) findViewById(R.id.itemDescription)).setText(items.description);
((TextView) findViewById(R.id.itemSeller)).setText(items.seller);
}