如何使用数据库中的json获取数据并在android中的水平列表视图中填充它

时间:2015-07-16 09:05:43

标签: android mysql json horizontallistview

 main xml containing horizontallistview                              
 activity_main.xml
                                <LinearLayout
                                  xmlns:android="http://schemas.android.com/apk/res/android"
                                  android:orientation="vertical"
                                  android:layout_width="fill_parent"
                                  android:layout_height="fill_parent"
                                  android:background="#fff"
                                  >

                                  <com.example.newomolistview.HorizontalScrollView
                                    android:id="@+id/listview"
                                    android:layout_width="fill_parent"
                                    android:layout_height="wrap_content"
                                    android:background="#ddd"
                                  />

                                </LinearLayout>
                                contain three textviews-for product name,original rate and offer rate
                                app_custom_list.xml

                        I think in this java class i have gone wrong
                        ApplicationAdapter.java
                        package com.example.newomolistview;

                        import java.text.NumberFormat;
                        import java.util.List;

                        import android.content.Context;
                        import android.content.res.Resources;
                        import android.view.LayoutInflater;
                        import android.view.View;
                        import android.view.ViewGroup;
                        import android.widget.ArrayAdapter;
                        import android.widget.BaseAdapter;
                        import android.widget.ImageView;
                        import android.widget.LinearLayout;
                        import android.widget.TextView;

                        import com.example.newomolistview.R;
                        import com.example.newomolistview.HorizontalScrollView;

                        public class ApplicationAdapter extends ArrayAdapter<Application>{
                            private List<Application> items;


                            public ApplicationAdapter(Context context, List<Application> items) {
                                super(context, R.layout.app_custom_list, items);
                                this.items = items;
                            }

                         //   private BaseAdapter mAdapter = new BaseAdapter() {
                            @Override
                            public int getCount() {
                                return items.size();
                            }

                            @Override
                            public View getView(int position, View convertView, ViewGroup parent) {
                                View v = convertView;

                                if(v == null) {
                                     v = LayoutInflater.from(parent.getContext()).inflate(R.layout.app_custom_list, null); //new code
                                  //  LayoutInflater li = LayoutInflater.from(getContext());  //original
                               //     v = li.inflate(R.layout.app_custom_list, null);          //original 

                                //   LayoutInflater li = (LayoutInflater) this._context
                                //          .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                                //  convertView = li.inflate(R.layout.app_custom_list, null);

                                }

                                Application app = items.get(position);

                                if(app != null) {

                                    TextView productName = (TextView)v.findViewById(R.id.textView1);
                                    TextView originalRate = (TextView)v.findViewById(R.id.textView2);
                                    TextView offerRate = (TextView)v.findViewById(R.id.textView3);



                                    if(productName != null) productName.setText(app.getProductName());
                                    if(originalRate != null) originalRate.setText(app.getOriginalRate()); 
                                    if(offerRate != null) offerRate.setText(app.getOfferRate());

                                }
                                         return v;
                        //                 View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.listitem, null);
                        //              TextView title = (TextView) retval.findViewById(R.id.title);
                        //              title.setText(dataObjects[position]);
                        //              
                        //              return retval;

                            }

                        //    };
                        }

我需要使用数据库中的json数据(产品名称,原始速率和报价率)填充水平listview。我使用数组适配器填充水平列表视图。我认为我在ApplicationAdapter.java和MainActivity.java中出错了                         在本节中,我也出错了                         MainActivity.java

                    package com.example.newomolistview;

                    import java.util.List;

                    import com.example.newomolistview.HorizontalScrollView;
                    import com.example.newomolistview.R;

                    import android.app.ListActivity;
                    import android.app.ProgressDialog;
                    import android.os.Bundle;
                    import android.widget.BaseAdapter;
                    import android.widget.Toast;

                    public class MainActivity extends ListActivity implements FetchDataListener{
                        private ProgressDialog dialog;


                        @Override
                        protected void onCreate(Bundle savedInstanceState) {
                            super.onCreate(savedInstanceState);        
                            setContentView(R.layout.activity_main);     
                    //        HorizontalScrollView listview = (HorizontalScrollView) findViewById(R.id.listview);
                    //      listview.setAdapter(mAdapter);
                            initView();   
                        }


                        private void initView() {
                            // show progress dialog
                            dialog = ProgressDialog.show(this, "", "Loading...");
                            String key="saasvaap123";
                            String cityid="1";
                            String url = "http://www.gooffers.in/omowebservices/index.php/webservice/Public_User/homePagepoffers?";
                            FetchDataTask task = new FetchDataTask(this);
                            task.execute(url,key,cityid);
                        }

                        @Override
                        public void onFetchComplete(List<Application> data) {
                            // dismiss the progress dialog
                            if(dialog != null)  dialog.dismiss();
                            HorizontalScrollView listview = (HorizontalScrollView) findViewById(R.id.listview);  //new code
                            // create new adapter
                            ApplicationAdapter mAdapter = new ApplicationAdapter(this, data);

                            listview.setAdapter(mAdapter);   //new code
                            // set the adapter to list
                           // setListAdapter(adapter);        
                        }

                        @Override
                        public void onFetchFailure(String msg) {
                            // dismiss the progress dialog
                            if(dialog != null)  dialog.dismiss();
                            // show failure message
                            Toast.makeText(this, msg, Toast.LENGTH_LONG).show();        
                        }
                    }


          I need to populate horizontal listview with json data from database(product name, original rate and offer rate).I used array adapter to populate the horizontal listview. I think I have gone wrong somewhere in ApplicationAdapter.java and MainActivity.java 

1 个答案:

答案 0 :(得分:0)