从 JSON 文件获取特定数据到列表视图

Posted

技术标签:

【中文标题】从 JSON 文件获取特定数据到列表视图【英文标题】:Get specific data from JSON file to listview 【发布时间】:2020-09-16 00:52:53 【问题描述】:

尝试仅获取第二个用户数据

id 为 :2(他的全部数据)

问题是当我获取数据时,它会显示所有用户,而不仅仅是第二个用户。

我听说我可以使用 Arraylist 获得它,但不知道如何制作它。

mListView.get(position).get(Mykey);

是否有获取特定用户数据的方法,而不是显示所有用户数据,我是 android 新手

users.json

[

"id": "1",
"name": "tester1",
"username": "user1",
"verify": "1",
"about": "tesst",
"description": "description....... etc",
"mobile": "+.....",
"lvl": "1",
"imageurl": "..../pic.png",
"technologyexists": "1",
"password": "xxx"
,

"id": "2",
"name": "tester2",
"username": "user2",
"verify": "0",
"about": "tesst",
"description": "description ....... etc",
"mobile": "+.....",
"lvl": "1",
"imageurl": "..../pic.png",
"technologyexists": "1",
"password": "xxx"
,

"id": "3",
"name": "tester3",
"username": "user3",
"verify": "0",
"about": "tesst",
"description": "description ....... etc",
"mobile": "+.....",
"lvl": "1",
"imageurl": "..../pic.png",
"technologyexists": "1",
"password": "xxx"

]

ActivityMain

class Spacecraft 
    /*
    INSTANCE FIELDS
     */
    @SerializedName("id")
    private String id;

    @SerializedName("name")
    private String name;

    @SerializedName("username")
    private String username;

    @SerializedName("password")
    private String password;

    @SerializedName("about")
    private String about;

    @SerializedName("description")
    private String description;

    @SerializedName("mobile")
    private String mobile;

    @SerializedName("lvl")
    private String lvl;

    @SerializedName("imageurl")
    private String imageURL;

    @SerializedName("technologyexists")
    private int technologyexists;

    @SerializedName("verify")
    private String verify;
    ////////

    public Spacecraft(String id, String name, String username,String password, String about, String description,  String mobile, String lvl, String imageURL, int technologyexists, String verify) 

        this.id = id;
        this.name = name;

        this.username = username;
        this.password = password;

        this.about = about;
        this.description = description;

        this.mobile = mobile;
        this.lvl = lvl;

        this.imageURL = imageURL;
        this.technologyexists = technologyexists;

        this.verify = verify;

    

    /*
     *GETTERS AND SETTERS
     */
    public String getId() 
        return id;
    

    public String getName() 
        return name;
    
    public void setName(String name) 
        this.name = name;
    

    public String getusername() 
        return username;
    

    public String getpassword() 
        return password;
    


    public String getAbout() 
        return about;
    

    public String getImageURL() 
        return imageURL;
    

    public String getMobile() 
        return mobile;
    

    public String getlvl() 
        return lvl;
    

    public String getDescription() 
        return description;
    

    public String getVerify() 
        return verify;
    

    public int getTechnologyExists() 
        return technologyexists;
    

    /*
    TOSTRING
     */
    @Override
    public String toString() 
        return name;
    


 interface MyAPIService 

    @GET("/users.json")
    Call<List<Spacecraft>> getSpacecrafts();


    static class RetrofitClientInstance 

    private static Retrofit retrofit;
    private static final String BASE_URL = "http://Server_ip_to_get_json_file:80/";

    public static Retrofit getRetrofitInstance() 
        if (retrofit == null) 
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        
        return retrofit;
    





    class ListViewAdapter extends BaseAdapter

    private List<Spacecraft> spacecrafts;
    private Context context;

    public ListViewAdapter(Context context,List<Spacecraft> spacecrafts)
        this.context = context;
        this.spacecrafts = spacecrafts;
    

    @Override
    public int getCount() 
        return spacecrafts.size();
    

    @Override
    public Object getItem(int pos) 
        return spacecrafts.get(pos);
    

    @Override
    public long getItemId(int pos) 
        return pos;
    

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) 
        if(view==null)
        
            view=LayoutInflater.from(context).inflate(R.layout.model,viewGroup,false);
        

        TextView nameTxt = view.findViewById(R.id.nameTextView);
        TextView txtPropellant = view.findViewById(R.id.propellantTextView);
        CheckBox chkTechExists = view.findViewById(R.id.myCheckBox);
        ImageView spacecraftImageView = view.findViewById(R.id.spacecraftImageView);

        final Spacecraft thisSpacecraft= spacecrafts.get(position);

        nameTxt.setText(thisSpacecraft.getName());
        //  txtPropellant.setText("Lvl : "+thisSpacecraft.getPropellant());
        txtPropellant.setText("Lvl : "+thisSpacecraft.getlvl());
        chkTechExists.setChecked( thisSpacecraft.getTechnologyExists()==1);
        chkTechExists.setEnabled(false);

        if(thisSpacecraft.getImageURL() != null && thisSpacecraft.getImageURL().length()>0)
        
            Picasso.get().load(thisSpacecraft.getImageURL()).placeholder(R.drawable.pic).into(spacecraftImageView);
        else 
            Toast.makeText(context, "Empty Image URL", Toast.LENGTH_LONG).show();
            Picasso.get().load(R.drawable.pic).into(spacecraftImageView);
        

        view.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 


                //  Toast.makeText(context, "This : " + thisSpacecraft.getName(), Toast.LENGTH_SHORT).show();

                //  Toast.makeText(context, "This : " + thisSpacecraft.getImageURL(), Toast.LENGTH_SHORT).show();

                Intent myIntent = new Intent(Main3Activity.this, Main4Activity.class);

                myIntent.putExtra("Name", thisSpacecraft.getName());
                myIntent.putExtra("Uri", thisSpacecraft.getImageURL());

                myIntent.putExtra("Mobile", thisSpacecraft.getMobile());
                myIntent.putExtra("Lvl", thisSpacecraft.getlvl());

                myIntent.putExtra("Description", thisSpacecraft.getDescription());

                myIntent.putExtra("About", thisSpacecraft.getAbout());

                myIntent.putExtra("Verify", thisSpacecraft.getVerify());

                myIntent.putExtra("User_ID", thisSpacecraft.getId());

                myIntent.putExtra("Username", thisSpacecraft.getusername());

                myIntent.putExtra("Password", thisSpacecraft.getpassword());

                myIntent.putExtra("Username_me", Username_me);

                startActivity(myIntent);

                update();
            
        );

        return view;
    



  private ListViewAdapter adapter;
private ListView mListView;
ProgressBar myProgressBar;
private int mImageResource;

private void populateListView(List<Spacecraft> spacecraftList) 
    mListView = findViewById(R.id.mListView);


    adapter = new ListViewAdapter(this,spacecraftList);
    mListView.setAdapter(adapter);


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


           final ProgressBar myProgressBar= findViewById(R.id.myProgressBar);
    myProgressBar.setIndeterminate(true);
    myProgressBar.setVisibility(View.VISIBLE);

    /*Create handle for the RetrofitInstance interface*/
    MyAPIService myAPIService = RetrofitClientInstance.getRetrofitInstance().create(MyAPIService.class);

    Call<List<Spacecraft>> call = myAPIService.getSpacecrafts();
    call.enqueue(new Callback<List<Spacecraft>>() 

           @Override
        public void onResponse(Call<List<Spacecraft>> call, Response<List<Spacecraft>> response) 
            myProgressBar.setVisibility(View.GONE);
            populateListView(response.body());
        
        @Override
        public void onFailure(Call<List<Spacecraft>> call, Throwable throwable) 
            myProgressBar.setVisibility(View.GONE);
            //  Toast.makeText(Main3Activity.this, throwable.getMessage(), Toast.LENGTH_LONG).show();
            Toast.makeText(Main3Activity.this, "Check Your Internet Connection", Toast.LENGTH_LONG).show();
        
    );



【问题讨论】:

检查这个问题,我可以帮你Question 它只显示完整数据,而不是具体数据! if (object.getString("id") =="1") 【参考方案1】:
String id = object.getString("id");

if(id.equals("1"))

   //do your code

【讨论】:

【参考方案2】:
if(thisSpacecraft.getId().equals("2"))


List<Spacecrafts> list = new ArrayList<>();
    if(list.get(position).getId().equals(2)) //check if the list contains the element
         
          list.get(list.indexOf(position)); //get the element by passing the index of the element
         

【讨论】:

我在里面测试了两个:public View getView(int position, View view, ViewGroup viewGroup) //// 但不行,它们就在那里!对不起,我只是新人

以上是关于从 JSON 文件获取特定数据到列表视图的主要内容,如果未能解决你的问题,请参考以下文章

从视图表中获取数据时出现 Laravel 错误

文本文件字符串拆分然后将每个元素输出到视图表中

如何从 Google bigquery(google-cloud-ruby gem)的视图表(具有 resource_full)中获取数据

访问使用列表框中选择的项目更新子表单

在线时更新列表视图内容

如何从json获取数据中对两个或多个值求和