-1

我現在試圖通過一個意圖顯示圖像和名稱的新活動,但我不知道如何將偵聽器添加到代碼,有人可以幫助我如何將代碼放入監聽器?我想單擊到detailsActivity視圖,我在下面的detailsActivity.xml中。想要創建一個新的活動,通過意圖顯示圖像和名稱

MainActivity.java

package net.simplifiedcoding.androidcustomgridview; 

import android.app.ProgressDialog; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.GridView; 

import com.android.volley.RequestQueue; 
import com.android.volley.Response; 
import com.android.volley.VolleyError; 
import com.android.volley.toolbox.JsonArrayRequest; 
import com.android.volley.toolbox.Volley; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.util.ArrayList; 

public class MainActivity extends AppCompatActivity implements  AdapterView.OnItemClickListener{ 

    //Web api url 

    public static final String DATA_URL =  "http://www.simplifiedcodingreaders.16mb.com/superheroes.php"; 

    //Tag values to read from json 
    public static final String TAG_IMAGE_URL = "image"; 
    public static final String TAG_NAME = "name"; 


    //GridView Object 
    private GridView gridView; 

    //ArrayList for Storing image urls and titles 
    private ArrayList<String> images; 
    private ArrayList<String> names; 


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

     images = new ArrayList<>(); 
     names = new ArrayList<>(); 
     gridView = (GridView) findViewById(R.id.gridView); 
     gridView.setOnItemClickListener(this); 
     //Calling the getData method 
     getData(); 
    } 

    private void getData(){ 
     //Showing a progress dialog while our app fetches the data from url 
     final ProgressDialog loading = ProgressDialog.show(this, "Please  wait...","Fetching data...",false,false); 

     //Creating a json array request to get the json from our api 
     JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(DATA_URL, 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 
         //Dismissing the progressdialog on response 
         loading.dismiss(); 

         //Displaying our grid 
         showGrid(response); 
        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 

        } 
       } 
     ); 

     //Creating a request queue 
     RequestQueue requestQueue = Volley.newRequestQueue(this); 
     //Adding our request to the queue 
     requestQueue.add(jsonArrayRequest); 
    } 


    private void showGrid(JSONArray jsonArray){ 
     //Looping through all the elements of json array 
     for(int i = 0; i<jsonArray.length(); i++){ 
      //Creating a json object of the current index 
      JSONObject obj = null; 
      try { 
       //getting json object from current index 
       obj = jsonArray.getJSONObject(i); 

       //getting image url and title from json object 
       images.add(obj.getString(TAG_IMAGE_URL)); 
       names.add(obj.getString(TAG_NAME)); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
     //Creating GridViewAdapter Object 
     GridViewAdapter gridViewAdapter = new GridViewAdapter(this,images,names); 

     //Adding adapter to gridview 
     gridView.setAdapter(gridViewAdapter); 

    } 
} 

GridViewApapter.java

package net.simplifiedcoding.androidcustomgridview; 

import android.content.Context; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.GridView; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

import com.android.volley.toolbox.ImageLoader; 
import com.android.volley.toolbox.NetworkImageView; 

import java.util.ArrayList; 

public class GridViewAdapter extends BaseAdapter { 

    //Imageloader to load images 
    private ImageLoader imageLoader; 

    //Context 
    private Context context; 

    //Array List that would contain the urls and the titles for the images 
    private ArrayList<String> images; 
    private ArrayList<String> names; 

    public GridViewAdapter (Context context, ArrayList<String> images, ArrayList<String> names){ 
     //Getting all the values 
     this.context = context; 
     this.images = images; 
     this.names = names; 
    } 

    @Override 
    public int getCount() { 
     return images.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return images.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     //Creating a linear layout 
     LinearLayout linearLayout = new LinearLayout(context); 
     linearLayout.setOrientation(LinearLayout.VERTICAL); 

     //NetworkImageView 
     NetworkImageView networkImageView = new NetworkImageView(context); 

     //Initializing ImageLoader 
     imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader(); 
     imageLoader.get(images.get(position), ImageLoader.getImageListener(networkImageView, R.mipmap.ic_launcher,  android.R.drawable.ic_dialog_alert)); 

     //Setting the image url to load 
     networkImageView.setImageUrl(images.get(position),imageLoader); 

     //Creating a textview to show the title 
     TextView textView = new TextView(context); 
     textView.setText(names.get(position)); 

     //Scaling the imageview 
     networkImageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     networkImageView.setLayoutParams(new  GridView.LayoutParams(200,200)); 

     //Adding views to the layout 
     linearLayout.addView(textView); 
     linearLayout.addView(networkImageView); 

     //Returnint the layout 
     return linearLayout; 
    } 
} 

我detailsActivity 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="wrap_parent"  android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context="net.simplifiedcoding.imageuploadsample.detailsActivity"> 


    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/imageViewFull" 
     android:layout_alignParentTop="true" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/TextView" 
      android:text="text" 

     /> 


</RelativeLayout> 
+0

點擊Listener到哪個視圖? – Raghunandan

+0

我想點擊detailsActivity視圖,然後我創建detailsActivity xml, 如何點擊視圖? – bbbb

回答

0

您需要使用的Intents的幫助下通過圖像的URL和圖像名稱您詳細活動上的網格視圖像這樣單擊事件: -

gridView.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 

      Intent intent = new Intent(getBaseContext(), 
        DetailActivity.class); 
      intent.putExtra("ImageName", names.get(arg2)); 
      intent.putExtra("ImageURL", images.get(arg2)); 
      startActivity(intent); 

     } 
    }); 

和您的詳細活動應該像這一點: -

public class DetailActivity extends Activity { 

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

     Bundle extras = getIntent().getExtras(); 
     if (extras != null) { 

      ((TextView) findViewById(R.id.TextView)).setText(extras 
        .getString("ImageName")); 

      ImageLoader imageLoader = CustomVolleyRequestQueue 
        .getInstance(this).getImageLoader(); 

      // If you are using normal ImageView 
      imageLoader.get(extras.getString("ImageURL"), new ImageListener() { 

       @Override 
       public void onErrorResponse(VolleyError error) { 
        // Log.e(TAG, "Image Load Error: " + error.getMessage()); 
       } 

       @Override 
       public void onResponse(ImageContainer response, boolean arg1) { 
        if (response.getBitmap() != null) { 
         // load image into imageview 
         ((ImageView) findViewById(R.id.imageViewFull)) 
           .setImageBitmap(response.getBitmap()); 
        } 
       } 
      }); 
     } 

    } 

} 

我已經測試你的代碼顯然存在一些問題,基本URL使用的是我已經增強,並更新其在GitHub上你ç一個在這裏找到它

https://github.com/hiteshsahu/Volley-Image-Loading-Sample 

由於您的網址是不正確的,我使用的樣品URL像這樣

enter image description here

和詳細的活動顯示圖像看起來像這樣

enter image description here

我強烈建議你使用fragment代替的活動。希望它能幫到你

+0

非常感謝,它幫助我解決這個問題=] – bbbb

+0

我很高興它幫助請接受答案並關閉線程。 –

0

所有你需要添加ItemClickListener網格視圖由你將能得到網格視圖中點擊項目的位置。在裏面你可以打開你的Details Activity並使用intent傳遞你的數據。

gridview.setOnItemClickListener(new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
     Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show(); 
    } 
});} 

你會找到一個很好的例子here

相關問題