Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法在McClickListener中获取项_Java_Android_Listview_Android Arrayadapter_Simpleadapter - Fatal编程技术网

Java 无法在McClickListener中获取项

Java 无法在McClickListener中获取项,java,android,listview,android-arrayadapter,simpleadapter,Java,Android,Listview,Android Arrayadapter,Simpleadapter,在我的活动中,我将SimpleAdapter用于listView。我添加了一个图像和一个文本视图来显示在listView中。若我删除图像,那个么列表就可以了。意味着我可以点击列表项并转到下一个活动。但当我添加图像时,我无法单击列表项。以下是我的完整代码: Company.java public class Company extends Activity { Strings st1 = new Strings(); TextView t1, click; Button

在我的活动中,我将SimpleAdapter用于listView。我添加了一个图像和一个文本视图来显示在listView中。若我删除图像,那个么列表就可以了。意味着我可以点击列表项并转到下一个活动。但当我添加图像时,我无法单击列表项。以下是我的完整代码:

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());
                }
    }
}
公共类公司扩展活动{
Strings st1=新字符串();
text查看t1,单击;
按钮b1、b2、b3;
字符串url、CompName、CompID;
列表r1;
@SuppressLint(“新API”)
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_company);
r1=新的ArrayList();
url=st1.getUrl();
连接1();
}
公共布尔onKeyDown(int-keyCode,KeyEvent事件){
if(keyCode==KeyEvent.keyCode\u BACK&&event.getRepeatCount()==0){
//在背后做点什么。
这个;
意向i=新意向(Company.this,MainActivity.class);
i、 设置标志(意图、标志、活动、清除、顶部);
星触觉(i);
返回true;
}
返回super.onKeyDown(keyCode,event);
}
私有void connect1(){
字符串数据;
列表r=新的ArrayList();
List data1=new ArrayList();
ArrayAdapteradapter=新的ArrayAdapter(getApplicationContext(),R.layout.list\u text\u black,R.id.list\u item,R);
最终ListView列表=(ListView)findViewById(R.id.listView1);
试一试{
DefaultHttpClient=新的DefaultHttpClient();
HttpGet请求=新的HttpGet(url+“retrve.php?q=cat”);
HttpResponse response=client.execute(请求);
HttpEntity=response.getEntity();
数据=EntityUtils.toString(实体);
Log.e(“字符串”,数据);
试一试{
JSONArray json=新的JSONArray(数据);
for(int i=0;i父项、视图、int位置、长id){
所选字符串=r1.get(位置);
Log.e(“ID:……”,选中);
意向i1=新意向(公司、本、类别、类别);
i1.putExtra(“CompID”,选中);
星触觉(i1);
}
});
}
}捕获(JSONException e){
e、 printStackTrace();
}
}捕获(客户端协议例外e){
Log.d(“HTTPCLIENT”,例如getLocalizedMessage());
}捕获(IOE异常){
Log.d(“HTTPCLIENT”,例如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>


请告诉我我在代码中哪里做错了。

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

列表\u文本\u 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>


请同时发布布局(xml)文件。附加
列表文本\黑色。xml
请检查此行ArrayAdapteradapter=new ArrayAdapter(getApplicationContext(),R.layout.list文本\黑色,R.id.list \项,R);arraylist为r,项目单击r1