Android Gridview侦听器不工作
在我的应用程序中,我有网格视图,我编写了一个侦听器来获取网格视图中被单击项的位置。我尝试了很多方法来解决这个问题,但都不管用。我正在寻找解决方案或帮助。我检查了所有内容,但无法清除它。我不知道我在哪里犯了错误。有人帮我解决这个问题吗Android Gridview侦听器不工作,android,gridview,onclicklistener,baseadapter,onitemclicklistener,Android,Gridview,Onclicklistener,Baseadapter,Onitemclicklistener,在我的应用程序中,我有网格视图,我编写了一个侦听器来获取网格视图中被单击项的位置。我尝试了很多方法来解决这个问题,但都不管用。我正在寻找解决方案或帮助。我检查了所有内容,但无法清除它。我不知道我在哪里犯了错误。有人帮我解决这个问题吗 rowitems xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/an
rowitems xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="30dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="@drawable/customborder"
android:gravity="center_vertical"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageView
android:id="@+id/product_img"
android:layout_width="350dp"
android:layout_height="150dp"
android:layout_alignParentLeft="true"
android:src="@drawable/ic_launcher"
android:focusable="false"
android:focusableInTouchMode="false"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_weight="0.5"
android:background="#D7D7D7" >
<TextView
android:id="@+id/product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="37dp"
android:text="@string/pname"
android:textAppearance="?android:attr/textAppearanceMedium"
android:focusable="false"
android:focusableInTouchMode="false"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="75dp" >
<Button
android:id="@+id/product_decribe_cart"
style="?android:attr/buttonStyleSmall"
android:layout_width="75dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="46dp"
android:background="#D41A37"
android:text="@string/cart"
android:textColor="@android:color/white"
android:textSize="8dp"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:id="@+id/product_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="40dp"
android:text="@string/rate"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#D41A37"
android:focusable="false"
android:focusableInTouchMode="false"/>
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="262dp"
android:layout_height="113dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:background="@drawable/customborder" >
<TextView
android:id="@+id/product_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:text="Product details"
android:textAppearance="?android:attr/textAppearanceMedium"
android:focusable="false"
android:focusableInTouchMode="false"/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class ProductActivity extends Activity
{
JSONArray results;
JSONObject jobj;
CustomProdAdapter adapter1;
ArrayList<String> a1,a2,a3,a4;
ArrayList<String> pname=new ArrayList<String>();
ArrayList<String> productdescription=new ArrayList<String>();
ArrayList<String> pimage=new ArrayList<String>();
ArrayList<String> pprice=new ArrayList<String>();
Button add;
String pid;
GridView gView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.product_activity);
a1=new ArrayList<String>();
a2=new ArrayList<String>();
a3=new ArrayList<String>();
a4=new ArrayList<String>();
Intent in=getIntent();
pid=in.getStringExtra("Dialoog");
System.out.println("productid"+pid);
gView = (GridView)findViewById(R.id.pa_grid);
/* gView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
System.out.println("inside listener");
}
});*/
new Producttask().execute();
}
private class Producttask extends AsyncTask<String, String, JSONObject>
{
private ProgressDialog pDialog;
@Override
protected void onPreExecute()
{
super.onPreExecute();
pDialog = new ProgressDialog(ProductActivity.this);
pDialog.setMessage("Loading Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args)
{
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl("my link");
return json;
}
@Override
protected void onPostExecute(JSONObject json)
{
System.out.println("---------------return product list json------------"+json);
pDialog.dismiss();
try
{
jobj=json.getJSONObject("response");
// Getting Array of Contacts
results = jobj.getJSONArray("obejects");
System.out.println("In product Activity after JSON");
// looping through All Contacts
for(int i = 0; i < results.length(); i++)
{
JSONObject c = results.getJSONObject(i);
pname.add(c.getString("product_name"));
pimage.add(c.getString("product_image"));
pprice.add(c.getString("product_price"));
productdescription.add(c.getString("Description"));
}
}
catch (JSONException e)
{
e.printStackTrace();
}
adapter1 = new CustomProdAdapter(getApplicationContext(), R.layout.product_describe, pname, pimage,productdescription,pprice);
gView.setAdapter(adapter1);
gView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
System.out.println("outside on click"+position);
}
});
/*gv=(GridView)findViewById(R.id.product_decribe_cart);
gv.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(ProductActivity.this,product_details.class);
startActivity(i);
}
});
*/ }
}
}
rowitems xml
导入java.util.ArrayList;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.AdapterView;
导入android.widget.Button;
导入android.widget.GridView;
导入android.widget.Toast;
导入android.widget.AdapterView.OnItemClickListener;
公共类ProductActivity扩展了活动
{
JSONArray结果;
JSONObject jobj;
CustomProdAdapter适配器1;
阵列列表a1、a2、a3、a4;
ArrayList pname=新的ArrayList();
ArrayList productdescription=新建ArrayList();
ArrayList pimage=新的ArrayList();
ArrayList pprice=新的ArrayList();
按钮添加;
串pid;
GridView;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.product_活动);
a1=新的ArrayList();
a2=新的ArrayList();
a3=新的ArrayList();
a4=新的ArrayList();
Intent in=getIntent();
pid=in.getStringExtra(“Dialoog”);
System.out.println(“产品ID”+pid);
gView=(GridView)findViewById(R.id.pa_网格);
/*gView.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共链接(AdapterView arg0、视图arg1、内部arg2、,
长arg3){
//TODO自动生成的方法存根
System.out.println(“内部侦听器”);
}
});*/
新建Producttask().execute();
}
私有类Producttask扩展了AsyncTask
{
私人对话;
@凌驾
受保护的void onPreExecute()
{
super.onPreExecute();
pDialog=newprogressdialog(ProductActivity.this);
setMessage(“正在加载数据…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
@凌驾
受保护的JSONObject doInBackground(字符串…args)
{
JSONParser jParser=新的JSONParser();
JSONObject json=jParser.getJSONFromUrl(“我的链接”);
返回json;
}
@凌驾
受保护的void onPostExecute(JSONObject json)
{
System.out.println(“--------------退货产品列表json---------”+json);
pDialog.disclose();
尝试
{
jobj=json.getJSONObject(“响应”);
//获取联系人数组
结果=jobj.getJSONArray(“对象”);
System.out.println(“JSON之后的产品内活动”);
//通过所有触点循环
对于(int i=0;i
网格视图列表
new Producttask().execute();
gView = (GridView)findViewById(R.id.pa_grid);
gView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
System.out.println("outside on click"+position);
}
});
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
View row=convertView;
ViewHolder vh;
if(row==null)
{
row=inflater.inflate(R.layout.custom_product_activity, parent, false);
vh=new ViewHolder();
vh.pname=(TextView)row.findViewById(R.id.product_name);
vh.image1=(ImageView) row.findViewById(R.id.imageView1);
row.setTag(vh);
}
else
{
vh = (ViewHolder) row.getTag();
row=convertView;
}
vh.pname.setText(c_pname.get(position));
iloader.DisplayImage(c_pimage.get(position), vh.image1);
row.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
System.out.println("outside on click"+"position");
}
});
return row;
}