不能将列表视图的位置传递给第二个活动?

     2023-04-19     241

关键词:

【中文标题】不能将列表视图的位置传递给第二个活动?【英文标题】:Can't Pass the postion of a listview to second activity? 【发布时间】:2015-03-21 16:41:24 【问题描述】:

您好,我创建了一个存储一些编辑文本的数据库。我有三个活动 A、B 和 C。活动 A 在列表视图中显示存储的值。当单击活动 A 中的列表视图项时,它会将其位置和存储的数据传递给活动 B,活动 B 将其显示为简单文本且无法编辑。现在我想将这些数据从活动 B 传递到可以编辑的活动 C。如何通过 Acitvity B 将列表视图位置从 A 传递到 C。

活动 A

public class UserActivity extends Activity implements OnItemClickListener 

    private ListAdapter users;
    private ListView lista;
    private AccessModel data;
    private ArrayList<User> query;
    private LinearLayout empty_data;


    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);     
        requestWindowFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.listusers);
        lista = (ListView) findViewById(R.id.lstusers);
        empty_data= (LinearLayout) findViewById(R.id.empty_data);
        data = new AccessModel(this);       
    


    @Override
    protected void onResume() 
        // TODO Auto-generated method stub
        super.onResume();       
         query = data.listUser();

         if(query.size()>0)
           empty_data.setVisibility(View.GONE);
         else
           empty_data.setVisibility(View.VISIBLE);

         users = new ListAdapter(this, query);
         lista.setAdapter(users);
         lista.setOnItemClickListener(this);

    

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) 
        // TODO Auto-generated method stub
        getMenuInflater().inflate(R.menu.optionsbar, menu);
        return super.onPrepareOptionsMenu(menu);
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        // TODO Auto-generated method stub
        switch (item.getItemId()) 
            case R.id.adduser:
                Intent i = new Intent(this, RegisterUser.class);
                startActivity(i);
                return true;                
            default:
                return super.onOptionsItemSelected(item);
        

    


    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) 
        // TODO Auto-generated method stub      
        User u = (User) query.get(position);
        Intent i = new Intent(UserActivity.this, ViewPatientData.class);
        i.putExtra(getString(R.string.valuesId), String.valueOf(u.getId()));
        startActivity(i);       
    





活动 B

 protected void onCreate(Bundle savedInstanceState) 
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.view_patient_data);

        getActionBar().setDisplayHomeAsUpEnabled(true);

        data = new AccessModel(this);

        name = (TextView) findViewById(R.id.tv_name);
        age = (TextView) findViewById(R.id.tv_age);
        gender = (TextView) findViewById(R.id.tv_gender);
        height = (TextView) findViewById(R.id.tv_height);
        weight = (TextView) findViewById(R.id.tv_weight);
        dateofbirth = (TextView) findViewById(R.id.tv_dateofbirth);
        religion = (TextView) findViewById(R.id.tv_religion);
        maritalstatus = (TextView) findViewById(R.id.tv_maritalstatus);
        dateofadmission = (TextView) findViewById(R.id.tv_dateofadmission);
        address = (TextView) findViewById(R.id.tv_address);
        contact = (TextView) findViewById(R.id.tv_contact);
        emergencycontact = (TextView) findViewById(R.id.tv_emergencycontact);
        email = (TextView) findViewById(R.id.tv_email);
        occupation = (TextView) findViewById(R.id.tv_occupation);
        bloodtype = (TextView) findViewById(R.id.tv_bloodtype);
        refferedvia = (TextView) findViewById(R.id.tv_refferedvia);

        btnedit = (Button) findViewById(R.id.btedit);
        btnedit.setOnClickListener(this);

    

    @Override
    protected void onResume() 
        // TODO Auto-generated method stub
        super.onResume();

        Bundle v = getIntent().getExtras();

        if(v==null)
            back();
            return;
        

        userId = v.getString(getString(R.string.valuesId));     
        loadData(userId);

    

    private void loadData(String id)

            User u = (User) data.getUser(id);
            Toast.makeText(this, u.getName(), Toast.LENGTH_LONG).show();            
            name.setText(u.getName().toString());           
            age.setText(u.getAge());
            gender.setText(u.getGender());
            height.setText(u.getHeight());
            weight.setText(u.getWeight());
            dateofbirth.setText(u.getDateofbirth());
            religion.setText(u.getReligion());          
            maritalstatus.setText(u.getMaritalstatus());
            dateofadmission.setText(u.getDateofadmission());
            contact.setText(u.getContact());
            emergencycontact.setText(u.getEmergencycontact());
            email.setText(u.getEmail());
            occupation.setText(u.getOccupation());          
            bloodtype.setText(u.getBloodtype());
            refferedvia.setText(u.getRefferedvia());
            address.setText(u.getAddress());

    


    @Override
    public void onClick(View v) 
        // TODO Auto-generated method stub
        switch(v.getId())
        case R.id.btedit:


        
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        // TODO Auto-generated method stub
        switch (item.getItemId()) 
            case android.R.id.home:
                back();
                return true;                
            default:
                return super.onOptionsItemSelected(item);
        

    

    private void back()
        Intent i = new Intent(this, UserActivity.class);
        startActivity(i);
    


活动 C

 @Override
    protected void onResume() 
        // TODO Auto-generated method stub
        super.onResume();

        Bundle v = getIntent().getExtras();

        if(v==null)
            back();
            return;
        

        userId = v.getString(getString(R.string.valuesId));     
        loadData(userId);

    

    private void loadData(String id)

            User u = (User) data.getUser(id);
            Toast.makeText(this, u.getName(), Toast.LENGTH_LONG).show();            
            name.setText(u.getName().toString());           
            age.setText(u.getAge());
            gender.setText(u.getGender());
            height.setText(u.getHeight());
            weight.setText(u.getWeight());
            dateofbirth.setText(u.getDateofbirth());
            religion.setText(u.getReligion());          
            maritalstatus.setText(u.getMaritalstatus());
            dateofadmission.setText(u.getDateofadmission());
            contact.setText(u.getContact());
            emergencycontact.setText(u.getEmergencycontact());
            email.setText(u.getEmail());
            occupation.setText(u.getOccupation());          
            bloodtype.setText(u.getBloodtype());
            refferedvia.setText(u.getRefferedvia());
            address.setText(u.getAddress());


    


    private void updateUser(String id)
        String[] ids = id;
        try
            data.updateUser(ids,name.getText().toString(), age.getText().toString(), gender.getText().toString(), height.getText().toString(), weight.getText().toString(), dateofbirth.getText().toString()
                    ,religion.getText().toString(), maritalstatus.getText().toString(), dateofadmission.getText().toString(), address.getText().toString(), contact.getText().toString(), emergencycontact.getText().toString()
                    ,email.getText().toString(), occupation.getText().toString(), bloodtype.getText().toString(), refferedvia.getText().toString());                        
            Toast.makeText(this, getString(R.string.good), Toast.LENGTH_LONG).show();           
        catch(Exception e)
            Toast.makeText(this, getString(R.string.error), Toast.LENGTH_LONG).show();
           
    

    private void deleteUser(String id)
        String[] ids = id;
        try
            data.deleteUser(ids);                       
            Toast.makeText(this, getString(R.string.good), Toast.LENGTH_LONG).show();
            back();
        catch(Exception e)
            Toast.makeText(this, getString(R.string.error), Toast.LENGTH_LONG).show();
           
    



    @Override
    public void onClick(View v) 
        // TODO Auto-generated method stub
        switch(v.getId())
        case R.id.btupdate:
            updateUser(userId);
            break;
        case R.id.btdelete:
            deleteUser(userId);
            break;
        
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        // TODO Auto-generated method stub
        switch (item.getItemId()) 
            case android.R.id.home:
                back();
                return true;                
            default:
                return super.onOptionsItemSelected(item);
        

    

    private void back()
        Intent i = new Intent(this, UserActivity.class);
        startActivity(i);
    


【问题讨论】:

【参考方案1】:

你已经得到了答案。它与您在 Activity B 中添加到 Intent 的 id extra 非常相似。

在 A 中,执行以下操作:

Intent i = new Intent(ActivityA.this, ActivityB.class);
i.putExtra(getString(R.string.valuesId), String.valueOf(u.getId()));
i.putExtra("position", position);

在 B 中,将其提取为:

Bundle extras = getIntent().getExtras();
int position = extras.getInt("position");
Intent i = new Intent(ActivityB.this, ActivityC.class);
i.putExtra(getString(R.string.valuesId), String.valueOf(u.getId()));
i.putExtra("position", position);

在C中,像以前一样提取位置:

Bundle extras = getIntent().getExtras();
int position = extras.getInt("position");

【讨论】:

为什么?这是 Android 希望您在活动之间传递数据的方式 请看上面的编辑代码,告诉我把你提供的这些代码放在哪里 这些行就在您执行 startActivity(i) 之前。所以在 A 中,它将位于 onItemClick() 方法中。在 B 和 C 中,它将在 back() 方法中。只需添加我编写的代码行,而你还没有,你应该很高兴。 对我不起作用..仍然给出错误...在活动 BI 中必须将此代码放在案例 R.id.btedit 之后:我认为在活动 C 中 back() 方法不正确在我看来这个地方......可能是它在简历活动中????【参考方案2】:

您必须从变量中的 Activity B 中的意图包中检索值,然后将该值传递到进入活动 C 的新意图包中

【讨论】:

通过第二个活动从数据库传递列表视图的项目位置

】通过第二个活动从数据库传递列表视图的项目位置【英文标题】:Passingitempositionoflistviewfromdatabasethrough2ndactivity【发布时间】:2015-03-2712:22:08【问题描述】:您好,我创建了一个可以保存笔记的日记。我有3个活动,比如说A、B... 查看详情

无法快速显示第一个视图控制器将值传递给第二个视图控制器

】无法快速显示第一个视图控制器将值传递给第二个视图控制器【英文标题】:Unabletoshowfirstviewcontrollerpassedvaluestosecondviewcontrollerinswift【发布时间】:2020-08-1110:19:51【问题描述】:我的第二个viewcontroller包含mkmapview..所有时间地... 查看详情

Django:将请求直接(内联)传递给第二个视图

】Django:将请求直接(内联)传递给第二个视图【英文标题】:Django:Passingarequestdirectly(inline)toasecondview【发布时间】:2009-12-1213:39:17【问题描述】:我正在尝试直接从另一个视图调用视图(如果可能的话)。我有一个看法:defpro... 查看详情

执行 segue 时将字符串传递给第二个视图控制器的 UILabel

】执行segue时将字符串传递给第二个视图控制器的UILabel【英文标题】:Passingastringtosecondviewcontroller\'sUILabelwhenperformingsegue【发布时间】:2017-02-1008:34:37【问题描述】:我正在尝试使用calloutAccessoryControlTapped方法将注释的标题传递... 查看详情

使用自定义视图将字符串数组传递给另一个活动

...时间】:2017-05-2615:04:57【问题描述】:我在将数组传递到列表视图时遇到问题我可以将图像传递给第二个活动,但是当我尝试将数组传递给列表视图我得到一个lang.NullPointerException错误并且应用崩溃了。任何有用的意见都会非常... 查看详情

如何将一个查询的结果传递给第二个查询并将结果显示在一个表中?

...一个查询的结果传递到第二个。我收到错误“致命错误:不能使用CI_DB_mysql_result类型的对象作 查看详情

Swift 3:如何动态加载图像并将变量传递给第二个视图控制器?

】Swift3:如何动态加载图像并将变量传递给第二个视图控制器?【英文标题】:Swift3:Howtoloadimagesdynamicallyandpassvariablestothesecondviewcontroller?【发布时间】:2017-03-1702:00:48【问题描述】:我正在尝试从URL加载图像以动态加载到表格视... 查看详情

如何将值传递给第二个控制器

】如何将值传递给第二个控制器【英文标题】:HowtopassvaluetosecondController【发布时间】:2016-04-0617:44:23【问题描述】:您好,我想将值传递给我的第二个Controller。第二个Controller连接到NavigationController,并且情节提要Id已在Navigation... 查看详情

SwiftUI 为啥不能在视图之间传递发布者?

】SwiftUI为啥不能在视图之间传递发布者?【英文标题】:SwiftUIWhyCan\'tpassapublisherbetweenViews?SwiftUI为什么不能在视图之间传递发布者?【发布时间】:2020-11-1103:17:36【问题描述】:我只是想做一些这样的测试↓从第一个视图创建一... 查看详情

无法将 swift 文件分配给第二个视图

】无法将swift文件分配给第二个视图【英文标题】:Can\'tassignaswiftfiletothesecondview【发布时间】:2020-08-2518:13:05【问题描述】:我创建了第二个视图,当我想分配一个新的swift文件作为它的类时,它没有这样做。这是一个错误吗?... 查看详情

我想在自定义列表视图中选择一项,该项目将显示在第二个活动的文本视图上

】我想在自定义列表视图中选择一项,该项目将显示在第二个活动的文本视图上【英文标题】:Iwantselectoneitemincustomlistviewandthatitemwillgetdisplayontextviewofsecondactivity【发布时间】:2020-04-0704:24:19【问题描述】:我在我的程序中使用... 查看详情

使用android中的可序列化将对象从第二个活动传递回主要活动

...其中创建了第二个活动的意图。在这个活动中,我在一个列表视图中显示了费用,由于它没有完全实现,所以现在我已经 查看详情

多重突变:如何让第一个突变将 ID 传递给第二个突变

】多重突变:如何让第一个突变将ID传递给第二个突变【英文标题】:Multiplemutation:howtohavefirstmutationpassIDtosecondmutation【发布时间】:2020-06-0208:55:54【问题描述】:问题问题已解决,见下文我希望使用firstmutation创建的ID用于第二次... 查看详情

Android:将listView项目位置传递给下一个活动

...ity【发布时间】:2015-07-2817:35:44【问题描述】:我需要将列表视图单击的行的索引传递给下一个活动。现在我只能通过单击列表视图项打开一个活动,但我尝试多次传递索引,但没有成功。顺便说一下,我的列表视图是带有自定... 查看详情

如何将第二个viewcontroller文本字段值传递给第一个viewcontroller数组并在swift4中点击按钮时关闭

...ft4【发布时间】:2019-05-0809:06:06【问题描述】:我有两个视图控制器,我想将第 查看详情

通过活动 1 时刷新活动 2 中的列表视图

】通过活动1时刷新活动2中的列表视图【英文标题】:Refreshlistviewinactivity2whengoingthroughacitivity1【发布时间】:2014-04-0706:10:44【问题描述】:在我的应用程序中有两个活动,在第二个活动中有列表视图。列表视图正在使用自定义光... 查看详情

将参数传递给视图转换

】将参数传递给视图转换【英文标题】:Passaparamethertoaviewtransition【发布时间】:2011-02-1412:04:10【问题描述】:我有一个类管理两个视图之间的转换。在第一个视图中,我有一个章节表,在第二个视图中,我有每章的文本。现在... 查看详情

使用捆绑将数据从一个活动传递到另一个活动 - 不在第二个活动中显示

】使用捆绑将数据从一个活动传递到另一个活动-不在第二个活动中显示【英文标题】:Passingdatafromoneactivitytoanotherusingbundle-notdisplayinginsecondactivity【发布时间】:2013-03-0422:07:16【问题描述】:我目前正在尝试获取通过RESTAPI调用获... 查看详情