En primer lugar, se necesita una vista de lista, por lo
private volatile ArrayList<String> searchResults;
ListView searchList = (ListView) findViewById(R.id.listYOURLISTVIEW);
listAdapter = new ArrayAdapter<String>(this, R.layout.blacklist, R.id.list_content, searchResults); //blacklist is a layout to paint the fonts black
searchList.setAdapter(listAdapter);
Aquí hay algo que podría ayudar.
private Thread refreshThread;
private boolean isRefreshing = false;
private void reinitializeRefreshThread()
{
if (!refreshThread.equals(null)) refreshThread.stop();
Log.d(LOGTAG+"::reinitializeRefreshThread()", "Creating new thread!\n");
isRefreshing = true;
refreshThread = (new Thread(new Runnable()
{
public void run()
{
Looper.prepare();
while (isRefreshing)
{
//GRAB YOUR SEARCH RESULTS HERE
//make sure the methods are synchronized
//maybe like
String[] results = grabResults(); //doesn't need to be synchronized
addResultList(results);
Message message = myHandler.obtainMessage();
myHandler.sendMessage(message);
try
{
Thread.sleep(2000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
Log.d(LOGTAG+"->refreshThread", "Refresh finished!\n");
}
}
}));
Log.d(LOGTAG+"::reinitializeRefreshThread()", "Refresh thread started!\n");
refreshThread.start();
}
final Handler myHandler = new Handler()
{
public void handleMessage(android.os.Message msg)
{
listAdapter.notifyDataSetChanged();
};
};
donde
public synchronized void addResultList(String[] results)
{
// remove the whole list, repopulate with new data
searchResults.clear();
for (int i = 0; i < results.length; i++)
{
addResult(results[i]);
}
}
private synchronized void addResult(CharSequence result)
{
if (!searchResults.contains(result.toString()))
searchResults.add(result.toString());
else
Toast.makeText(getApplicationContext(), "Result " + result.toString() + " was already on the list!", Toast.LENGTH_SHORT).show();
}
Todo esto es seguro para subprocesos, lo que significa que le permitirá 'cambio' interfaz gráfica de usuario a partir de hilos que no sean el hilo principal mediante el envío del mensaje al hilo principal que la La interfaz de usuario necesita una actualización. Como puede ver, refreshThread actualiza la colección de búsqueda (asegúrese de que el método esté sincronizado) y luego notifica al hilo principal (UI) para actualizar la lista.
es probable encontrar este útil también
searchList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
listIndex = arg2;
selectedItem = (String) searchList.getItemAtPosition(listIndex);
Toast.makeText(getApplicationContext(), "Result[" + listIndex + "] " + selectedItem + " selected!", Toast.LENGTH_SHORT).show();
}
});
Puede haber algunos cabos sueltos, tales como sea necesario para inicializar LISTINDEX y selectedItem o simplemente tirar A, pero en general esto hizo el truco para mí en una situación muy similar (no tengo un subproceso de fondo que introduce en una lista en el tiempo con las nuevas entradas)
espero que ayude :)
Deberá realizar la búsqueda en un hilo separado más probablemente si no desea que la búsqueda bloquee la ejecución de su aplicación principal. Luego consígalo para actualizar periódicamente un contenedor con nuevos objetos TextView. Por lo tanto, podría tener un diseño lineal que esté vacío y llamar periódicamente al hilo de búsqueda para ver qué resultados tiene y agregarlos a su diseño lineal. – DeliveryNinja
¿Su pregunta sigue sin respuesta? – winklerrr