1

In my code below I created a method which is: I would like to put a variable in putExtra that I can get it in the new activity. The problem is in the new activity gave me the first value(numtelephone) from first element in my list. I know that because of getItem(0), but how can I get the value( numtelephone ) for every element from the list?

public class ListClient extends Activity {
    String myJSON,test; 
  TextView numt;
    private static final String TAG_RESULTS="result";
    private static final String TAG_NOMCLIENT = "nomclient";
    private static String TAG_NUMTELEPHONE ="numtelephone";
    private static final String TAG_DEPART ="depart";
    private static final String TAG_DESTINATION ="destination";
    private static final String TAG_NBRPERSONNE ="nbrpersonne";  
    JSONArray peoples = null;
    ArrayList<HashMap<String, String>> personList;

ListView list;
@SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_client);
        list = (ListView) findViewById(R.id.list);
        personList = new ArrayList<HashMap<String,String>>();
        getData();
    }

    protected void showList(){
            //json parsing etc...

            ListAdapter adapter = new SimpleAdapter(
                    ListClient.this, personList, R.layout.list_item,
                    new String[]{TAG_NOMCLIENT,TAG_NUMTELEPHONE,TAG_DEPART,TAG_DEPART,TAG_NBRPERSONNE},
                    new int[]{R.id.name, R.id.numt, R.id.depart, R.id.destination, R.id.nbrpersonne}
            );
            list.setAdapter(adapter);
    }

    public void getData(){
        class GetDataJSON extends AsyncTask<String, Void, String>{

            @Override
            protected String doInBackground(String... params) {
               // json dowload from web, not needed in this question
               return ""; 
            }

            @Override
            protected void onPostExecute(String result){
                myJSON=result;
                showList();
            }
        }
        GetDataJSON g = new GetDataJSON();
        g.execute();

    }

    public void GoUpdate(View v ) {
        Intent in = new Intent(getBaseContext(), Update.class);
            in.putExtra("NUMTELEPHONE",((HashMap<String, String>)list.getAdapter().getItem(0)).get("numtelephone"));
        }

        startActivity(in);
    }
}
Toumash
  • 1,077
  • 11
  • 27
Andro
  • 15
  • 6
  • post some more code ... – ASP Jul 01 '15 at 23:59
  • Create a string array the size of your list, Iterate through all the positions in a for loop then create a new Bundle and use putStringArray("myKey", myStringArray) and then retrieve the bundle in the new activity. – Mark Jul 02 '15 at 00:06

1 Answers1

0

There is simple and only one solution. Delete your xml android:click property of list and add this list#setOnItemClickListener in your onCreate:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_client);
    list = (ListView) findViewById(R.id.list);
    personList = new ArrayList<HashMap<String,String>>();
    getData();

    //this thing has been added
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(id!=-1){
            Intent in = new Intent(getBaseContext(), Update.class);
            in.putExtra("NUMTELEPHONE", ((HashMap<String, String>) list.getAdapter().getItem((int)id)).get("numtelephone")); //TAG_NUMTELEPHONE

            startActivity(in);
            }
        }
    });
}

To move it from onCreate do:

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_client);
        list = (ListView) findViewById(R.id.list);
        personList = new ArrayList<HashMap<String, String>>();
        getData();
        list.setOnItemClickListener(new OnMyListItemClickListener());
    }

    Context context = this;

    private class OnMyListItemClickListener implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(id!=-1){
            Intent in = new Intent(context, Update.class);
            in.putExtra("NUMTELEPHONE", ((HashMap<String, String>) list.getAdapter().getItem((int) id)).get("numtelephone")); //TAG_NUMTELEPHONE

            startActivity(in);
            }
        }
    }

Lesson learned - dont create sucha buttons - they are not aware in which item they are, so you cant extract the id - instead just use something like AlertDialog

  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(id!=-1){
        AlertDialog.Builder al = new AlertDialog.Builder(context);
        al.setMessage("Are you sure you want to do it?");
        al.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Intent in = new Intent(context, Update.class);
                in.putExtra("NUMTELEPHONE", ((HashMap<String, String>) list.getAdapter().getItem((int) id)).get(TAG_NUMTELEPHONE));

                startActivity(in);
            }
        });
        al.setNegativeButton(android.R.string.no, null);
        al.show(); 
        }        
    }
Toumash
  • 1,077
  • 11
  • 27
  • it gave me all the telephones numbers – Andro Jul 02 '15 at 00:21
  • Is that bad? What should we achieve? `but how can I get the value( numtelephone ) for every element from the list?` – Toumash Jul 02 '15 at 00:30
  • iwant just single value get affiched beffor i add the code you suggest i have the same value in every item but now all the value show on i want just the similar value of every item – Andro Jul 02 '15 at 00:37
  • i cnat upload a image that well help you to understand – Andro Jul 02 '15 at 01:20
  • upload it on imageshack and put a link. Do you want to put other stuff like name surname email or so? You are extremely unclear xd – Toumash Jul 02 '15 at 09:18
  • i want this http://imagizer.imageshack.us/a/img673/2060/b8jf0G.png but withe your code thats whats hapend http://imagizer.imageshack.us/a/img673/2884/Y80YcS.png – Andro Jul 02 '15 at 13:17
  • Okay, now i understand. GoUpdate is an event handler for? Button.onClickListener? How is your screen on the right generated. You just need to pass `getAdapter().getItem(itemID)` but i dont know how you are storing it – Toumash Jul 02 '15 at 14:01
  • Sorry for stupid thinking process, i didnt spot that small `0` - i was really tired. Finally - see my edited answer ; ) Should work like a charm – Toumash Jul 02 '15 at 14:49
  • im sorry Toumash but the item is not clickable nothing happend when i click on it :( – Andro Jul 02 '15 at 15:21
  • Ok, one more thing - there is basic problem with such a button - they dont know about id of the 'user' in whose details are they - insted of button create AlertDialog with a button - then you can click on the whole item and alert will display – Toumash Jul 02 '15 at 15:25
  • There is no other option for SimpleAdapter. In raw adapters where you can controll all stuff you can do [this instead](http://stackoverflow.com/questions/20541821/get-listview-item-position-on-button-click) – Toumash Jul 02 '15 at 15:33
  • whene i do getItem(0) it gave me the same result in all item i know its normal but whene i add getItem((int) id)) like u tell me i got this error java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1 – Andro Jul 02 '15 at 15:50
  • Ok, that will occur when no item is clicked - just add a little `if` -> `if(id!=-1){all the logic in onItemClicked}` - its just a strange Android design : ) – Toumash Jul 02 '15 at 21:06
  • im sorry for disturbing you , i will try a diffrent way to display the data from external database to listview , do you have an esay tutoriel !! – Andro Jul 03 '15 at 00:23
  • See this for sure - [optimization for ListView - ViewHolder](http://gmariotti.blogspot.com/2013/06/tips-for-listview-view-recycling-use.html) (without it ListView just sucks in perf) - use that ViewHolder and getView method with [CursorAdapter code](https://github.com/codepath/android_guides/wiki/Populating-a-ListView-with-a-CursorAdapter) – Toumash Jul 03 '15 at 08:54