无法在onItemClickListener中获取项目

时间:2014-08-30 10:00:20

标签: java android listview android-arrayadapter simpleadapter

在我的活动中,我使用SimpleAdapter for listView。我添加了一个图像和一个textView以在listView中显示。如果我删除图像,那么列表工作正常。意味着我可以单击列表项并可以转到下一个活动。但是当我添加图像时,我无法单击List的项目。以下是我的完整代码:

Company.java

public class Company extends Activity  {

    Strings st1 = new Strings();
    TextView t1, click;
    Button b1, b2, b3;
    String url, CompName, CompID;
    List<String> r1;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_company);

        r1 = new ArrayList<String>();
        url=st1.getUrl();
        connect1();
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            // do something on back.
            this.finish();
            Intent i = new Intent(Company.this, MainActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(i);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void connect1() {
        String data;
        List<String> r = new ArrayList<String>();

        List<Map<String, String>> data1 = new ArrayList<Map<String, String>>();
        ArrayAdapter<String>adapter=new ArrayAdapter<String>(getApplicationContext(),R.layout.list_text_black,R.id.list_item,r);
        final ListView list=(ListView)findViewById(R.id.listView1);

            try {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet(url+"Retrive.php?q=cat");

                HttpResponse response = client.execute(request);
                HttpEntity entity=response.getEntity();
                data=EntityUtils.toString(entity);
                Log.e("STRING", data);
                try {
                        JSONArray json=new JSONArray(data);
                        for(int i=0;i<json.length(); i++) {
                            JSONObject obj=json.getJSONObject(i);

                            CompName=obj.getString("fldCompName");
                            CompID=obj.getString("fldCompID");

                            Log.e("STRING", CompName);
                            Log.e("STRING",CompID);
                            String word=CompName.substring(0, 1);

                            r.add(CompName);
                            r1.add(CompID);
                            Map<String, String> datum = new HashMap<String, String>();
                            datum.put("comename",word);// rr1.get(i));
                            datum.put("CompName",CompName);//rr2.get(i));
                            data1.add(datum);
                            SimpleAdapter adapter1 = new SimpleAdapter(getBaseContext(), data1,R.layout.list_text_black, 
                                new String[] {"comename", "CompName" },  new int[] {R.id.imageView3,R.id.list_item });
                            list.setAdapter(adapter1);
                            list.setTextFilterEnabled(true);
                            list.setOnItemLongClickListener(new OnItemLongClickListener() {

                                @Override
                                public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                                    // TODO Auto-generated method stub
                                    String selected=r1.get(arg2);
                                    Log.e("ID :....: ",selected);
                                    Intent i1 = new Intent(Company.this,Category.class);
                                    i1.putExtra("CompID", selected);
                                    startActivity(i1);
                                    return true;
                                }
                            });
                            list.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent,View view, int position, long id) {
                                    String selected=r1.get(position);

                                    Log.e("ID :....: ",selected);
                                    Intent i1=new Intent(Company.this,Category.class);
                                    i1.putExtra("CompID", selected);
                                    startActivity(i1);
                                }
                            });
                       }
                    } catch (JSONException e)  {
                        e.printStackTrace();
                    }
                } catch (ClientProtocolException e) {
                    Log.d("HTTPCLIENT", e.getLocalizedMessage());
                } catch (IOException e) {
                    Log.d("HTTPCLIENT", e.getLocalizedMessage());
                }
    }
}

list_text_black.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:ignore="HardcodedText,ContentDescription,UselessParent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp" >

        <TextView
            android:id="@+id/imageView3"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="0dp"
            android:adjustViewBounds="true"
           android:textSize="25sp"
           android:textStyle="bold"
           android:capitalize="words"
           android:gravity="center"
            android:background="@drawable/circlered" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="10dp"
            android:adjustViewBounds="true"
            android:maxHeight="30dp"
            android:maxWidth="30dp"
            android:src="@drawable/arrow1" />

        <TextView
            android:id="@+id/list_item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/imageView1"
            android:layout_toRightOf="@+id/imageView3"
            android:text="Just Test"
            android:textColor="#000000"
            android:textSize="18sp"
            android:textStyle="bold" />

    </RelativeLayout>

</LinearLayout>

请告诉我代码中的错误。

1 个答案:

答案 0 :(得分:1)

Okk ......我得到了解决方案.. !!实际上我在list_text_black.xml文件中首先使用两个布局,然后使用相对布局。所以我删除了一个布局。这是更新的xml文件:

<强> list_text_black.xml

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"

    tools:ignore="ContentDescription" >

    <TextView
        android:id="@+id/imageView3"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="0dp"
        android:adjustViewBounds="true"
        android:background="@drawable/circle_grey"
        android:gravity="center"
        android:textSize="25sp"
        android:textStyle="bold" 
        android:layout_margin="5dp"/>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="10dp"
        android:adjustViewBounds="true"
        android:maxHeight="30dp"
        android:maxWidth="30dp"
        android:src="@drawable/arrow1"
        android:layout_margin="5dp" />

    <TextView
        android:id="@+id/list_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="45dp"
        android:textColor="#000000"
        android:textSize="18sp"
        android:textStyle="bold"
        android:layout_marginTop="5dp" 
        android:layout_marginRight="5dp"
        android:layout_marginBottom="5dp"/>

</RelativeLayout>