This is a question about Android listview.

Asked 2 years ago, Updated 2 years ago, 132 views

Hello. I'm a developer who lacks a lot I'm posting a question like this because there's something I don't understand. I can't recognize the position in the listview, so I upload it like this Please understand if the sauce is a bit long crying First, turn the int on the screen with the listview!

 @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this, store_space.class);
        intent.putExtra("position", position);
        startActivity(intent);

Yes, and then it's the source of the source. I'm receiving the position value through Google spreadsheet. Please look at the link here if you want to refer to it https://docs.google.com/spreadsheets/d/18CYJ-2Xo4Prundk5Rp6PsPrxFXYzD56ybSioPT2XG-8/edit Yes, but the position value comes in well from zero, but even if you press any listview, only the position number 13 comes in That's why I'm asking this question

 private ArrayList<store_itemInfo> results = new ArrayList<>();
    private int mPosition = 0;
    private TextView source, title, address, explain;
    private ImageView imageview, imageview1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.store_space);
        mPosition = getIntent().getIntExtra("position", 0);
        source = (TextView) findViewById(R.id.store_space_Text_Source);
        title = (TextView) findViewById(R.id.store_space_Text_Title);
        address = (TextView) findViewById(R.id.store_space_Text_Address);
        explain = (TextView) findViewById(R.id.store_space_Text_Explain);
        imageview = (ImageView) findViewById(R.id.store_space_ImageView);
        imageview1 = (ImageView) findViewById(R.id.store_space_ImageView1);
        new AsyncHttpTask().execute();
//        //        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//        //        fab.setOnClickListener(new View.OnClickListener() {
//            //            @Override
//            //            public void onClick(View view) {
//                //                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                        //                        .setAction("Action", null).show();
//            }
//        });
    }
    public class AsyncHttpTask extends AsyncTask<String, Void, String> {
        HttpURLConnection urlConnection;
        StringBuilder result;
        ProgressDialog asyncDialog = new ProgressDialog(
                store_space.this);

        @Override
        protected void onPreExecute() {
            asyncDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            AsyncDialog.setMessage ("Loading..."");

            // // show dialog
            asyncDialog.show();
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {
            result = new StringBuilder();
            urlConnection = null;

            try {
                URL url = new URL("https://script.google.com/macros/s/AKfycby39ckxtMJ_fupsVFO2-AueX1eImiMA-C25srnn0WTKoNJlPKaW/exec");
                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.connect();
                InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                String line;
                String result = "";
                while ((line = reader.readLine()) != null) {
                    result += line;
                }
                // // Close stream
                if (null != in) {
                    in.close();
                }
                return result;

            } } catch (Exception e) {
                e.printStackTrace();
            } } finally {
                assert urlConnection != null;
                urlConnection.disconnect();
            }
            return result.toString();
        }


        @Override
        protected void onPostExecute(String result) {
            // // Download complete. Lets update UI
            parseResult(result);
            asyncDialog.dismiss();
        }

        private void parseResult(String result) {
            try {
                if (null != results) {
                    results = new ArrayList<store_itemInfo>();
                }
                JSONArray response = new JSONArray(result);
                for (int i = 0; i < response.length(); i++) {
                    store_itemInfo temp = new store_itemInfo();
                    temp.setTitle(response.getJSONObject(i).getString("title"));
                    temp.setImage(response.getJSONObject(i).getString("image"));
                    temp.setImage1(response.getJSONObject(i).getString("image1"));
                    temp.setPosition(response.getJSONObject(i).getInt("position"));
                    temp.setAddress(response.getJSONObject(i).getString("address"));
                    temp.setExplain(response.getJSONObject(i).getString("explain"));
                    temp.setSource(response.getJSONObject(i).getString("source"));
                    temp.setIndex(response.getJSONObject(i).getInt("index"));
                    source.setText(temp.getSource());
                    title.setText(temp.getTitle());
                    address.setText(temp.getAddress());
                    explain.setText(temp.getExplain());
                    Picasso.with(store_space.this).load(temp.getImage()).fit().into(imageview);
                    Picasso.with(store_space.this).load(temp.getImage1()).fit().into(imageview1);
                    if (mPosition == temp.getPosition())
                        results.add(temp);
                }

            } } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}

Thank you for watching every time!!

android adapter

2022-09-22 12:17

1 Answers

I don't quite understand the question. If you post the code for the part that doesn't work, it will be helpful to answer. I have attached a simple code to look at the processing of ListView click events. I think it would be better to compare it to the code you are working on and find the problem.

public class ListActivity extends android.app.ListActivity {

    static final String[] FRUITS = new String[]{"Apple", "Banana", "Strawberry",
        "Coconut", "mango", "kiwi", "Peach", "Melon", "Watermelon", "olive",
        "Melon," "Tomato," "Cucumber," "Basil," "Wallgyesu," "Peppermint," "Lemon"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, FRUITS));
        getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(ListActivity.this,
                    "Position: " + position + ", " + getListAdapter().getItem(position), Toast.LENGTH_SHORT)
                    .show();
            }
        });
    }
}

Result screen - Toast output when you click a peach in the list


2022-09-22 12:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.