我有一个大问题。我用UserSearchManager的搜索工作正常,但我不明白我如何将搜索结果放在我的列表视图中。
搜索中的结果类型保存在列表中,我想从行中获取值并将行的结果放在我的列表中查看Android中,但我不明白,我应该如何开始?
所以我希望你能帮助我。
此外,Iterator还有另一个问题。
Android Studio说它无法解决问题,演员不会帮助我。
Iterator<Row> it = resultData.getRows();
Row row = it.next();
Iterator iterator = row.getValues("jid");
这就是问题所在。在许多其他线程中,人们使用相同的代码片段并且工作正常,但对我来说却没有。
这是我的完整代码。
package de.hsrt.campusapp.app.module.xmppchat;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smackx.iqregister.packet.Registration;
import org.jivesoftware.smackx.search.ReportedData;
import org.jivesoftware.smackx.search.UserSearchManager;
import org.jivesoftware.smackx.xdata.Form;
import org.jxmpp.jid.DomainBareJid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import de.hsrt.campusapp.app.CampusAppConstants;
import de.hsrt.campusapp.app.R;
public class ActivitySearchContact extends AppCompatActivity {
private final static String serverDomain = "search.ewcexmpp1";
private static DomainBareJid serverDomainBareJid;
private ReportedData resultData;
private ListView contactList;
private EditText contactName;
private ImageButton searchContact;
private XMPPTCPConnection con = CampusAppConstants.xmppController.getNetworkManager().getXmppTCPConnection();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_contact);
contactList = (ListView) findViewById(R.id.listViewForSearchAnswer);
contactName = (EditText) findViewById(R.id.editTextContactInput);
searchContact = (ImageButton) findViewById(R.id.btn_searchContactDetail);
searchContact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String strContact = contactName.getText().toString();
userSearch(strContact);
}
});
}
/**
* Diese Methode soll mir alle gesuchten Kontakte zurückliefern
*
* @return
*/
public ReportedData userSearch(String searchname) {
//hier eventuell die campusappconstants mit der verbindung benutzen
UserSearchManager userSearchManager = new UserSearchManager(con);
try {
Form searchForm = userSearchManager.getSearchForm(getServerDomainBareJid());
//erstellt die antwort form
Form answerForm = searchForm.createAnswerForm();
//die eigentliche suche, ich suche in der spalte Username
answerForm.setAnswer("Username", true);
answerForm.setAnswer("search", searchname);
//hier hol ich das ergebnis
this.resultData = userSearchManager.getSearchResults(answerForm, getServerDomainBareJid());
Toast.makeText(this, resultData.getRows().toString(), Toast.LENGTH_LONG).show();
if(resultData.getRows() != null)
{
Iterator<Row> it = resultData.getRows();
while(it.hasNext())
{
Row row = it.next();
Iterator iterator = row.getValues("jid");
if(iterator.hasNext())
{
String value = iterator.next().toString();
Log.i("Iteartor values......"," "+value);
}
//Log.i("Iteartor values......"," "+value);
}
Toast.makeText(this,"Username Exists",Toast.LENGTH_SHORT).show();
}
}catch(Exception e){
e.printStackTrace();
e.getMessage();
//Log.e(this.getClass().getSimpleName().toString(), "Fehler bei der Suche", e);
}
return this.resultData;
}
private static DomainBareJid getServerDomainBareJid() {
try {
serverDomainBareJid = JidCreate.domainBareFrom(serverDomain);
} catch (XmppStringprepException e) {
e.printStackTrace();
}
return serverDomainBareJid;
}
}