1
我有一個鏈接在一起的3個不同部分的列表視圖,圖像,文本框和另一個圖像。現在,當我點擊列表視圖時,它會轉到另一個頁面。如果用戶點擊圖片,我希望它將其引導到另一個頁面。請幫忙!!以下是課程代碼。如何將listview中的項目分開以指向不同的頁面?
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>
您可以將onclicklistener設置爲單獨的視圖 –
但是所有都在列表視圖中。我設置了一個listview onclick監聽器。我如何設置onclicklistener分開? – user3197009
爲你的列表視圖創建自定義適配器,你可以將操作添加到單獨的視圖。 – keshav