片段中的自定义ListView不可单击

时间:2015-05-18 08:56:53

标签: java android listview android-fragments fragment

我在viewpager中有一个片段,片段有一个listview,它应该从mysql表中提取数据并将它们插入到listview中,问题是列表中的行不可点击,就好像它是行的图像。

        package test.example.com.test;

        import android.app.ProgressDialog;
        import android.os.AsyncTask;
        import android.os.Bundle;
        import android.support.v4.app.Fragment;
        import android.util.Log;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.AdapterView;
        import android.widget.ImageButton;
        import android.widget.ListAdapter;
        import android.widget.ListView;
        import android.widget.SimpleAdapter;
        import android.widget.TextView;
        import android.widget.Toast;

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

        import java.util.ArrayList;
        import java.util.HashMap;
        import java.util.List;

    public class FragmentCtab3 extends Fragment {

//SimpleAdapter sAdapter;
ListView lvDemand;
String Email,CompId,UserName;
// Progress Dialog
private ProgressDialog pDialog;

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

ArrayList<HashMap<String, String>> demandsList;

// url to get all products list
private static String LIST_COMP_URL = "http://eshteghel.southlebanon.net/lvCompany.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_DEMANDS = "demands";
private static final String TAG_NAME= "Name";
private static final String TAG_EMAIL = "Email";
private static final String TAG_DESCRIPTION = "Description";
private static final String TAG_PHONE = "Phone";
private static final String TAG_GENDER = "Gender";
private static final String TAG_MAJOR= "Major";
private static final String TAG_EXPERIENCE = "Experience";
private static final String TAG_SALARY = "Salary";
private static final String TAG_REGION = "Region";
// products JSONArray
JSONArray demands = null;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_company_tab3, container, false);
    demandsList = new ArrayList<HashMap<String, String>>();

    new LoadAllDemanded().execute();

    //lv = (ListView) getActivity().findViewById(R.id.lvDemand);
    lvDemand = (ListView) rootView.findViewById(R.id.lvDemand);



    lvDemand.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

            Toast.makeText(getActivity(),ColName, Toast.LENGTH_LONG).show();
            Log.d("All Jobs: ","Log");
            b.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(),"TOAST", Toast.LENGTH_LONG).show();
                    Log.d("All Jobs: ", "Log");
                }
            });
        }
    });
    String myValue = container.getTag().toString();
    String credentials[] = myValue.split(" ");
    UserName = credentials[0];
    Email = credentials[1];
    //CompId = credentials[2];

    return rootView;
}

class LoadAllDemanded extends AsyncTask<String, String, String> {

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

    /**
     * getting All products from url
     * */
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(LIST_COMP_URL, "GET", params);

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

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

            if (success == 1) {
                // products found
                // Getting Array of Products
                demands = json.getJSONArray(TAG_DEMANDS);

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

                    // Storing each json item in variable
                    String ColName = c.getString(TAG_NAME);
                   // String Email = c.getString(TAG_EMAIL);
                    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_NAME, ColName);
                    //map.put(TAG_EMAIL, Email);
                    map.put(TAG_DESCRIPTION, Description);

                    // adding HashList to ArrayList
                    demandsList.add(map);
                }
            } else {
                // no products found
                // Launch Add New product Activity
                /*Intent i = new Intent(getApplicationContext(),
                        NewProductActivity.class);
                // Closing all previous activities
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);*/

                Toast.makeText(getActivity(),"No jobs Found", Toast.LENGTH_LONG).show();
            }
        } 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
        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        getActivity(), demandsList,
                        R.layout.custom_layout, new String[]{TAG_NAME,
                        /*TAG_EMAIL,*/ TAG_DESCRIPTION},
                        new int[]{R.id.ColName,R.id.ColDescription});
                // updating listview
                lvDemand.setAdapter(adapter);
            }
        });

    }

}

}

这是布局代码

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="50dp"
>

<TextView
    android:id="@+id/ColName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:focusable="false"
    android:text="Name"/>


<TextView
    android:id="@+id/ColDescription"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:focusable="false"
    android:text="Description"/>


<ImageButton
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/mail2"
    android:focusable="false"
    android:layout_gravity="right"
    android:id="@+id/btlsSendMail"
    />

1 个答案:

答案 0 :(得分:0)

尝试在ListView对象上设置选择模式:

  • none 0不表示选择的正常列表。
  • singleChoice 1该列表最多允许一个选项。
  • multipleChoice 2该列表允许多种选择。
  • multipleChoiceModal 3该列表允许在自定义选择模式中进行多项选择。

Guide