【问题标题】:I want to display the contents of that specific text view when i click on the text view on a different activity. I当我单击不同活动的文本视图时,我想显示该特定文本视图的内容。一世
【发布时间】:2015-11-15 14:21:18
【问题描述】:

这是我的主要活动 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);


            }

        });

    }}

【问题讨论】:

    标签: java android arraylist serializable


    【解决方案1】:

    取消注释这两行:

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

    然后使用以下命令将其可见性设置为 GONE:

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

    或使用 xml。

    通过这种方式,您可以在列表视图中仅显示一个姓名,但您还可以获得地址和联系方式以继续进行另一项活动。

    【讨论】:

      【解决方案2】:

      那堂课似乎还可以。但是你应该让你的类Colleges实现Parcelableinterface。

      错误必须在CollegeDetails类中

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-07
        • 1970-01-01
        • 1970-01-01
        • 2018-08-19
        相关资源
        最近更新 更多