网格视图单击侦听器未给出响应

时间:2014-06-07 07:06:21

标签: android gridview

当我点击GridView项目时,一切运行良好没有响应点击 如何设置OnItem Clicklistener

这是我的活动

public class MeetPeople extends Activity {

private ProgressDialog pDialog;
private GridView gridView;
//private GridViewAdapter customGridAdapter;
// URL to get contacts JSON
private static String url = "http://www........com.";

// JSON Node names
private static final String TAG_CONTACTS = "";
private static final String TAG_ID = "User_Id";
private static final String TAG_NAME = "User_Full_Name";
private static final String TAG_IMAGE = "User_Image";


// contacts JSONArray
JSONArray contacts = null;

// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList;
GridViewAdapter customGridAdapter;

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

    gridView = (GridView) findViewById(R.id.gridView);
    contactList = new ArrayList<HashMap<String, String>>();

            new GetContacts().execute();




            gridView=(GridView)findViewById(R.id.gridView); 

                    gridView.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
                    Intent i =new Intent(getApplicationContext(),SingleUserProfile.class);
                    i.putExtra("id", position);
                    startActivity(i);
                        //Toast.makeText(MeetPeople.this, position + "#Selected",Toast.LENGTH_SHORT).show();
                    }

                });

}
/*public void  load(){
    customGridAdapter = new GridViewAdapter(this, R.layout.row_grid, contactList);
    gridView.setAdapter(customGridAdapter);
}*/
/**
 * Async task class to get json by making HTTP call
 * */
private class GetContacts extends AsyncTask<ArrayList<HashMap<String, String>>, Void, ArrayList<HashMap<String, String>>> {
    ArrayList<HashMap<String, String>>  contact_List = new ArrayList<HashMap<String, String>>();
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(MeetPeople.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(ArrayList<HashMap<String, String>>... arg0) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();

         UserModel user = (UserModel) getIntent().getSerializableExtra("User");
        String ID=String.valueOf(user.getUser_Id());
        String DST=String.valueOf(100);
        params.add(new BasicNameValuePair("id",ID));
        params.add(new BasicNameValuePair("distance", DST));
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.POST, params);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONArray contacts = new JSONArray(jsonStr);

                // Getting JSON Array node


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

                    String User_Id = c.getString(TAG_ID);
                    String User_Full_Name = c.getString(TAG_NAME);
                    String User_Image=c.getString(TAG_IMAGE);


                    // Phone node is JSON Object


                    // tmp hashmap for single contact

                    HashMap<String, String> contact = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    contact.put(TAG_ID, User_Id);
                    contact.put(TAG_NAME, User_Full_Name);
                    contact.put(TAG_IMAGE, User_Image);


                    // adding contact to contact list
                    contact_List.add(contact);

                }
                return contact_List;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }

    @Override
    protected void onPostExecute(final ArrayList<HashMap<String, String>> result) {

        // Dismiss the progress dialog
        if (pDialog.isShowing())

            //aj
        contactList.addAll(result);
        customGridAdapter = new GridViewAdapter(MeetPeople.this, R.layout.row_grid, contactList);
        gridView.setAdapter(customGridAdapter);
        super.onPostExecute(result);

        pDialog.dismiss();

    }



}

这是My Gridview Adapter

public class GridViewAdapter extends BaseAdapter {
private Context context;
private int layoutResourceId;
private ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
HashMap<String, String> consumerdata;
public GridViewAdapter(Context context, int layoutResourceId,ArrayList<HashMap<String, String>> data) {
   // super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ViewHolder holder = null;

    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new ViewHolder();
        holder.imageTitle = (TextView) row.findViewById(R.id.text);
        holder.image = (ImageView) row.findViewById(R.id.image);
        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }
   consumerdata = new HashMap<String, String>();
    consumerdata = data.get(position);


    holder.imageTitle.setText(getItem());
   if(getImage()!=null){
        holder.image.setImageBitmap(getImage());
   }else
   {
       Drawable myDrawable = context.getResources().getDrawable(R.drawable.no_image);
       Bitmap anImage      = ((BitmapDrawable) myDrawable).getBitmap();
       holder.image.setImageBitmap(anImage);
   }
    return row;
}

static class ViewHolder {
    TextView imageTitle;
    ImageView image;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return data.size();
}

public String getItem() {

    return consumerdata.get("User_Full_Name");
}
  public Bitmap getImage() {
String  urlname = consumerdata.get("User_Image");
 if(!urlname.isEmpty() ){
URL url_value = null;
try {
    url_value = new URL(urlname);
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

    Bitmap bitmap = null;
    try {
        bitmap = BitmapFactory.decodeStream(url_value.openConnection().getInputStream());
         return bitmap;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }

 } else
 {
     Drawable myDrawable = context.getResources().getDrawable(R.drawable.no_image);
     Bitmap anImage      = ((BitmapDrawable) myDrawable).getBitmap();
     return anImage;
 }


 }
  @Override
  public long getItemId(int Position) {
    // TODO Auto-generated method stub
    return 0;
  }

   @Override
  public Object getItem(int Position) {
    // TODO Auto-generated method stub
    return Position;
}

}  这是我的row_grid.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="vertical"
android:padding="5dp"
android:clickable="true"
android:background="@drawable/grid_color_selector"
android:focusable="true">

<ImageView
    android:id="@+id/image"
    android:layout_width="100dp"
    android:layout_height="100dp" >
</ImageView>

<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:gravity="center"
    android:textSize="12sp" >
</TextView>

我想在gridView项目监听器上的用户可以任何人帮助我 请 我是android的新手

我使用row_grid.xml编辑

2 个答案:

答案 0 :(得分:1)

首先尝试将android:descendantFocusability="blocksDescendants"添加到根布局

如果上述解决方案无效,请尝试在您的row.setOnClickListener(...) getView(.....)中添加GridViewAdapter

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;

 ............ 
consumerdata = data.get(position);
 .........

 row.setOnClickListener(new OnClickListener() {
            @Override 
            public void onClick(View v) {
                Intent i =new Intent(getApplicationContext(),SingleUserProfile.class);
                i.putExtra("id", position);
                startActivity(i);

                }

            });
return row;
}

答案 1 :(得分:0)

从row_grid.xml中的根android:focusable="true"删除android:clickable="true"LinearLayout