如何在另一个活动中显示不同的字符串?

时间:2018-03-25 09:42:17

标签: android listview onclicklistener

我正在尝试根据listview listitem点击显示textview的不同文本。目前我正在创建许多新活动来完成这项任务。以下代码就是其中一种情况。有没有简单的方法来显示相同​​活动(BSC活动)中的不同文本。我不想创建Post Activity。有关示例如果listview position == 0项目已单击

  

R.String.Bsc

必须在bsc Activity中显示。如果position == 1点击了

  

R.String.Post

必须在bsc活动中显示。

ListView类

public class AHSMLS extends AppCompatActivity{

    ListView list;
    String[] itemname ={
            "Degree in Physiotherapy",
            "Post Graduate options"

    };

    Integer[] imgid={
            R.drawable.mlsico,
            R.drawable.mls
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cp_listview_main_activity);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("Physiotherapy");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        ImageView img = (ImageView)findViewById(R.id.thumbnail);
        img.setImageResource(R.drawable.physiotherapy);

        CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
        list=(ListView)findViewById(R.id.list);
        list.setAdapter(adapter);

        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub

                if (position == 0) {

                    Intent intent = new Intent(getApplicationContext(), Bsc.class);
                    startActivity(intent);

                }else if (position == 1){

                    Intent intent = new Intent(getApplicationContext(), Post.class);
                    startActivity(intent);

                }
            }
        });
    }
}

Bsc活动

public class Bsc extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multiple_text_view);

        TextView tv3 = (TextView)findViewById(R.id.durationtextView);
        tv3.setText("•\t 3 or 4 Year");

        TextView tv4 = (TextView)findViewById(R.id.institutiontextView);
        tv4.setText(R.string.bsc);


    }
}

发布活动

public class Post extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_multiple_text_view);

            TextView tv3 = (TextView)findViewById(R.id.durationtextView);
            tv3.setText("•\t 3 or 4 Year");

            TextView tv4 = (TextView)findViewById(R.id.institutiontextView);
            tv4.setText(R.string.Post);


        }
    }

2 个答案:

答案 0 :(得分:1)

如果我的问题正确的话。这是你要找的吗?有了这个解决方案,你就不需要你的课程。您可以使用Bsc类更改文本。

ListView类

    public class AHSMLS extends AppCompatActivity{

        ListView list;
        String[] itemname ={
                "Degree in Physiotherapy",
                "Post Graduate options"

        };

        Integer[] imgid={
                R.drawable.mlsico,
                R.drawable.mls
        };
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.cp_listview_main_activity);

            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            toolbar.setTitle("Physiotherapy");
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

            ImageView img = (ImageView)findViewById(R.id.thumbnail);
            img.setImageResource(R.drawable.physiotherapy);

            CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
            list=(ListView)findViewById(R.id.list);
            list.setAdapter(adapter);

            list.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    // TODO Auto-generated method stub

                Intent intent = new Intent(getApplicationContext(), Bsc.class);
                Bundle bundle = new Bundle();

                //Add your data to bundle
                bundle.putInt("x", position);

                //Add the bundle to the intent
                intent.putExtras(bundle);
                startActivity(intent);

                    }
                }
            });
        }
    }

Bsc活动

 public class Bsc extends AppCompatActivity {

    TextView tv3;
    TextView tv4;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_multiple_text_view);

         tv3 = (TextView)findViewById(R.id.durationtextView);
         tv4 = (TextView)findViewById(R.id.institutiontextView);

        //Get the bundle
        Bundle bundle = getIntent().getExtras();

        //Extract the data…
        int stuff = bundle.getInt("x");

        if(stuff == 0){

            tv3.setText("•\t 3 or 4 Year");
            tv4.setText(R.string.bsc);

    }else if(stuff == 1){

            tv3.setText("•\t 3 or 4 Year");
            tv4.setText(R.string.Post);
}

        }
    }

所以你不需要你的帖子活动。祝你好运兄弟!

答案 1 :(得分:0)

(对于糟糕的格式化感到抱歉......在粘贴代码时仍然没有弄清楚如何保留缩进)

Anyhoo ....我认为这就是Reaz Murshed试图说的:

public class AHSMLS extends AppCompatActivity{

// START NEW CODE
TextView tv3;
TextView tv4;
// END NEW CODE



ListView list;
String[] itemname ={
        "Degree in Physiotherapy",
        "Post Graduate options"

};

Integer[] imgid={
        R.drawable.mlsico,
        R.drawable.mls
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cp_listview_main_activity);


    // START NEW CODE
    tv3 = (TextView)findViewById(R.id.durationtextView);
    tv3.setText("•\t 3 or 4 Year");

    tv4 = (TextView)findViewById(R.id.institutiontextView);

    // END NEW CODE




    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("Physiotherapy");
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    ImageView img = (ImageView)findViewById(R.id.thumbnail);
    img.setImageResource(R.drawable.physiotherapy);

    CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
    list=(ListView)findViewById(R.id.list);
    list.setAdapter(adapter);

    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // TODO Auto-generated method stub

            if (position == 0) {

                // START NEW CODE
                tv4.setText(R.string.bsc);

            }else if (position == 1){

                tv4.setText(R.string.Post);
                // END NEW CODE

            }
        }
    });
}
}