如何将listview中的项目分隔到不同的页面?

时间:2014-01-15 07:02:57

标签: android eclipse listview android-listview

我有一个列表视图,其中3个不同的部分都链接在一起,图像,文本框和另一个图像。现在,当我点击列表视图时,它将转到另一个页面。如果用户单击图像,我希望它将其指向另一个页面。请帮忙!!以下是类代码。

        package com.example.splashscreentwo;

import android.app.ListActivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class AllFTemployeesActivity extends ListActivity {

    LayoutInflater inflater;
     // Progress Dialog
   private ProgressDialog pDialog;

   // Creating JSON Parser object
   JSONParser jParser = new JSONParser();

   ArrayList<HashMap<String, String>> employeesList;

   // url to get all fulltime employees list
   private static String url_employees = "http://rollit.sg/FYP/Existing_employees_FullTime_1.php";

   // JSON Node names
   private static final String TAG_SUCCESS = "success";
   private static final String TAG_EMPLOYEE = "employee";
   private static final String TAG_PID = "pid";
   private static final String TAG_NAME = "name";
   private static final String TAG_DESCRIPTION = "description";


   // employees JSONArray
   JSONArray employee = null;
    private ListView listview;

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

       final GlobalClass globalVariable = (GlobalClass) getApplicationContext();

       ImageButton ButtonPT = (ImageButton) findViewById(R.id.btnpt);
       ImageButton BtnAddEmployee = (ImageButton) findViewById(R.id.addEmployeeBtn);
       ImageButton BtnSettings = (ImageButton) findViewById(R.id.settingsBtn);

       BtnAddEmployee.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View view) {
               // Launching emplopyee regstration Activity
               Intent i = new Intent(getApplicationContext(), RegisterEmployeeActivity.class);
               startActivity(i);

               finish();

           }
       });

       BtnSettings.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View view) {
               // Launching emplopyee regstration Activity
               Intent i = new Intent(getApplicationContext(), Settings.class);
               startActivity(i);

               finish();

           }
       });


       ButtonPT.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View view) {
               // Launching All fulltime employees Activity
               Intent i = new Intent(getApplicationContext(), AllPTemployeesActivity.class);
               startActivity(i);

               finish();

           }
       });

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

       // Loading all fulltime employees in Background Thread
       new LoadAllEmployees().execute();

       // Get listview
       ListView lv = getListView();

       // on seleting single employee
       // launching Edit Employee Screen
       lv.setOnItemClickListener(new OnItemClickListener() {

           @Override
           public void onItemClick(AdapterView<?> parent, View view,
                   int position, long id) {
               // getting values from selected ListItem
               String pid = ((TextView) view.findViewById(R.id.pid)).getText()
                       .toString();

               globalVariable.setEmployeeId(pid);

               ImageButton PartTimeHrs = (ImageButton) findViewById(R.id.btnpt);
               PartTimeHrs.setOnClickListener(new View.OnClickListener() {


                   @Override
                   public void onClick(View view) {

                       // Launching All fulltime employees Activity
                       Intent i = new Intent(getApplicationContext(), EmployeeDetails.class);
                       startActivity(i);


                   }
               });
               // Starting new intent
              Intent in = new Intent(getApplicationContext(),
                      EmployeeDetails.class);
               // sending pid to next activity
               //in.putExtra(TAG_PID, pid);

               // starting new activity and expecting some response back
               startActivityForResult(in, 100);
           }
       });

   }

   // Response from Edit single fulltime employee Activity
   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       super.onActivityResult(requestCode, resultCode, data);
       // if result code 100
       if (resultCode == 100) {
           // if result code 100 is received
           // means user edited/deleted employee
           // reload this screen again
           Intent intent = getIntent();
           finish();
           startActivity(intent);


       }

   }

   /**
    * Background Async Task to Load all fulltime employees by making HTTP Request
    * */
   class LoadAllEmployees extends AsyncTask<String, String, String> {

       /**
        * Before starting background thread Show Progress Dialog
        * */
       @Override
     //  protected void onPreExecute() {
        //   super.onPreExecute();
         //  pDialog = new ProgressDialog(AllEmployeesActivity.this);
         //  pDialog.setMessage("Loading Employees. Please wait...");
          // pDialog.setIndeterminate(false);
         //  pDialog.setCancelable(false);
         //  pDialog.show();
  //     }
//
       /**
        * getting All fulltime employees from url
        * */
       protected String doInBackground(String... args) {
           // Building Parameters
           List<NameValuePair> params = new ArrayList<NameValuePair>();
           // getting JSON string from URL
           JSONObject json = jParser.makeHttpRequest(url_employees, "GET", params);



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

           try {
               // Checking for SUCCESS TAG
               int success = json.getInt(TAG_SUCCESS);

               if (success == 1) {
                   // fulltime employees found
                   // Getting Array of fulltime employees
                   employee = json.getJSONArray(TAG_EMPLOYEE);

                   // looping through All fulltime employees
                   for (int i = 0; i < employee.length(); i++) {
                       JSONObject c = employee.getJSONObject(i);

                       // Storing each json item in variable
                       String id = c.getString(TAG_PID);
                       String name = c.getString(TAG_NAME);
                       String description= c.getString(TAG_DESCRIPTION);


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

                       // adding each child node to HashMap key => value
                       map.put(TAG_PID, id);
                       map.put(TAG_NAME, name);
                      map.put(TAG_DESCRIPTION, description);





                       // adding HashList to ArrayList
                       employeesList.add(map);
                   }
               } else {

               }
           } 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 employees
         //  pDialog.dismiss();


           // updating UI from Background Thread
           runOnUiThread(new Runnable() {
               public void run() {
                   /**
                    * Updating parsed JSON data into ListView
                    * */
                   ListAdapter adapter = new SimpleAdapter(
                           AllFTemployeesActivity.this, employeesList,
                           R.layout.list_item_ft, new String[] { TAG_PID,
                                   TAG_NAME,TAG_DESCRIPTION},

                           new int[] { R.id.pid, R.id.name, R.id.description});



                   // updating listview
                   setListAdapter(adapter);
               }
           });

       }


   }

}

那么有没有办法让用户点击列表视图中指向另一个页面的某些部分?任何帮助表示赞赏!!以下是相关的xml文件。

        <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" 
    android:weightSum="100" 
    android:background="#ffffff">
     <LinearLayout   
         android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="5"
        android:paddingBottom="10dp"

        >

     <View
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
</View>

     </LinearLayout>

   <LinearLayout   
         android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20"
        android:paddingBottom="10dp" >


         <ImageView
            android:id="@+id/img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="fitXY"
    android:adjustViewBounds="true"
      android:src="@drawable/thorb"/>

        </LinearLayout>

     <!-- text1 and text2 -->
    <LinearLayout   
        android:layout_width="0dp"
         android:layout_height="match_parent"
        android:layout_weight="60"
        android:paddingBottom="10dp"
        android:paddingLeft="20dp"
>



         <LinearLayout   
        android:layout_width="match_parent"
         android:layout_height="match_parent"
        android:weightSum="100"
        android:paddingBottom="10dp"
        android:paddingLeft="20dp"
        android:orientation="vertical">

               <LinearLayout   
         android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="20"
        android:paddingBottom="10dp"

        >

     <View
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
</View>

     </LinearLayout>

                 <LinearLayout   
        android:layout_width="match_parent"
         android:layout_height="0dp"
        android:layout_weight="20"
        >
            <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             android:layout_gravity="center"
            android:textColor="@android:color/black"
            />
            </LinearLayout>

             <!-- text1 -->
             <LinearLayout   
        android:layout_width="match_parent"
         android:layout_height="0dp"
        android:layout_weight="20"
        >

                  <TextView  android:id="@+id/pid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"/>



         </LinearLayout>



         <!-- text2 -->
              <LinearLayout   
        android:layout_width="match_parent"
         android:layout_height="0dp"
        android:layout_weight="20"
        >
            <TextView
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             android:layout_gravity="center"
             android:textColor="@android:color/black"

            />
            </LinearLayout>








               <LinearLayout   
         android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="20"
        android:paddingBottom="10dp"

        >

     <View
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
</View>

     </LinearLayout>


            </LinearLayout>

         </LinearLayout>





<!-- image2 -->
     <LinearLayout   
         android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="10"
        android:paddingBottom="10dp"

        >


         <ImageView
            android:id="@+id/img1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="fitXY"
    android:adjustViewBounds="true"
     android:layout_gravity="center"
     android:src="@drawable/plushourslight"
        />

        </LinearLayout>

   <!-- space -->
     <LinearLayout   
         android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="5"
        android:paddingBottom="10dp"

        >

     <View
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
</View>

     </LinearLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

创建CustomAdapter而不是使用SimpleAdapter,在getview()方法中,您可以单独设置clicklisteners。