gpt4 book ai didi

android - ListView onItemClickListener 不工作 Android

转载 作者:太空狗 更新时间:2023-10-29 16:37:32 25 4
gpt4 key购买 nike

我看过很多帖子,很多教程,但我找不到完成这项工作的关键。我有一个带有好友请求的 ListView ,因为你看到这些请求有 2 个按钮(接受或拒绝),我想知道我怎么知道点击了哪个按钮(目前它甚至无法告诉我是否点击了一个项目).

请帮忙,我很绝望。

这是 Activity 的代码

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class Solicitudes extends Activity {
private ProgressDialog pDialog;
private static final String SOLICITUDES_URL = "http://192.168.56.1/webservice/cargarSolicitudes.php";
JSONParser jsonParser = new JSONParser();
private ListView lv;
// JSON IDS:

private static final String TAG_POSTS = "posts";

private static final String TAG_USERNAME = "username";
private HashMap<String, String> user;
SessionManager session;

// array con los amigos
private JSONArray mAmigos = null;
// organiza los amigos en una lista.
private ArrayList<HashMap<String, String>> mAmigosList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_solicitudes);
lv = (ListView) findViewById(R.id.listViewSolicitudes);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked
int itemPosition = position;

// ListView Clicked
String itemValue = (String) lv.getItemAtPosition(position);

// Show Alert
Toast.makeText(
getApplicationContext(),
"Position :" + itemPosition + " ListItem : "
+ itemValue, Toast.LENGTH_LONG).show();


}
});


}

private void updateList() {
// Para un ListActivity tenemos que tener un List Adapter. Este
// simpleAdapter
// utilizará nuestro ArrayList Hashmap actualizado. Utilizo
// single_request.xml para
// cada item en la lista y pongo la informaación correcta para cada ID.

ListAdapter adapter = new SimpleAdapter(this, mAmigosList,
R.layout.single_request, new String[] { TAG_USERNAME },
new int[] { R.id.textViewAmigoUsuario });

lv.setAdapter(adapter);

}

public void updateJSONdata() {
// Session class instance
session = new SessionManager(getApplicationContext());
// get user data from session
user = session.getUserDetails();
String username = user.get(SessionManager.KEY_USERNAME);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
mAmigosList = new ArrayList<HashMap<String, String>>();

JSONObject json = jsonParser.makeHttpRequest(SOLICITUDES_URL, "POST",
params);

// cuando parseamos JSON es conveniente hacer un catch de las
// excepciones

try {

mAmigos = json.getJSONArray(TAG_POSTS);

// Iterando para todos los post que nos devuelve el objeto json
for (int i = 0; i < mAmigos.length(); i++) {
JSONObject c = mAmigos.getJSONObject(i);

// devuelve el contenido de la etiqueta
String idUser1 = c.getString(TAG_USERNAME);

// creando un nuevo HashMap
HashMap<String, String> map = new HashMap<String, String>();

map.put(TAG_USERNAME, idUser1);
// map.put(TAG_ID2, idUser2);

// agrego el HashList al ArrayList
mAmigosList.add(map);

}

} catch (JSONException e) {
e.printStackTrace();
}
}

@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
// cargando los amigos a través de AsynTask
new buscarSolicitudes().execute();
}

public class buscarSolicitudes extends AsyncTask<Void, Void, Boolean> {

@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getApplicationContext());
pDialog.setMessage("Cargando solicitudes...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
}

@Override
protected Boolean doInBackground(Void... arg0) {
updateJSONdata();
return null;

}

@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
pDialog.dismiss();
updateList();
}
}


}

activity_layout的代码

<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"
android:background="#ffffff"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Buscar" >

<TextView
android:id="@+id/textViewSolicitudes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:gravity="center"
android:text="@string/solicitudes"
android:textColor="@color/AzulClaro" />


<ListView
android:id="@+id/listViewSolicitudes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@android:id/empty"
android:layout_below="@id/textViewSolicitudes"
tools:listitem="@layout/single_request"
android:focusable="false"
android:descendantFocusability="blocksDescendants"
android:drawSelectorOnTop="true" />

</RelativeLayout>

以及关于 single_request 布局的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#f0f0f0"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical" >

<LinearLayout
android:id="@+id/boxSF1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:orientation="horizontal" >

<ImageView
android:id="@+id/imageUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/perfildefecto" />

<RelativeLayout
android:id="@+id/boxSF2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:orientation="vertical"
android:padding="5dp" >

<TextView
android:id="@+id/textViewAmigoUsuario"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:paddingBottom="2dip"
android:paddingLeft="5dp"
android:paddingTop="6dip"
android:textColor="#333"
android:textSize="16sp"
android:textStyle="bold" />


<Button
android:id="@+id/btnEliminar"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/textViewSolicitud"
android:layout_marginTop="30dp"
android:background="@drawable/denegar"
android:max="100" />

<Button
android:id="@+id/btnAgregar"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignBaseline="@+id/btnEliminar"
android:layout_alignBottom="@+id/btnEliminar"
android:layout_marginRight="24dp"
android:layout_toLeftOf="@+id/btnEliminar"
android:background="@drawable/aceptar"
android:max="100" />

<TextView
android:id="@+id/textViewSolicitud"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_below="@id/textViewAmigoUsuario"
android:layout_margin="10dp"
android:gravity="left"
android:text="@string/friend_request"
android:textColor="@color/AzulClaro" />

</RelativeLayout>
</LinearLayout>
</LinearLayout>

</LinearLayout>

最佳答案

onItemClicked 未被调用,因为在您的行中有 Button。添加

android:descendantFocusability="blocksDescendants"

到你的 single_request 布局的根

关于android - ListView onItemClickListener 不工作 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25033669/

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