CustomListView OnItemClickListener无法正常工作

时间:2015-07-14 15:50:22

标签: android listview android-listview android-custom-view onitemclicklistener

我使用本地数据库来获取记录并使用它创建自定义列表视图。自定义列表视图可以完美显示。问题出在Onitemclicklistener上。它在点击时没有做任何事情。我的目标是将点击的项目的位置发送到另一个活动。我实现了它,但它没有工作。 列表视图的屏幕截图 -     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)
    {

    }


}


}

这是第二个活动listclick.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;
}

}

2 个答案:

答案 0 :(得分:1)

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

android:clickable="true"

如果您要使用listview的onItemClickListener(),那么您不需要该部分。如果您要专门为任何项目设置View.OnCLickListner,则仅设置clickable。所以只需删除XML中的那一行。

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

inflate(R.layout.listcustom,null)

您应该像这样对列表中的项目进行充气:

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

答案 1 :(得分:0)

您可能正在启动该活动,但您的旧活动正在阻止该视图。试试这个:

index_6