简体   繁体   English

OnItemClickListener不使用自定义ListView适配器

[英]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. 一个在列表视图中显示所有通知,并显示指向pdf文件的链接。 Second activity runs when an item is selected. 选择项目时会运行第二个活动。 Basically second activity should fire up on item click and it should open the pdf file. 基本上第二个活动应该点击项目点击,它应该打开PDF文件。

Well the thing is that it does not fire up 2nd activity on item select. 好吧,事情是它没有启动项目选择的第二个活动。 Posting the code for your reference. 发布代码供您参考。

CODE: 码:

AllEventsActivity.java 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代码: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 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: 最简单和最合适的方法是将ListView项中的所有子视图设置为不可聚焦/不可点击,如下所示:

  <?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文件中添加了第二个活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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