在扩展Listview上设置onClick侦听器

时间:2015-05-05 09:18:50

标签: android email android-listview

  

我正在处理电子邮件应用程序。我能够获得Inbox Mail   列表显示。现在我必须单击列表并打开该特定电子邮件   在不同的活动中。但我不明白如何设置Onclick   列表视图。

     

这是我的收件箱邮件活动代码。

  public class InboxActivity extends ListActivity {
    // Progress Dialog
    private ProgressDialog pDialog;

    public static String uid;
    public static String uri;
    SharedPreferences myPriferene2;
    // Creating JSON Parser object
    JSONParser jsonParser = new JSONParser();

    ArrayList<HashMap<String, String>> inboxList;

    // products JSONArray
    JSONArray inbox = null;

    // Inbox JSON url
    private static final String INBOX_URL = "http://xyz/webservice/inboxlist.php";

    // ALL JSON node names
    private static final String TAG_MESSAGES = "messages";
    private static final String TAG_ID = "id";
    private static final String TAG_FROM = "from";
    private static final String TAG_EMAIL = "email";
    private static final String TAG_SUBJECT = "subject";
    //private static final String TAG_DATE = "date";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.inbox_list);

        // Hashmap for ListView
        inboxList = new ArrayList<HashMap<String, String>>();



        myPriferene2=getSharedPreferences("my_key_share", MODE_PRIVATE);

        uri = myPriferene2.getString("uid","");


        String uid =uri.toString();

        Log.d("helloooooo",uid);
        // Loading INBOX in Background Thread
        new LoadInbox().execute(uid);
    }

    /**
     * Background Async Task to Load all INBOX messages by making HTTP Request
     * */
    class LoadInbox extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(InboxActivity.this);
            pDialog.setMessage("Loading Inbox ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        /**
         * getting Inbox JSON
         * */
        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();

            params.add(new BasicNameValuePair("uid", args[0]));

            // getting JSON string from URL
            JSONObject json = jsonParser.makeHttpRequest(INBOX_URL, "GET",params);

            // Check your log cat for JSON reponse
            Log.d("Inbox JSON: ", json.toString());

            try {
                inbox = json.getJSONArray(TAG_MESSAGES);
                // looping through All messages
                for (int i = 0; i < inbox.length(); i++) {
                    JSONObject c = inbox.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString(TAG_ID);
                    String from = c.getString(TAG_FROM);
                    String subject = c.getString(TAG_SUBJECT);
                    //String date = c.getString(TAG_DATE);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_ID, id);
                    map.put(TAG_FROM, from);
                    map.put(TAG_SUBJECT, subject);
                    //map.put(TAG_DATE, date);

                    // adding HashList to ArrayList
                    inboxList.add(map);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(
                            InboxActivity.this, inboxList,
                            //R.layout.inbox_list_item, new String[] { TAG_FROM, TAG_SUBJECT, TAG_DATE },
                            R.layout.inbox_list_item, new String[] { TAG_FROM, TAG_SUBJECT,TAG_ID},
                            //new int[] { R.id.from, R.id.subject, R.id.emailID});

                    new int[] { R.id.from, R.id.subject , R.id.date});
                    // updating listview
                    setListAdapter(adapter);
                }
            });
        }
    }
}
  

这是我的ListActivity

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
  

这是我的列表项活动

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#746555"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/from"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="8dip"
        android:paddingLeft="8dip"
        android:paddingBottom="4dip"
        android:textSize="20dip"
        android:textStyle="bold" />


    <TextView android:id="@+id/subject"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:paddingLeft="8dip"
        android:paddingBottom="6dip"
        android:textSize="15dip"
        android:layout_below="@id/from"/>


    <TextView android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:padding="8dip"/>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

在您的活动中覆盖onListItemClick。

protected void onListItemClick(ListView l, View v, int position, long id) {
        Intent intent = new Intent(InboxActivity.this, NewActivity.class);
        startActivity(intent)
    }