Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将数据从JSON对象传递到EditText和ImageView_Android_Json - Fatal编程技术网

Android 将数据从JSON对象传递到EditText和ImageView

Android 将数据从JSON对象传递到EditText和ImageView,android,json,Android,Json,我正在从一个我也通过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.andr

我正在从一个我也通过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" />
}

任何帮助都将不胜感激

您的fetchData()函数正在异步运行,因此当您尝试在onCreate()中填充ImageView和TextView时,可能是fetchData()尚未完成其任务

我认为解决方案是在try块末尾从onPostExecute()调用填充ImageView和TextView的语句

您的fetchData()函数正在异步运行,因此当您尝试在onCreate()中填充ImageView和TextView时,可能是fetchData()尚未完成其任务

我认为解决方案是在try块末尾从onPostExecute()调用填充ImageView和TextView的语句

用这个

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);



}
用这个

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);



}

我用过这个,但它仍然不能填充我的编辑文本和图像视图。我认为单击listview时传递项目id有问题。将在修复时更新。非常感谢。是,未从其他类成功传递项id。不过我用过这个。非常感谢。我用过这个,但它仍然不能填充我的编辑文本和图像视图。我认为单击listview时传递项目id有问题。将在修复时更新。非常感谢。是,未从其他类成功传递项id。不过我用过这个。非常感谢。