简体   繁体   中英

OnItemClickListener not working with Custom ListView adapter

I have two activities. One displays all the notices in a listview and it displays links to pdf files. Second activity runs when an item is selected. Basically second activity should fire up on item click and it should open the pdf file.

Well the thing is that it does not fire up 2nd activity on item select. Posting the code for your reference.

CODE:

AllEventsActivity.java

     public class AllEventsActivity extends ListActivity {


private ProgressDialog pDialog;

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

ArrayList<HashMap<String, String>> productsList;


private static String url_all_products = "http://myurl/fetchnotice.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "notices";
private static final String TAG_PID = "nid";
private static final String TAG_NAME = "notice";
private static final String TAG_INFO = "noticeinfo";
private static final String TAG_DATE = "date";

// products JSONArray
JSONArray products = null;

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

    // Hashmap for ListView
    productsList = new ArrayList<HashMap<String, String>>();
    Button go_btn = (Button) findViewById(R.id.go_btn);
    // Loading products in Background Thread
    new LoadAllProducts().execute();

    // Get listview
    ListView lv = getListView();


    // on seleting single product
    // launching pdfviewer activity
    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.einfo)).getText()
                    .toString();

            // Starting new intent
            Intent in = new Intent(getApplicationContext(),
                    PdfViewActivity.class);
            // sending pid to next activity
            in.putExtra("pathid", pid);

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

}

// Response from Edit Product 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 product
        // reload this screen again
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }

}

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

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(AllEventsActivity.this);
        pDialog.setMessage("Loading results. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

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

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

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

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

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

                    // Storing each json item in variable
                    String id = c.getString(TAG_PID);
                    String name = c.getString(TAG_NAME);
                    String info = c.getString(TAG_INFO);
                    String date = c.getString(TAG_DATE);
                    // 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_INFO, info);
                    map.put(TAG_DATE, date);
                    // adding HashList to ArrayList
                    productsList.add(map);
                }
            } else {
                // nothing found
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        AllEventsActivity.this, productsList,
                        R.layout.list_item_eve, new String[] { TAG_PID,
                                TAG_NAME, TAG_INFO, TAG_DATE },
                        new int[] { R.id.eid, R.id.ename, R.id.einfo, R.id.date});
                // updating listview
                setListAdapter(adapter);
            }
        });

    }
}
}

2nd Activity:

       public class PdfViewActivity extends Activity { 


   private WebView webview;

    //private ProgressDialog progressDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photoview);

        webview = (WebView) findViewById(R.id.my_img);
        webview.getSettings().setBuiltInZoomControls(true);
        webview.getSettings().setUseWideViewPort(true);
        webview.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);

        String id = getIntent().getExtras().getString("pathid");
        webview.loadUrl("http://docs.google.com/viewer?url="+id);
    }
  }

XML code: list_item_eve.xml

   <?xml version="1.0" encoding="utf-8"?>

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" 
android:background="@color/itm_bg"
android:textColor="@drawable/text_color">

<TextView
    android:id="@+id/eid"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="gone" 
    android:textColor="@drawable/text_color"/>

<!-- Name Label -->
<TextView
    android:id="@+id/ename"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="6dip"
    android:paddingLeft="6dip"
    android:textSize="12dip"
    android:textStyle="italic" 
    android:textColor="@drawable/text_color"/>


<TextView
    android:id="@+id/einfo"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="12dip"
    android:autoLink="web"
    android:textColorLink="@color/link"
    android:paddingTop="6dip"
    android:textSize="14dip"
    android:textStyle="bold" 
    android:textColor="@drawable/text_color"/>

<TextView
    android:id="@+id/date"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:autoLink="web"
    android:textColorLink="@color/link"
    android:paddingLeft="15dip"
    android:paddingTop="6dip"
    android:textColor="#ffffff"
    android:textSize="9dip"
    android:textStyle="italic" />

<View android:id="@+id/sepbottom" 
      android:background="#000000" 
      android:layout_width = "fill_parent"
      android:layout_height="2dip"
      android:layout_marginTop="10dip"
      />

</LinearLayout>

all_events.xml

       <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/itm_bg"
android:textColor="@drawable/text_color">

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

</LinearLayout>

The easiest and most appropriate approach would be to set all child views inside ListView item to not focusable/not clickable, like this:

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="vertical" 
   android:background="@color/itm_bg"
   android:textColor="@drawable/text_color">

<TextView
android:id="@+id/eid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" 
android:focusable="false"
android:clickable="false"
 android:textColor="@drawable/text_color"/>

   <!-- Name Label -->
  <TextView
android:id="@+id/ename"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:textSize="12dip"
android:textStyle="italic" 
android:focusable="false"
android:clickable="false"
android:textColor="@drawable/text_color"/>


<TextView
android:id="@+id/einfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="12dip"
android:autoLink="web"
android:textColorLink="@color/link"
android:paddingTop="6dip"
android:textSize="14dip"
android:textStyle="bold" 
android:focusable="false"
android:clickable="false"
android:textColor="@drawable/text_color"/>

<TextView
android:id="@+id/date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="web"
android:textColorLink="@color/link"
android:paddingLeft="15dip"
android:paddingTop="6dip"
android:textColor="#ffffff"
 android:textSize="9dip"
android:focusable="false"
 android:clickable="false"
 android:textStyle="italic" />

<View android:id="@+id/sepbottom" 
  android:background="#000000" 
  android:layout_width = "fill_parent"
  android:layout_height="2dip"
  android:focusable="false"
  android:clickable="false"
  android:layout_marginTop="10dip"
  />

   </LinearLayout>

检查您是否在AndroidManifest文件中添加了第二个活动。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM