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

Posted

技术标签:

【中文标题】不能将列表视图的位置传递给第二个活动?【英文标题】: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 的新意图包中

【讨论】:

以上是关于不能将列表视图的位置传递给第二个活动?的主要内容,如果未能解决你的问题,请参考以下文章

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

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

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

为啥数据不能传递给第二个活动

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

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