gpt4 book ai didi

java - 无法在 onItemClickListener 中获取项目

转载 作者:行者123 更新时间:2023-11-30 11:15:12 25 4
gpt4 key购买 nike

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

公司.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>

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

最佳答案

好的...我得到了解决方案..!!实际上我在 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>

关于java - 无法在 onItemClickListener 中获取项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25581185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com