0
I’m doing my TCC and I’m having trouble removing the ID of some user from Listview and using it in another Activity (The intention is that when the user holds the click on someone from the list, a popup (popAdd) appears asking if he wants to send a friend request to that person (And for that I need to know the ID of the chosen person)
popAddAmigo.java
public class popAddAmigo extends Activity {
String urlAddress="http://192.168.1.107/line/Pesquisa.php";
// String urlAddress="http://172.16.2.15/line/Pesquisa.php";
SearchView sv;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popaddamigo);
lv = (ListView) findViewById(R.id.listaAmigos);
sv = (SearchView) findViewById(R.id.svPesquisa);
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
SenderReceiver sr = new SenderReceiver(popAddAmigo.this, urlAddress, query, lv);
sr.execute();
return false;
}
@Override
public boolean onQueryTextChange(String query) {
SenderReceiver sr = new SenderReceiver(popAddAmigo.this, urlAddress, query, lv);
sr.execute();
return false;
}
});
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int pos, long id) {
Intent abreAdd = new Intent(popAddAmigo.this, popAdd.class);
startActivity(abreAdd);
return false;
}
});
}
}
Parser.java
public class Parser extends AsyncTask<Void,Void,Integer> {
Context c;
String data;
ListView lv;
ArrayList<String> names = new ArrayList<>();
public Parser(Context c, String data, ListView lv) {
this.c = c;
this.data = data;
this.lv = lv;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Integer doInBackground(Void... params) {
return this.parse();
}
@Override
protected void onPostExecute(Integer integer) {
super.onPostExecute(integer);
if(integer==1) {
ArrayAdapter adapter = new ArrayAdapter(c,R.layout.listalayout,names);
lv.setAdapter(adapter);
} else {
Toast.makeText(c,"Não encontramos resultado :(",Toast.LENGTH_SHORT).show();
}
}
private int parse() {
try {
JSONArray ja = new JSONArray(data);
JSONObject jo = null;
names.clear();
for(int i=0;i<ja.length();i++) {
jo=ja.getJSONObject(i);
String name = jo.getString("nome");
names.add(name);
}
return 1;
} catch (JSONException e) {
e.printStackTrace();
}
return 0;
}
}
Senderreceiver.java
public class SenderReceiver extends AsyncTask<Void,Void,String> {
Context c;
String urlAddress;
String query;
ListView lv;
ProgressDialog pd;
public SenderReceiver(Context c, String urlAddress, String query, ListView lv,ImageView...imageViews) {
this.c = c;
this.urlAddress = urlAddress;
this.query = query;
this.lv = lv;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd=new ProgressDialog(c);
pd.setTitle("Pesquisando...");
pd.setMessage("Por favor aguarde.");
pd.show();
}
@Override
protected String doInBackground(Void... params) {
return this.sendAndReceive();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
pd.dismiss();
lv.setAdapter(null);
if(s != null) {
if(!s.contains("null")) {
Parser p = new Parser(c,s,lv);
p.execute();
} else {
Toast.makeText(c.getApplicationContext(), "Usuário não encontrado.", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(c.getApplicationContext(), "Nenhuma conexão com a Internet foi encontrada.", Toast.LENGTH_LONG).show();
}
}
private String sendAndReceive()
{
HttpURLConnection con = Connector.connect(urlAddress);
if(con==null) {
return null;
} try {
OutputStream os = con.getOutputStream();
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os));
bw.write(new DataPackager(query).packageData());
bw.flush();
bw.close();
os.close();
int responseCode = con.getResponseCode();
if(responseCode==con.HTTP_OK) {
InputStream is = con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
if(br != null) {
while ((line=br.readLine()) != null) {
response.append(line+"n");
}
} else {
return null;
}
return response.toString();
} else {
return String.valueOf(responseCode);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
Datapackager.java
public class DataPackager {
String query;
public DataPackager(String query) {
this.query = query;
}
public String packageData() {
JSONObject jo = new JSONObject();
StringBuffer queryString = new StringBuffer();
try {
jo.put("Query",query);
Boolean firstValue = true;
Iterator it = jo.keys();
do {
String key = it.next().toString();
String value = jo.get(key).toString();
if(firstValue) {
firstValue = false;
} else {
queryString.append("&");
}
queryString.append(URLEncoder.encode(key,"UTF-8"));
queryString.append("=");
queryString.append(URLEncoder.encode(value,"UTF-8"));
} while (it.hasNext());
return queryString.toString();
} catch (JSONException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
}
Your class
Parser
is with the content ofSenderReceiver
. You can update it?– Danilo de Oliveira
All right, I’ve updated
– Higor
Within the method
parse()
of yourParser
, you have a Jsonobjectjo
. Inside it you have the user id too or just the name? Equal injo.getString("nome")
, only you wouldjo.getString("id")
– Danilo de Oliveira
Only the name, so if I do with the id will work? I tried here and nothing..
– Higor
No. Only the id will not work either, but you need both. I will update the answer...
– Danilo de Oliveira