CustomListView OnItemClickListener 不工作

CustomListView OnItemClickListener not working

我使用本地数据库获取记录并使用它创建自定义列表视图。自定义列表视图得到完美显示。问题出在 Onitemclicklistener 上。单击它不会执行任何操作。我的目标是将点击项目的位置发送到另一个 activity。我实施了它,但它不起作用。 列表视图的屏幕截图 - https://www.dropbox.com/s/pz83i162sxdv2b0/Untitled.png?dl=0

这是我的MainActivity.java:

public class MainActivity extends ActionBarActivity {
ListView lv;
TextView tv1,tv2,tv3;
ArrayList<String> a=new ArrayList<String>();
String mydata,name,name1;
public String[] s1 = new String[50];
public int[] img = {R.drawable.rty, R.drawable.sf, R.drawable.rty};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv1=(TextView)findViewById(R.id.textView);
    lv = (ListView) findViewById(R.id.listView);
    new MyData().execute();

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent in = new Intent(MainActivity.this, listclick.class);
            in.putExtra("position", position);
            startActivity(in);
        }
    });
}
public void abc(View v)
{
    Intent in=new Intent(MainActivity.this,webform.class);
    startActivity(in);

}
public class MyData extends AsyncTask<String,String,String>
{
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        CustomAdapter cad = new CustomAdapter(MainActivity.this, s1, img);
        lv.setAdapter(cad);
    }

    @Override
    protected String doInBackground(String... params) {
        getData();
        return null;
    }
}

public void getData()
{
    try {
        HttpClient httpClient=new DefaultHttpClient();

        HttpPost httpPost=new HttpPost("http://10.0.2.2/abcd.php");
        HttpResponse response=httpClient.execute(httpPost);
        HttpEntity httpEntity=response.getEntity();
        InputStream is=httpEntity.getContent();
        BufferedReader reader=new BufferedReader(new InputStreamReader(is,"utf-8"),8);
        StringBuilder strbuilder=new StringBuilder();
        String line=null;
        while ((line=reader.readLine())!=null)
        {
            strbuilder.append(line);
        }
        is.close();
        mydata=strbuilder.toString();
        JSONArray obj=new JSONArray(mydata);
        for(int i=0;i<obj.length();i++)
        {

            JSONObject obj1=obj.getJSONObject(i);
            a.add(i,obj1.getString("Name"));
        }
        String[] s = new String[a.size()];
        s=a.toArray(s);
        s1 = s;
    }
    catch (Exception e)
    {

    }


}


}

这是第二个activitylistclick.java

public class listclick extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listclick);
    Intent in=getIntent();
    String s1=in.getStringExtra("position");
   }
}

这里是Customlist.xml(布局文件)

 <?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF9200"
android:clickable="true">
<TableRow>
    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/sf"

        android:id="@+id/imageview"
        />
    <TextView
        android:layout_height="70dp"
        android:layout_width="fill_parent"
        android:textSize="20dp"
        android:fitsSystemWindows="true"
        android:textStyle="bold"

        android:layout_gravity="center"
        android:gravity="center"
        android:paddingLeft="20dp"
        android:text="Codewars 3.0"
        android:id="@+id/textView"
        />
</TableRow>
</TableLayout>

这里是CustomAdapter.java

public class CustomAdapter extends ArrayAdapter<String>{

Context c1;
String s1[];
int s2[];
CustomAdapter(Context c,String s[],int s3[])
{
    super(c,R.layout.listcustom,s);
    this.c1=c;
    this.s1=s;
    this.s2=s3;
}


@Override
public View getView(int position, View v, ViewGroup parent) {
    LayoutInflater li=(LayoutInflater) c1.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v=li.inflate(R.layout.listcustom,null);
    TextView tv=(TextView)v.findViewById(R.id.textView);
    tv.setText(s1[position]);






    return v;
}

}

您可能正在启动 activity,但您的旧版本挡住了视线。试试这个:

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent in = new Intent(MainActivity.this, listclick.class);
        in.putExtra("position", position);
        startActivity(in);
        finish();
    }
});

我建议你在适配器的 getView 方法中完成这项工作,触摸事件可能会在列表视图中发生冲突。

class MyAdapter extends BaseAdapter implements View.OnClickListener {

    ....

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        ....
        view.setOnClickListener(this);
        view.setTag(position);
        return view;
    }

    @Override
    public void onClick(View v) {
        int position = (Integer) v.getTag();
        ....
    }
}

我认为你的代码有两个问题,第一个是你的 listcustom.xml 有这个集合:

android:clickable="true"

如果您要使用列表视图的 onItemClickListener(),则不需要该部分。如果您要专门为永远的项目设置 View.OnCLickListner,则只设置可点击。所以只需删除 XML.

中的那一行

可能导致问题的下一行如下:

inflate(R.layout.listcustom,null)

您应该像这样扩充列表中的项目:

inflate(R.layout.listcustom, parent, false);