单击Android Studio时如何从项目列表视图中获取价值?

时间:2019-07-14 11:48:51

标签: android android-studio

在开发Android应用程序方面,我还是一个相对较新的人。运行一段时间后,当我的程序自动切换到活动时,当我在列表视图中单击某个项目时,它会变成启动器,从而出现问题。

实际上该怎么做?

我使用从服务器获取的JSON。

我打算从列表视图中的项目中检索数据,并将其放在edittext中的另一个活动中。

活动一

public class OneActivity extends AppCompatActivity {
    private LKamarAdapter lKamarAdapter;
    @BindView(R.id.lv)
    ListView lv;

    BaseApiService mApiService;
    Context mContext;
    ArrayList<ModelListView> modelListViewArrayList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);
        ButterKnife.bind(this);
        mContext = this;
        mApiService = UtilsApi.getAPIService();
        getJSONResponse();
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String nama = modelListViewArrayList.get(i).getNama();

                Intent intent = new Intent(mContext, TwoActivity.class);
                intent.putExtra("nama",nama);
                startActivity(intent);
            }
        });
    }

    private void getJSONResponse() {
        Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BaseApiService.JSONURL)
            .addConverterFactory(ScalarsConverterFactory.create())
            .build();

        BaseApiService api = retrofit.create(BaseApiService.class);

        Call<String> call = api.getOne();

        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Log.i("Responsestring", response.body());
                if (response.isSuccessful()) {
                    if (response.body() != null) {
                        Log.i("onSuccess",response.body());
                        String jsonresponse = response.body();
                        writeListView(jsonresponse);
                    } else {
                        Log.i("onEmptyResponse", "Returned empty response");
                    }
                }
            }
            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });
    }

    private void writeListView(String response) {
        try {
            JSONObject obj = new JSONObject(response);
            if(obj.optString("pesan").equals("berhasil")) {
                ArrayList<ModelListView> modelListViewArrayList = new ArrayList<>();
                JSONArray dataArray = obj.getJSONArray("kamar");

                for (int j = 0; j < dataArray.length(); j++) {
                    ModelListView modelListView = new ModelListView();
                    JSONObject dataobj = dataArray.getJSONObject(j);
                    modelListView.setNama(dataobj.getString("nama"));
                    modelListViewArrayList.add(modelListView);
                }

                lKamarAdapter = new LKamarAdapter(this, modelListViewArrayList);
                lv.setAdapter(lKamarAdapter);
            } else {
                Toast.makeText(mContext, obj.optString("listkamar")+"", Toast.LENGTH_SHORT).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

第二项活动

public class TwoActivity extends AppCompatActivity {
    @BindView(R.id.etNama)
    EditText etNama;

    String nama;

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

        ButterKnife.bind(this);

        nama = getIntent().getStringExtra("nama");
        etNama.setText(nama);
    }

1 个答案:

答案 0 :(得分:2)

代替String nama = modelListViewArrayList.get(i).getNama();

尝试一下String nama = ((ModelListView) lv.getAdapter().getItem(i)).getNama();