-1

I want to send the se_id and se_name of the selected item of the listview to another activity. I've retrieved the se_id and se_name from the database but it will only display the se_name in the list. If a user selects a name from the list, it will take the user to another activity where the se_id and se_name of the selected item should be sent. This is my SEList.class and I'll be sending the se_id and se_name in ChooseOptionSE.class. How do I send it using intent? Please reply as soon as possible. Thanks in advance.

    package com.example.user.entapp;

    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.os.AsyncTask;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.Toast;

    import org.apache.http.NameValuePair;
    import org.apache.http.message.BasicNameValuePair;
    import org.json.JSONArray;
    import org.json.JSONObject;

    import java.util.ArrayList;
    import java.util.List;

    public class SEList extends AppCompatActivity {

    int sup_id, val;
    String s1, msg, se_id, se_name;
    ListView list;
    ArrayList<String> arrayList = new ArrayList<>();
    ArrayAdapter<String> arrayAdapter;
    JSONParser jsonParser=new JSONParser();
    JSONArray pro;
    URL url = new URL();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selist);
        sup_id=getIntent().getExtras().getInt("sup_id");
        list= (ListView) findViewById(R.id.list_se);
        new DataReceive().execute();

    }
    public class DataReceive extends AsyncTask<String, String, String> {

        ProgressDialog progressDialog = new ProgressDialog(SEList.this);

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            progressDialog.setMessage("Please wait...");
            progressDialog.setCancelable(false);
            progressDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {
            try{
                List<NameValuePair> pairlist = new ArrayList<NameValuePair>();
                s1=Integer.toString(sup_id);
                pairlist.add(new BasicNameValuePair("supervisor_id", s1));
                JSONObject jsonObject=jsonParser.makeHttpRequest(url.RECEIVE_SENAME,"POST",pairlist);
                val=jsonObject.getInt("val");
                msg=jsonObject.getString("msg");
                //id1=jsonObject.getInt("id");
                pro=jsonObject.getJSONArray("information");

                for(int i=0; i<pro.length(); i++) {
                    JSONObject jo = pro.getJSONObject(i);
                    se_id = jo.getString("se_id");
                    se_name = jo.getString("se_name");

                    arrayList.add(se_name);
                }

            }catch (Exception e){

            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {

            super.onPostExecute(s);
            progressDialog.dismiss();
            if(val==1){
                Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
                arrayAdapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,arrayList);
                list.setAdapter(arrayAdapter);
                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        String se = (String) parent.getItemAtPosition(position);
                        Intent intent = new Intent(SEList.this, ChooseOptionSE.class);
                        intent.putExtra("se_id",se_id);
                        startActivity(intent);

                    }
                });

            }
            else{

                Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();

            }
        }
    }
}
Ninja
  • 678
  • 10
  • 26
Israt
  • 1
  • 2
  • Not sure what the end goal is here but it looks like you are only adding `se_name` to arrayList and never adding `se_id`. Is this what you want to do? – Mackattack Aug 08 '17 at 14:00
  • you are not storing your `se_id` to any where so you can not get that directly. First you need to store it in some array or create your own `model` class. Use that and get id from their. – Farmer Aug 08 '17 at 14:01
  • The goal is to send the se_name and se_id of the selected item to another activity. I've used onItemClick() so that the se_id and se_name of the selected item can be known but I'm not sure how to use it to get the values of the se_id and se_name of the selected item. Can you please help me on that? And I'm not adding se_id because I don't want to show the id to the user, only the name will be displayed. – Israt Aug 08 '17 at 14:09

4 Answers4

0

You can store the IDs in sperate ArrayList or create a custom object that has an Id and Name. Then you can use, in case 1:

String se = (String) parent.getItemAtPosition(position);
se_id = idsList.get(position);
Intent intent = new Intent(SEList.this, ChooseOptionSE.class);
intent.putExtra("se_id",se_id);
startActivity(intent);

in case 2:

CustomType obj =((CustomAdapter)customAdapter).getAdapter().getItemAt(position);
String se_name = obj.getName();
int se_id = obj.getId();
Galalen
  • 64
  • 1
  • 7
0

Instead of using ArrayLisy<String> you can use ArrayList<CustomObject> to achieve this, Create your custom class like this toString() will help you to display the item in the list.

Note: If you make your CustomObject as Serializable or Parcelable, You can directly send the object to another Activity

class CustomObject {
    String name;
    String id;

    @Override
    public String toString() {
        return name;
    }
}

Onclick Listener, you can do like this,

   @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        CustomObject se = (CustomObject) arrayList.get(position) ;
        Intent intent = new Intent(SEList.this, ChooseOptionSE.class);
        intent.putExtra("se_id",se.id);
        intent.putExtra("se_name",se.name);


        startActivity(intent);

    }
Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41
0

Try this.

Take one another arraylist for id storing

ArrayList<String> arrayIdList = new ArrayList<>();

Now store your id in arrayIdList.

try{
            List<NameValuePair> pairlist = new ArrayList<NameValuePair>();
            arrayIdList = new ArrayList<String>();

            for(int i=0; i<pro.length(); i++) {
                JSONObject jo = pro.getJSONObject(i);
                se_id = jo.getString("se_id");
                se_name = jo.getString("se_name");

                arrayList.add(se_name);
                arrayIdList.add(se_id);
            }

        }catch (Exception e){

        }

now do like this to get id and pass to another activity

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    String se_id = "";
    for(int i = 0; i < arrayIdList.size(); i++)
    {
        if(position == i){
            se_id = arrayIdList.get(i);
            break;
        }
    }

    Log.i("se_is = ", ""+ se_id);
    if(se_id != null){
        String se = (String) parent.getItemAtPosition(position);
        Intent intent = new Intent(SEList.this, ChooseOptionSE.class);
        intent.putExtra("se_id",se_id);
        startActivity(intent);
    }
}

But as i told you that this in not good way to do. Please learn about CustomAdapter.

Ninja
  • 678
  • 10
  • 26
0

First you should create a model class .And set your se_id and se_name to it.

public class Model {
String name;
String id;
public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}




public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}

and in your main class

public class SEList extends AppCompatActivity {

int sup_id, val;
String s1, msg, se_id, se_name;
ListView list;
List<Model> arrayList = new ArrayList<>();
ArrayAdapter<String> arrayAdapter;
JSONParser jsonParser=new JSONParser();
JSONArray pro;
URL url = new URL();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_selist);
    sup_id=getIntent().getExtras().getInt("sup_id");
    list= (ListView) findViewById(R.id.list_se);
    new DataReceive().execute();

}
public class DataReceive extends AsyncTask<String, String, String> {

    ProgressDialog progressDialog = new ProgressDialog(SEList.this);

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        progressDialog.setMessage("Please wait...");
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        try{
            List<NameValuePair> pairlist = new ArrayList<NameValuePair>();
            s1=Integer.toString(sup_id);
            pairlist.add(new BasicNameValuePair("supervisor_id", s1));
            JSONObject jsonObject=jsonParser.makeHttpRequest(url.RECEIVE_SENAME,"POST",pairlist);
            val=jsonObject.getInt("val");
            msg=jsonObject.getString("msg");
            //id1=jsonObject.getInt("id");
            pro=jsonObject.getJSONArray("information");

            for(int i=0; i<pro.length(); i++) {
                JSONObject jo = pro.getJSONObject(i);
                Model model = new Model();
                se_id = jo.getString("se_id");
                se_name = jo.getString("se_name");

                model.setId(se_id);
                model.setName(se_name);


                arrayList.add(model);
            }

        }catch (Exception e){

        }
        return null;
    }

    @Override
    protected void onPostExecute(String s) {

        super.onPostExecute(s);
        progressDialog.dismiss();
        if(val==1){
            Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();

            CustomAdapter adapter = new CustomAdapter(arrayList,SEList.this)
            list.setAdapter(adapter);

             list.setOnItemClickListener(new OnItemClickListener() {
                 
                                       @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    //String se = (String) parent.getItemAtPosition(position);
                    Model model = arrayList.get(position);
                    Intent intent = new Intent(SEList.this, ChooseOptionSE.class);
                    intent.putExtra("se_id", model.getId);
                    startActivity(intent);

                }
            });

        }
        else{

            Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();

        }
    }
}

You should create a CustomAdapter class for setting Listview.

public class CustomAdapter extends ArrayAdapter<Model> {

private List<Model> dataSet;
Context mContext;

// View lookup cache
private static class ViewHolder {
    TextView txtName;

}

public CustomAdapter(ArrayList<Model> data, Context context) {
    super(context, R.layout.row_item, data); // Create a layout xml file with one text view
    this.dataSet = data;
    this.mContext=context;

}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    Model dataModel = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    ViewHolder viewHolder; // view lookup cache stored in tag

    final View result;

    if (convertView == null) {

        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.row_item, parent, false);
        viewHolder.txtName = (TextView) convertView.findViewById(R.id.name);

        result=convertView;

        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
        result=convertView;
    }

    viewHolder.txtName.setText(dataModel.getName());

    // Return the completed view to render on screen
    return convertView;
}
}
Bivin
  • 375
  • 3
  • 24