2015-01-21 30 views
0

我爲我的Android應用程序創建了一個ListView,並且爲項目的單獨部分創建單獨的onClickListeners時出現問題。我的項目有一個image1,image2和一個文本。我想要的是根據哪些被點擊而開始不同的活動。以下是我的代碼。如何在Android中爲列表視圖項目的單獨組件(部件)啓動一項新活動

**MainActivity.java** 
public class MainActivity extends Activity { 
    ListView mListView; 
    ImageView rightarrow; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     // URL to the JSON data   
     String strUrl = "url of php file"; 

     // Creating a new non-ui thread task to download json data 
     DownloadTask downloadTask = new DownloadTask(); 

     // Starting the download process 
     downloadTask.execute(strUrl); 
    } 


    /** A method to download json data from url */ 
    private String downloadUrl(String strUrl) throws IOException{ 
     String data = ""; 
     InputStream iStream = null; 
     try{ 
       URL url = new URL(strUrl); 

       // Creating an http connection to communicate with url 
       HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 

       // Connecting to url 
       urlConnection.connect(); 

       // Reading data from url 
       iStream = urlConnection.getInputStream(); 

       BufferedReader br = new BufferedReader(new InputStreamReader(iStream)); 

       StringBuffer sb = new StringBuffer(); 

       String line = ""; 
       while((line = br.readLine()) != null){ 
        sb.append(line); 
       } 

       data = sb.toString(); 

       br.close(); 

     }catch(Exception e){ 
       Log.d("Exception while downloading url", e.toString()); 
     }finally{ 
       iStream.close(); 
     } 

     return data; 
    } 





/** AsyncTask to download json data */ 
     private class DownloadTask extends AsyncTask<String, Integer, String>{ 
      String data = null; 
        @Override 
        protected String doInBackground(String... url) { 
          try{ 
           data = downloadUrl(url[0]); 

          }catch(Exception e){ 
           Log.d("Background Task",e.toString()); 
          } 
          return data; 
        } 

        @Override 
        protected void onPostExecute(String result) { 

          // The parsing of the xml data is done in a non-ui thread 
          ListViewLoaderTask listViewLoaderTask = new ListViewLoaderTask(); 

          // Start parsing xml data 
          listViewLoaderTask.execute(result);       

        } 
     } 



/** AsyncTask to parse json data and load ListView */ 
     private class ListViewLoaderTask extends AsyncTask<String, Void, SimpleAdapter>{ 

      JSONObject jObject; 
      // Doing the parsing of xml data in a non-ui thread 
      @Override 
      protected SimpleAdapter doInBackground(String... strJson) { 
       try{ 
        jObject = new JSONObject(strJson[0]); 
        CountryJSONParser countryJsonParser = new CountryJSONParser(); 
        countryJsonParser.parse(jObject); 
       }catch(Exception e){ 
        Log.d("JSON Exception1",e.toString()); 
       } 

       // Instantiating json parser class 
       CountryJSONParser countryJsonParser = new CountryJSONParser(); 

       // A list object to store the parsed countries list 
       List<HashMap<String, Object>> countries = null; 

       try{ 
        // Getting the parsed data as a List construct 
        countries = countryJsonParser.parse(jObject); 
       }catch(Exception e){ 
        Log.d("Exception",e.toString()); 
       }   

       // Keys used in Hashmap 
       // String[] from = { "country","flag","details"}; 
       String[] from = {"flag"}; 
       // Ids of views in listview_layout 
       // int[] to = { R.id.tv_country,R.id.iv_flag,R.id.tv_country_details}; 
       int[] to = { R.id.iv_flag}; 

       // Instantiating an adapter to store each items 
       // R.layout.listview_layout defines the layout of each item   
       SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), countries, R.layout.lv_layout, from, to); 

       return adapter; 
      } 

      /** Invoked by the Android on "doInBackground" is executed */ 
      @Override 
      protected void onPostExecute(SimpleAdapter adapter) { 

       // Setting adapter for the listview 
       mListView.setAdapter(adapter); 

       for(int i=0;i<adapter.getCount();i++){ 
        HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(i); 
        String imgUrl = (String) hm.get("flag_path"); 
        ImageLoaderTask imageLoaderTask = new ImageLoaderTask(); 

        HashMap<String, Object> hmDownload = new HashMap<String, Object>(); 
        hm.put("flag_path",imgUrl); 
        hm.put("position", i); 

        // Starting ImageLoaderTask to download and populate image in the listview 
        imageLoaderTask.execute(hm); 

        } 
      } 

     } 



/** AsyncTask to download and load an image in ListView */ 
     private class ImageLoaderTask extends AsyncTask<HashMap<String, Object>, Void, HashMap<String, Object>>{ 

      @Override 
      protected HashMap<String, Object> doInBackground(HashMap<String, Object>... hm) { 

       InputStream iStream=null; 
       String imgUrl = (String) hm[0].get("flag_path"); 
       int position = (Integer) hm[0].get("position"); 

       URL url; 
       try { 
        url = new URL(imgUrl); 

        // Creating an http connection to communicate with url 
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 

        // Connecting to url     
        urlConnection.connect(); 

        // Reading data from url 
        iStream = urlConnection.getInputStream(); 

        // Getting Caching directory 
        File cacheDirectory = getBaseContext().getCacheDir(); 

        // Temporary file to store the downloaded image 
        File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"+position+".png"); 



        // tmpFile.getParentFile().mkdirs(); 
        // tmpFile.createNewFile(); 
        // The FileOutputStream to the temporary file 
        FileOutputStream fOutStream = new FileOutputStream(tmpFile); 

        // Creating a bitmap from the downloaded inputstream 
        Bitmap b = BitmapFactory.decodeStream(iStream);    

        // Writing the bitmap to the temporary file as png file 
        b.compress(Bitmap.CompressFormat.PNG,100, fOutStream);    

        // Flush the FileOutputStream 
        fOutStream.flush(); 

        //Close the FileOutputStream 
        fOutStream.close();    

        // Create a hashmap object to store image path and its position in the listview 
        HashMap<String, Object> hmBitmap = new HashMap<String, Object>(); 

        // Storing the path to the temporary image file 
        hmBitmap.put("flag",tmpFile.getPath()); 

        // Storing the position of the image in the listview 
        hmBitmap.put("position",position);    

        // Returning the HashMap object containing the image path and position 
        return hmBitmap;     


       }catch (Exception e) {    
        e.printStackTrace(); 
       } 
       return null; 
      } 

      @Override 
      protected void onPostExecute(HashMap<String, Object> result) { 
       // Getting the path to the downloaded image 
       String path = (String) result.get("flag");   

       // Getting the position of the downloaded image 
       int position = (Integer) result.get("position"); 

       // Getting adapter of the listview 
       SimpleAdapter adapter = (SimpleAdapter) mListView.getAdapter(); 

       // Getting the hashmap object at the specified position of the listview 
       HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(position); 

       // Overwriting the existing path in the adapter 
       hm.put("flag",path); 

       // Noticing listview about the dataset changes 
       adapter.notifyDataSetChanged(); 
      } 
     } 

     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      getMenuInflater().inflate(R.menu.activity_main, menu); 

      return true; 
     } 
    } 

我得到我從database.because ListViewListView內容是父視圖,以便當我申請名單上觀看clicklistener它得到它的每個項目的組件應用。所以我想ListView行及其組件開始不同的活動。 這裏是我的適配器類:

public class CountryJSONParser { 

// Receives a JSONObject and returns a list 
public List<HashMap<String,Object>> parse(JSONObject jObject){  

    JSONArray jCountries = null; 
    try {  
     // Retrieves all the elements in the 'countries' array 
     jCountries = jObject.getJSONArray("countries"); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    // Invoking getCountries with the array of json object 
    // where each json object represent a country 
    return getCountries(jCountries); 
} 


private List<HashMap<String, Object>> getCountries(JSONArray jCountries){ 
    int countryCount = jCountries.length(); 
    List<HashMap<String, Object>> countryList = new ArrayList<HashMap<String,Object>>(); 
    HashMap<String, Object> country = null; 

    // Taking each country, parses and adds to list object 
    for(int i=0; i<countryCount;i++){ 
     try { 
      // Call getCountry with country JSON object to parse the country 
      country = getCountry((JSONObject)jCountries.get(i)); 
      countryList.add(country); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 

    return countryList; 
} 

// Parsing the Country JSON object 
private HashMap<String, Object> getCountry(JSONObject jCountry){ 

    HashMap<String, Object> country = new HashMap<String, Object>(); 

    String flag=""; 


    } catch (JSONException e) {   
     e.printStackTrace(); 
    }  
    return country; 
    } 
    } 

這裏是我的XML文件:一個用於ListView等是ListView項目:

**activity_main.xml** 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" >  


    <ListView 
     android:id="@+id/lv_countries" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     tools:context=".MainActivity" 
    android:background="@drawable/gradient_drawable" 
     android:dividerHeight="1dp" 
     android:divider="#b5b5b5" /> 


</RelativeLayout> 

**lv_layout.xml** 
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
     android:id="@+id/tv_country" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 

     android:textSize="20dp" 
     android:textStyle="bold" /> 

    <ImageView 
     android:id="@+id/iv_flag" 
     android:layout_width="80dp" 
     android:layout_height="80dp" 

     android:layout_centerVertical="true" 
     android:padding="5dp" 
     android:contentDescription="@string/str_iv_flag" /> 


     <ImageView 
     android:id="@+id/right_arrow" 
     android:layout_width="20dp" 
     android:layout_height="20dp" 
     android:src="@drawable/arrow2" 

     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true"/> 

    <TextView 
     android:id="@+id/tv_country_details" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/iv_flag" 

     android:layout_below="@id/tv_country" /> 

    </RelativeLayout> 

我已經嘗試了How to set onClickListener for separate parts of custom listView item? [Android]後 但沒有解決我的問題,因爲它確實帶我一個新的活動意味着什麼都沒有發生,也沒有得到任何錯誤。所以請幫我解決這個問題。

+0

發佈適配器類 – 2015-01-21 06:54:47

+0

您可以簡單地爲'ListView'調用'OnItemClickListener'方法。 – Piyush 2015-01-21 06:55:35

+0

你使用自定義的listview ...如果是的話請在這裏分享適配器,如果不使用自定義listview與適配器和處理點擊適配器.. – 2015-01-21 06:59:19

回答

0

從獲取點擊的項目的文本listviewin聽衆,然後採取哪些活動應該在點擊開始決定,從here

lv.setOnItemClickListener(new OnItemClickListener() 
{ 
    @Override 
    public void onItemClick(AdapterView<?> a, View v, int position, long id) 
    { 
     // In the following line "v" refers to the View returned by the `getView()` method; meaning the clicked View. 
     TextView txtName = (TextView)v.findViewById(R.id.yourTextViewID); 
     String name = txtName.getText().toString(); 
     switch(name) 
     { 
       case "nameOne": 
        Intent intent = new Intent(YourCurrentActivity.this, NameOneActivity.class); 
        startActivity(intent); 
        break; 

       case "nameTwo": 
        Intent intent = new Intent(YourCurrentActivity.this, NameTwoActivity.class); 
        startActivity(intent); 
        break; 

       //And so on and so forth.... 
     } 

    } 
}); 
+0

謝謝你的幫助。但上面的代碼與custombaseadapter一起工作,但在我的代碼中,我沒有使用這個類。所以請看我的代碼,並告訴在這種情況下要做什麼,如果它需要使用自定義列表適配器,並且如果是,請告訴它在哪裏使用它以及它的整個代碼。並很抱歉打擾你。 – 2015-01-21 07:43:59

0

採取內,您的BaseAdpter類設置onClickListener在每個項目上和在隨後的onclick啓動你想要這樣的活動:

image1.setOnClickListener(this); 
image2.setOnClickListener(this); 
textview.ssetOnClickListener(this); 

然後

@Override 
public void onClick(View view) { 
switch(view.getId()){ 
case image1.getId(): 
mActivity.startAcivity(new Intent(mActivity,ActivityToStart.class)); 
break; 
case image2.getId(): 
mActivity.startAcivity(new Intent(mActivity,ActivityToStart.class)); 
break; 
.....and so on 
} 
} 
+0

對不起,但它沒有幫助我 – 2015-01-21 07:56:56

+0

您是否將此添加到視圖所屬類或內部的BaseAdapter的getView? – 2015-01-21 08:22:50

+0

抱歉,但在我上面貼的代碼中,我沒有適配器類,所以請建議適配器類或類型(基本或自定義)的代碼,這是相關的我的發佈的code.I只是嘗試了一個鏈接中給出的代碼沒有實際實現因爲它在我的項目中給了我太多的錯誤。 – 2015-01-21 08:58:50

相關問題