2
在我的活動中,我使用SimpleAdapter作爲listView。我添加了一個圖像和一個textView以顯示在listView中。如果我刪除圖像,那麼列表工作正常。意味着我可以點擊列表項並且可以轉到下一個活動。但是當我添加一個圖像時,我無法點擊List的項目。這是我下面全碼:無法獲取onItemClickListener中的項目
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>
請告訴我,我做錯了在代碼中。
請張貼布局(XML)的文件。 – OAEI 2014-08-30 10:04:29
附加'list_text_black.xml'以及 – 2014-08-30 10:06:01
請檢查此行ArrayAdapter adapter = new ArrayAdapter (getApplicationContext(),R.layout.list_text_black,R.id.list_item,r); arraylist是r並且項目點擊r1 –
2014-08-30 10:10:53