通过第二个活动将listview的项目位置从数据库传递

时间:2015-01-28 14:41:10

标签: android eclipse listview

嗨我创建了一本可以保存笔记的日记。我有3个活动让我们说A,B和C活动。现在我将列表视图中的活动A中编辑过的笔记保存起来,当点击项目位置时,它会转到活动B,向我显示我在编辑框中输入的TextView格式。现在在Activity B中向我展示了TextView注释以及一个编辑按钮,我可以在其中编辑或更新Activity C中的相同数据。所以我希望当我点击Activity B中的编辑/更新按钮时,它会传递列表的项目位置保存在活动A中。简而言之,我想通过活动B将项目位置从A传递到C.我可以成功地从A传递到B但是不能从B传递到C.请提供代码以便它很容易我。这是一些代码。

活动A

@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 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(ActivityA.this, ActivityB.class);
        i.putExtra(getString(R.string.valuesId), String.valueOf(u.getId()));
        i.putExtra("position", position);
        startActivity(i);       
    }



    }

这里是活动B

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

    }

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case R.id.btedit:
            Bundle extras = getIntent().getExtras();
            int position = extras.getInt("position");
            Intent i = new Intent(ActivityB.this, ActivityC.class);
            i.putExtra("position", position);   
            startActivity(i);
            break;
        }
    }

这里是活动C

    data = new AccessModel(this);

        name = (EditText) findViewById(R.id.mn_name);
        age = (EditText) findViewById(R.id.mn_age);
        gender = (EditText) findViewById(R.id.mn_gender);
        height = (EditText) findViewById(R.id.mn_height);
        weight = (EditText) findViewById(R.id.mn_weight);
        dateofbirth = (EditText) findViewById(R.id.mn_dob);

        btnupdate = (Button) findViewById(R.id.btupdate);
        btndelete = (Button) findViewById(R.id.btdelete);               

        btnupdate.setOnClickListener(this);
        btndelete.setOnClickListener(this);

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Bundle extras = getIntent().getExtras();
        if(extras==null){
            back();
            return;
        }

        userId = extras.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());        
    }


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

            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, ActivityA.class);
        startActivity(i);
    }

}

2 个答案:

答案 0 :(得分:1)

这很简单。

在活动B中右键传递字符串

i.putExtra("message", userId);

在活动C中,得到这个

userId  = extras.getString("message");

答案 1 :(得分:0)

你正在过去&#34;位置&#34;从BC,但在userId = extras.getString(getString(R.string.valuesId));中阅读C ...

我想B应该userId传递给C而不是position?!?!?

更新: 活动B:

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case R.id.btedit:
            Bundle extras = getIntent().getExtras();
            int position = extras.getInt("position");
            Intent i = new Intent(ActivityB.this, ActivityC.class);

            // You pass "position" here
            i.putExtra("position", position);   
            startActivity(i);
            break;
        }
    }

所以你通过&#34;位置&#34;到活动C,但在活动C中你读到:

@Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Bundle extras = getIntent().getExtras();
        if(extras==null){
            back();
            return;
        }

        // Here you don't read "position" but R.string.valuesId ?!?!?!?
        userId = extras.getString(getString(R.string.valuesId));        
        loadData(userId);

    }