listview项onclick无法显示想要的结果

2nbm6dog  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(351)

这是listview。在每个项目中只显示3种类型的信息(即职位名称、位置、薪资):
职位名称1
位置1
工资1
例如,上述3个信息是listview中的项1。如果我点击第1项,它应该显示以上3个信息加上3个更多的信息,即工作职责,公司,联系人:
职位名称1
位置1
工资1
工作职责1
公司1
联系人1
然而,它失败了。它只显示前3个信息,没有工作职责,公司和联系人。有人能帮忙吗?谢谢您
主活动.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\u main.xml

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

列表项.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" />

活动\u single\u 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"/>
ogq8wdun

ogq8wdun1#

编辑这部分代码

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);

相关问题