简体   繁体   English

当我在其他活动上单击文本视图时,我想显示该特定文本视图的内容。 一世

[英]I want to display the contents of that specific text view when i click on the text view on a different activity. I

Here is my main activity Collegelist with the contents already loaded from the database but i have only displayed the name.When i click on the name i want to show the name,address and contact on different activity. 这是我的主要活动Collegelist,其内容已经从数据库加载,但是我只显示了名称。当我单击名称时,我想显示名称,地址和其他活动的联系方式。

public class Collegelist extends ActionBarActivity {

    HTTPConnection http;

    List<Colleges> college = new ArrayList<Colleges>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.college_list);
        http = new HTTPConnection(getApplicationContext());
        if (http.isNetworkConnection()) {

            //String data = http.HTTPGetData("http://localhost/minorproject/show.php");
            //Toast.makeText(getApplicationContext(),data ,Toast.LENGTH_LONG).show();
            task.execute();
        }
        else {


            Toast.makeText(getApplicationContext(), "check your connection",
                    Toast.LENGTH_LONG).show();
        }
    }


    AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {

        @Override
        protected String doInBackground(Void... params) {
            String data = http.HTTPGetData("http://localhost/college/show.php");
            return data;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            populateList(result);
            displayList();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

    };
    protected void populateList(String result) {
        try {
            //Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();
            JSONObject jobj = new JSONObject(result);
            String res = jobj.getString("success");
            if (!res.equals("true")) {
                Toast.makeText(getApplicationContext(), "JSON Error",
                        Toast.LENGTH_LONG).show();
                return;
            }
            else
            {
                JSONArray data = jobj.getJSONArray("msg");
            //  Toast.makeText(getApplicationContext(),"successss",Toast.LENGTH_SHORT).show();
                for (int i = 0; i < data.length(); i++) {
                    JSONObject col = data.getJSONObject(i);
                    Colleges cg = new Colleges(col.getString("cname"), col.getString("caddress"), col.getString("ccontact_a"));
                    college.add(cg);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            // TODO: handle exception
        }


    }


    protected void displayList() {
        ArrayAdapter<Colleges> adapter = new ArrayAdapter<Colleges>(this, R.layout.list_item,college){

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = getLayoutInflater().inflate(R.layout.list_item,null);
                //set values
                Colleges c = college.get(position);
                ((TextView)view.findViewById(R.id.name)).setText(c.getName());
            /*  ((TextView)view.findViewById(R.id.address)).setText(c.getAddress());
                ((TextView)view.findViewById(R.id.contact)).setText(c.getContact());
                */
                return view;
            }

        };

        final ListView collegelistnew = (ListView) findViewById(R.id.listView);
        collegelistnew.setAdapter(adapter);

        collegelistnew.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                    long arg3) {
                /*Toast.makeText(
                        getApplicationContext(),
                        "You clicked position" + position + "with item name"
                                + college.get(position).getName(),
                        Toast.LENGTH_LONG).show();*/


                Intent newIntent =new Intent(getApplicationContext(),CollegeDetails.class);

                newIntent.putExtra("college", (Serializable) college.get(position));
                startActivity(newIntent);


            }

        });

    }}

Uncomment these two lines : 取消注释这两行:

/*  ((TextView)view.findViewById(R.id.address)).setText(c.getAddress());
            ((TextView)view.findViewById(R.id.contact)).setText(c.getContact());
            */

Then set their visibility to GONE using: 然后使用以下命令将其可见性设置为GONE:

((TextView)view.findViewById(R.id.address)).setVisibility(View.GONE);
((TextView)view.findViewById(R.id.contact)).setVisibility(View.GONE);

or using xml. 或使用xml。

And in this way you can show only one name in your list view but you get the address and contact also to carry on to another activty. 这样,您可以在列表视图中仅显示一个名称,但是您可以获取地址并联系以继续进行另一项活动。

That class seems to be OK. 那堂课似乎还可以。 However you should make your class Colleges to implement Parcelable interface. 但是,您应该使您的班级Colleges实现Parcelable接口。

The error must be in CollegeDetails class 该错误必须在CollegeDetails类中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我单击活动时,我在其中有3个文本视图。 我想2秒的差距后加载文本视图一个又一个什么样的? - When I click on the activity I have 3 Text View in that. I want to load the Text View one after another like after a gap of 2 seconds? 我想在Android活动中制作一个带有图像的可滚动文本列表。 我应该使用什么? - I want to make a scrollable list of text with images in an Android Activity. What should I use? 我想要如图所示的轮廓文本字段,未单击edittext或微调器我想要与按钮单击上的视图相同 - I want outlined text field with as shown in picture not clicked on edittext or spinner i want same as it is view on Button click 我想用文本视图创建一个计数器 - i want to create a counter with a text view 我有一个列表视图。如何将列表视图的点击总数保存到另一个活动文本视图? - I have a list view.how i will save list view total number of click to another activity text view? 我希望通过单击文本视图时出现的对话框在文本视图中显示数字 - I want numbers to be displayed in a text view from a dialog which appear when clicked on text view 我无法从一个卡片视图活动中导航到多个活动。 我可以导航到“仅1个” - I cannot navigate to multiple activities from a single Card View Activity. I can navigate to Only 1 如何从其他活动更改视图。 - How to change a view from a different activity. 如何通过单击更改文本视图中的文本? - How can i change the text in text view by click? 我有一个 Recycler View,onRun 会显示,但是单击按钮后它将是空的,我想设置一个默认的 recycler 视图 - I have a Recycler View, onRun will display, but upon a button click it will be empty, I want to set a default recycler view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM