Java 从数据库检索数据并将其放入列表后,我想将列表中所选项目的id发送给另一个活动

Java 从数据库检索数据并将其放入列表后,我想将列表中所选项目的id发送给另一个活动,java,android,listview,parameter-passing,android-arrayadapter,Java,Android,Listview,Parameter Passing,Android Arrayadapter,我想将listview中所选项目的se_id和se_名称发送到另一个活动。我已从数据库中检索到se_id和se_名称,但它将仅在列表中显示se_名称。如果用户从列表中选择名称,则会将用户带到另一个活动,在该活动中,应发送所选项目的se_id和se_名称。这是我的SEList.class,我将在ChooseOptions se.class中发送seu id和seu名称。如何使用intent发送它?请尽快回复。提前谢谢 package com.example.user.entapp;

我想将listview中所选项目的se_id和se_名称发送到另一个活动。我已从数据库中检索到se_id和se_名称,但它将仅在列表中显示se_名称。如果用户从列表中选择名称,则会将用户带到另一个活动,在该活动中,应发送所选项目的se_id和se_名称。这是我的SEList.class,我将在ChooseOptions se.class中发送seu id和seu名称。如何使用intent发送它?请尽快回复。提前谢谢

    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();

            }
        }
    }
}
package com.example.user.entapp;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.Toast;
导入org.apache.http.NameValuePair;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONArray;
导入org.json.JSONObject;
导入java.util.ArrayList;
导入java.util.List;
公共类SEList扩展了AppCompative活动{
内部监督id,val;
字符串s1,msg,seu id,seu name;
列表视图列表;
ArrayList ArrayList=新的ArrayList();
ArrayAdapter ArrayAdapter;
JSONParser JSONParser=新的JSONParser();
JSONArray pro;
URL=新URL();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selist);
sup_id=getIntent().getExtras().getInt(“sup_id”);
list=(ListView)findViewById(R.id.list\u-se);
新建DataReceive().execute();
}
公共类DataReceive扩展异步任务{
ProgressDialog=新建ProgressDialog(SEList.this);
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
progressDialog.setMessage(“请稍候…”);
progressDialog.setCancelable(假);
progressDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
试一试{
List pairlist=new ArrayList();
s1=整数.toString(sup_id);
添加(新的BasicNameValuePair(“主管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(“信息”);

对于(int i=0;i您可以将Id存储在sperate ArrayList中,或者创建一个具有Id和名称的自定义对象。 然后,在案例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);
在案例2中:

CustomType obj =((CustomAdapter)customAdapter).getAdapter().getItemAt(position);
String se_name = obj.getName();
int se_id = obj.getId();

不要使用
ArrayLisy
您可以使用
ArrayList
来实现这一点,而是像这样创建自定义类
toString()
将帮助您在列表中显示项目

注意:如果将CustomObject设置为可序列化或可打包,则 可以直接将对象发送到其他活动

单击Listener,您可以这样做

   @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);

    }
@覆盖
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
CustomObject se=(CustomObject)arrayList.get(位置);
意向意向=新意向(SEList.this,选择optionse.class);
意向。额外(“se_id”,se.id);
意向。额外(“se_名称”,se.名称);
星触觉(意向);
}
试试这个

将另一个arraylist用于id存储

ArrayList<String> arrayIdList = new ArrayList<>();
现在,执行此操作以获取id并传递到另一个活动

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);
    }
}
public void onItemClick(AdapterView父视图、视图视图、整型位置、长id){
字符串se_id=“”;
对于(int i=0;i

但正如我告诉过你的,这不是一个好方法。请了解。

首先,你应该创建一个模型类。并将你的seu id和seu name设置为它

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;
}
}
在你的主课上呢

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();

        }
    }
}
public类SEList扩展了appcompative活动{
内部监督id,val;
字符串s1,msg,seu id,seu name;
列表视图列表;
List arrayList=新建arrayList();
ArrayAdapter ArrayAdapter;
JSONParser JSONParser=新的JSONParser();
JSONArray pro;
URL=新URL();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selist);
sup_id=getIntent().getExtras().getInt(“sup_id”);
list=(ListView)findViewById(R.id.list\u-se);
新建DataReceive().execute();
}
公共类DataReceive扩展异步任务{
ProgressDialog=新建ProgressDialog(SEList.this);
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
progressDialog.setMessage(“请稍候…”);
progressDialog.setCancelable(假);
progressDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
试一试{
List pairlist=new ArrayList();
s1=整数.toString(sup_id);
添加(新的BasicNameValuePair(“主管id”,s1));
JSONObject JSONObject=jsonParser.makeHttpRequest(url.RECEIVE_SENAME,“POST”,pairlist);
val=jsonObject.getInt(“val”);
msg=jsonObject.getString(“msg”);
//id1=jsonObject.getInt(“id”);
pro=jsonObje
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;
}
}
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();

        }
    }
}
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;
}
}