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

时间:2015-08-21 15:04:16

标签: java android arraylist serializable

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


            }

        });

    }}

2 个答案:

答案 0 :(得分:0)

取消注释这两行:

trim

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

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

或使用xml。

通过这种方式,您只能在列表视图中显示一个名称,但您也可以获得地址和联系人以继续进行其他活动。

答案 1 :(得分:0)

那个班似乎没问题。但是,您应该让您的班级Colleges实施Parcelable界面。

错误必须在CollegeDetails class