2015-04-28 61 views
-2

這是該列表視圖。它只顯示了3個類型的信息(即就業崗位名稱,位置,薪水)中的每個項目:ListView項目onClick無法顯示想要的結果

招聘崗位名稱1

位置1

工資1

例如,上述3信息是列表視圖中的項目1。如果我點擊第1項,它應該表現出上述3種信息加3的詳細信息,即工作職責,公司,聯繫人:

招聘崗位名稱1

位置1

工資1

崗位職責1

公司1

聯繫1

但是,它失敗了。它只顯示前3個信息,沒有工作職責,公司和聯繫方式。誰能幫忙?謝謝

MainActivity.java

public class MainActivity extends ListActivity { 

private ProgressDialog pDialog; 

// URL to get contacts JSON 
private static String url = "http://192.168.0.102/get_json_select_all.php"; 

// JSON Node names 
private static final String TAG_INFO = "info"; 
private static final String TAG_POSTNAME = "PostName"; 
private static final String TAG_LOCATION = "Location"; 
private static final String TAG_SALARY = "Salary"; 
private static final String TAG_RESPONSIBILITY = "Responsibility"; 
private static final String TAG_COMPANY = "Company"; 
private static final String TAG_CONTACT = "Contact"; 


// contacts JSONArray 
JSONArray infos = null; 

// Hashmap for ListView 
ArrayList<HashMap<String, String>> infoList; 

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

infoList = new ArrayList<HashMap<String, String>>(); 

ListView lv = getListView(); 

// Listview on item click listener 
lv.setOnItemClickListener(new OnItemClickListener() { 

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


     // Starting single contact activity 
     Intent in = new Intent(getApplicationContext(), 
       SingleContactActivity.class); 

     in.putExtra(TAG_POSTNAME, name); 
     in.putExtra(TAG_LOCATION, cost); 
     in.putExtra(TAG_SALARY, description); 


     startActivity(in); 

    } 
}); 

// Calling async task to get json 
new GetContacts().execute(); 
} 

/** 
* Async task class to get json by making HTTP call 
* */ 
private class GetContacts extends AsyncTask<Void, Void, Void> { 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    // Showing progress dialog 
    pDialog = new ProgressDialog(MainActivity.this); 
    pDialog.setMessage("Please wait..."); 
    pDialog.setCancelable(false); 
    pDialog.show(); 

} 

@Override 
protected Void doInBackground(Void... arg0) { 
    // Creating service handler class instance 
    ServiceHandler sh = new ServiceHandler(); 

    // Making a request to url and getting response 
    String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET); 

    Log.d("Response: ", "> " + jsonStr); 

    if (jsonStr != null) { 
     try { 
      JSONObject jsonObj = new JSONObject(jsonStr); 

      // Getting JSON Array node 
      infos = jsonObj.getJSONArray(TAG_INFO); 

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

       String id = c.getString(TAG_POSTNAME); 
       String name = c.getString(TAG_LOCATION); 
       String email = c.getString(TAG_SALARY); 
       String address = c.getString(TAG_RESPONSIBILITY); 
       String gender = c.getString(TAG_COMPANY); 
       String mobile = c.getString(TAG_CONTACT); 


       // tmp hashmap for single contact 
       HashMap<String, String> info = new HashMap<String, String>(); 

       // adding each child node to HashMap key => value 
       info.put(TAG_POSTNAME, id); 
       info.put(TAG_LOCATION, name); 
       info.put(TAG_SALARY, email); 
       info.put(TAG_RESPONSIBILITY, address); 
       info.put(TAG_COMPANY, gender); 
       info.put(TAG_CONTACT, mobile); 
       // adding contact to contact list 
       infoList.add(info); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } else { 
     Log.e("ServiceHandler", "Couldn't get any data from the url"); 
    } 

    return null; 
} 

@Override 
protected void onPostExecute(Void result) { 
    super.onPostExecute(result); 
    // Dismiss the progress dialog 
    if (pDialog.isShowing()) 
     pDialog.dismiss(); 
    /** 
    * Updating parsed JSON data into ListView 
    * */ 
    ListAdapter adapter = new SimpleAdapter(
      MainActivity.this, infoList, 

      R.layout.list_item, new String[] { TAG_POSTNAME, TAG_LOCATION, 
      TAG_SALARY }, new int[] { R.id.PostName, 
      R.id.Location, R.id.Salary }); 

    setListAdapter(adapter); 
} 

} 

}   

SingleContactActivity.java

public class SingleContactActivity extends Activity { 

// JSON node keys 
private static final String TAG_POSTNAME = "PostName"; 
private static final String TAG_LOCATION = "Location"; 
private static final String TAG_SALARY = "Salary"; 

private static final String TAG_RESPONSIBILITY = "Responsibility"; 
private static final String TAG_COMPANY = "Company"; 
private static final String TAG_CONTACT = "Contact"; 

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

// getting intent data 
Intent in = getIntent(); 

// Get JSON values from previous intent 
String PostName = in.getStringExtra(TAG_POSTNAME); 
String Location = in.getStringExtra(TAG_LOCATION); 
String Salary = in.getStringExtra(TAG_SALARY); 

String Responsibility = in.getStringExtra(TAG_RESPONSIBILITY); 
String Company = in.getStringExtra(TAG_COMPANY); 
String Contact = in.getStringExtra(TAG_CONTACT); 

// Displaying all values on the screen 
TextView lblPostName = (TextView) findViewById(R.id.PostName_label); 
TextView lblLocation = (TextView) findViewById(R.id.Location_label); 
TextView lblSalary = (TextView) findViewById(R.id.Salary_label); 

TextView lblResponsibility = (TextView) findViewById(R.id.Responsibility_label); 
TextView lblCompany = (TextView) findViewById(R.id.Company_label); 
TextView lblContact = (TextView) findViewById(R.id.Contact_label); 

lblPostName.setText(PostName); 
lblLocation.setText(Location); 
lblSalary.setText(Salary); 

lblResponsibility.setText(Responsibility); 
lblCompany.setText(Company); 
lblContact.setText(Contact); 
} 
} 

ServiceHandler.java

public class ServiceHandler { 

static String response = null; 
public final static int GET = 1; 
public final static int POST = 2; 

public ServiceHandler() { 

} 


public String makeServiceCall(String url, int method) { 
return this.makeServiceCall(url, method, null); 
} 

public String makeServiceCall(String url, int method, 
          List<NameValuePair> params) { 
try { 
    // http client 
    DefaultHttpClient httpClient = new DefaultHttpClient(); 
    HttpEntity httpEntity = null; 
    HttpResponse httpResponse = null; 

    // Checking http request method type 
    if (method == POST) { 
     HttpPost httpPost = new HttpPost(url); 
     // adding post params 
     if (params != null) { 
      httpPost.setEntity(new UrlEncodedFormEntity(params)); 
     } 

     httpResponse = httpClient.execute(httpPost); 

    } else if (method == GET) { 
     // appending params to url 
     if (params != null) { 
      String paramString = URLEncodedUtils 
        .format(params, "utf-8"); 
      url += "?" + paramString; 
     } 
     HttpGet httpGet = new HttpGet(url); 

     httpResponse = httpClient.execute(httpGet); 

    } 
    httpEntity = httpResponse.getEntity(); 
    response = EntityUtils.toString(httpEntity); 

} catch (UnsupportedEncodingException e) { 
    e.printStackTrace(); 
} catch (ClientProtocolException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

return response; 

} 
} 

acticity_main.xml

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

list_item.xml

<TextView 
android:id="@+id/PostName" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:paddingBottom="2dip" 
android:paddingTop="6dip" 
android:textColor="#43bd00" 
android:textSize="16sp" 
android:textStyle="bold" /> 


<TextView 
android:id="@+id/Location" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:paddingBottom="2dip" 
android:textColor="#acacac" /> 


<TextView 
android:id="@+id/Salary" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:gravity="left" 
android:text="Salary: " 
android:textColor="#5d5d5d" 
android:textStyle="bold" /> 

activity_single_contact.xml

<TextView android:id="@+id/PostName_label" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:textSize="25dip" 
android:textStyle="bold" 
android:paddingTop="10dip" 
android:paddingBottom="10dip" 
android:textColor="#43bd00"/> 

<TextView android:id="@+id/Location_label" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:textColor="#acacac"/> 

<TextView android:id="@+id/Salary_label" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:textStyle="bold"/> 

<TextView android:id="@+id/Responsibility_label" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:textColor="#ff1e76ac"/> 

<TextView android:id="@+id/Company_label" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:textColor="#ff1e76ac"/> 

<TextView android:id="@+id/Contact_label" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:textColor="#ff1e76ac"/> 

回答

0

編輯的代碼

final listView lv=getListView(); 

lv.setOnItemClickListener(new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, 
          int position, long id) { 
     // getting values from selected ListItem 
     String name = ((TextView) view.findViewById(R.id.PostName)) 
       .getText().toString(); 
     String cost = ((TextView) view.findViewById(R.id.Location)) 
       .getText().toString(); 
     String description = ((TextView) view.findViewById(R.id.Salary)) 
       .getText().toString(); 
HashMap<String, String> info = new HashMap<String, String>(); 
     info=(HashMap<String, String>)lv.getAdapter().getItem(position); 

     // Starting single contact activity 
     Intent in = new Intent(getApplicationContext(), 
       SingleContactActivity.class); 

     in.putExtra(TAG_POSTNAME, name); 
     in.putExtra(TAG_LOCATION, cost); 
     in.putExtra(TAG_SALARY, description);  
     in.putExtra(RESPONSIBILITY, info.get(TAG_RESPONSIBILITY)); 
     in.putExtra(TAG_COMPANY, info.get(TAG_COMPANY)); 
     in.putExtra(TAG_CONTACT, info.get(TAG_CONTACT)); 
    startActivity(in); 
+0

這部分謝謝您的幫助!但是「getAdapter()。getItem(i);」有問題:1。無法解析方法'getAdapter()'。 2.無法解析符號'我' –

+0

對不起,不是「我」,它是「位置」 –

+0

看到更新的答案 –