数据填充到列表视图 [英] populating data into listview

查看:142
本文介绍了数据填充到列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

填充数据到列表视图

我使用JSON响应

MainActivity.java

 公共类MainActivity延伸活动{    // URL使请求    私有静态字符串URL =htt​​p://54.218.73.244:7002/;    ListView的yourListView;    清单<项目> yourData =新的ArrayList<项目>();    ProgressDialog progressDialog;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        yourListView =(ListView控件)findViewById(R.id.listViewID);        //实例化ProgressDialog与onCreate方法
        progressDialog =新ProgressDialog(MainActivity.this);
        新ParsingAsync()执行();    }    私有类ParsingAsync扩展的AsyncTask<太虚,太虚,太虚>
    {        @覆盖
        在preExecute保护无效(){
            // TODO自动生成方法存根
            super.on preExecute();
            progressDialog = ProgressDialog.show(MainActivity.this,,请等待,真,假);
        }        @覆盖
        保护无效doInBackground(虚空...... PARAMS){
            // TODO自动生成方法存根
            //创建一个JSON解析器实例-----从Android的使用JSON解析器
            JSONParser jParser =新JSONParser();            //获取JSON字符串URL从------二手JSON在Android阵
            JSONArray JSON = jParser.getJSONFromUrl(URL);            清单<项目> yourData =新的ArrayList<项目>();
            尝试{
                的for(int i = 0; I< json.length();我++)
                {
                    JSONObject的C = json.getJSONObject(我);在Android //二手JSON对象                    串RESTAURANT_NAME = c.getString(restaurantNAME);                    yourData.add(新项目(RESTAURANT_NAME));                }
            }赶上(JSONException E){
                // TODO自动生成catch块
                e.printStackTrace();
            }            返回null;        }        @覆盖
        保护无效onPostExecute(虚空结果){
            // TODO自动生成方法存根
            super.onPostExecute(结果);
            progressDialog.dismiss();
            ListAdapter customAdapter =新ListAdapter(这一点,R.layout.itemlistrow,yourData);            yourListView.setAdapter(customAdapter);
        }    }}

ListAdapter.java

 公共类ListAdapter扩展ArrayAdapter<项目> {    私人列表<项目>项目;    公共ListAdapter(上下文的背景下,INT资源列表与LT;项目>项目){        超(背景下,资源,资料);        this.items =物品;
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){        视图V = convertView;        TextView的TT1 = NULL;        如果(V == NULL){            LayoutInflater六;
            VI = LayoutInflater.from(的getContext());
            V = vi.inflate(R.layout.itemlistrow,NULL);
            TT1 =(TextView中)v.findViewById(R.id.RestaurantNameID);        }        项目P = items.get(位置);        如果(P!= NULL){            如果(TT1!= NULL){                tt1.setText(+ p.getName());
            }        }
        返回伏;    }
}

Item.java

 公共类项目{
    私人诠释年龄;
    私人字符串名称;
    私人字符串的城市;
    私人字符串性别;
    私人字符串的生日;    公共项目(字符串名称){        this.name =名称;
    }
    公共字符串的getName(){
        返回this.name;
    }
}

错误:

 描述资源路径位置类型
构造函数ListAdapter(MainActivity.ParsingAsync,INT,列表与LT;项目>)的未定义MainActivity.java / FindMyBuffet / src目录/ COM /项目/ findmybuffet线101 Java问题


解决方案

您必须找出你的ListView ID onCreate()方法。

所以把这个YOUE的setContentView()方法后。

 的ListView yourListView =(ListView控件)findViewById(R.id.listViewID);

在postExecute()方法只有你的用户界面将被更新。

和也不叫两个构造为您的适配器......所以只是删除这样的:

 公共ListAdapter(上下文的背景下,诠释textViewResourceId){
    超(背景下,textViewResourceId);
    // TODO自动生成构造函数存根
}

也改变

  ListAdapter customAdapter =新ListAdapter(这一点,R.layout.itemlistrow,yourData);

  ListAdapter customAdapter =新ListAdapter(YourActivity.this,R.layout.itemlistrow,yourData);

希望你会明白...!

populating data into listview

I am using JSON response

MainActivity.java

     public class MainActivity extends Activity {

    // url to make request

    private static String url="http://54.218.73.244:7002/";

    ListView yourListView;

    List<Item> yourData = new ArrayList<Item>();

    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        yourListView = (ListView) findViewById(R.id.listViewID);

        //Instantiating ProgressDialog with onCreate method
        progressDialog=new ProgressDialog(MainActivity.this);
        new ParsingAsync().execute();

    }

    private class ParsingAsync extends AsyncTask<Void, Void, Void>
    {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            progressDialog=ProgressDialog.show(MainActivity.this, "", "Please Wait", true, false);


        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub


            //Create a JSON parser Instance ----- Used JSON parser from Android
            JSONParser jParser=new JSONParser();

            //Getting JSON string from URL ------ Used JSON Array from Android
            JSONArray json=jParser.getJSONFromUrl(url);

            List<Item> yourData = new ArrayList<Item>();


            try {
                for(int i=0;i<json.length();i++)
                {
                    JSONObject c=json.getJSONObject(i);// Used JSON Object from Android

                    String RESTAURANT_NAME=c.getString("restaurantNAME");

                    yourData.add(new Item(RESTAURANT_NAME));

                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;

        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            progressDialog.dismiss();


            ListAdapter customAdapter = new ListAdapter(this, R.layout.itemlistrow, yourData);

            yourListView.setAdapter(customAdapter);


        }

    }

}

ListAdapter.java

 public class ListAdapter extends ArrayAdapter<Item> {



    private List<Item> items;

    public ListAdapter(Context context, int resource, List<Item> items) {

        super(context, resource, items);

        this.items = items;


    }

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

        View v = convertView;

        TextView tt1 = null;

        if (v == null) {

            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.itemlistrow, null);


            tt1 = (TextView) v.findViewById(R.id.RestaurantNameID);

        }

        Item p = items.get(position);

        if (p != null) {

            if (tt1 != null) {

                tt1.setText(""+p.getName());
            }

        }
        return v;

    }
}

Item.java

public class Item{
    private int age;
    private String name;
    private String city;
    private String gender;
    private String birthdate;        

    public Item( String name){

        this.name = name;
    }


    public String getName(){
        return this.name;
    }


}

Error ::

Description Resource    Path    Location    Type
The constructor ListAdapter(MainActivity.ParsingAsync, int, List<Item>) is undefined    MainActivity.java   /FindMyBuffet/src/com/project/findmybuffet  line 101    Java Problem

解决方案

You have find out your ListView id onCreate() method.

So put this after youe setContentView() method.

 ListView yourListView = (ListView) findViewById(R.id.listViewID);

and in postExecute() method only your UI will be updated.

And also don't call two constructor for your adapter... So just remove this:

public ListAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
    // TODO Auto-generated constructor stub
}

also change

ListAdapter customAdapter = new ListAdapter(this, R.layout.itemlistrow, yourData);

to

ListAdapter customAdapter = new ListAdapter(YourActivity.this, R.layout.itemlistrow, yourData);

Hope you will understand...!!

这篇关于数据填充到列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆