简体   繁体   中英

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.

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:

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

or using 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.

The error must be in CollegeDetails class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM