簡體   English   中英

當我在其他活動上單擊文本視圖時,我想顯示該特定文本視圖的內容。 一世

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

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


            }

        });

    }}

取消注釋這兩行:

/*  ((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。

這樣,您可以在列表視圖中僅顯示一個名稱,但是您可以獲取地址並聯系以繼續進行另一項活動。

那堂課似乎還可以。 但是,您應該使您的班級Colleges實現Parcelable接口。

該錯誤必須在CollegeDetails類中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM