Получить определенные c данные из файла jason в список - PullRequest
0 голосов
/ 28 мая 2020

Попытка получить только данные второго пользователя

с идентификатором: 2 (все его данные)

Проблема в том, что когда я получаю данные, они показывают всех пользователей, не только второй.

Я слышал о том, что могу получить его с помощью Arraylist, но не могу понять, как это сделать.

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

есть ли способ получить конкретные c пользовательские данные, не отображать данные всех пользователей, Я новичок в android

пользователей. 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();
        }
    });
}

}

Ответы [ 2 ]

0 голосов
/ 28 мая 2020
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
         }
0 голосов
/ 28 мая 2020
String id = object.getString("id");

if(id.equals("1"))
{
   //do your code
}
...