For another listView in another activity the text color of the items is black, like it should be. However, in another activity when using setAdapter in a new thread when the new items created the text color is white when I want it black. Here is the contents of Layout and Java code:
<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:context="project.yemeb.Search"
android:background="#ffffffff">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="@drawable/back_web"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:id="@+id/relativeLayout"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:id="@+id/button2"
android:background="#ffffffff"
android:onClick="onBtnClick"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_alignBottom="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#ffffffff"
android:layout_toLeftOf="@+id/button2"
android:layout_toStartOf="@+id/button2"
android:layout_alignTop="@+id/button2" />
</RelativeLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentBottom="true"
android:layout_below="@+id/relativeLayout"
android:headerDividersEnabled="false" />
</RelativeLayout>
Java code:
package project.yemeb;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
public class Search extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");
((EditText) findViewById(R.id.editText)).setText(message);
final String url = "http://example.com/sql.php?keyword="+((EditText) findViewById(R.id.editText)).getText().toString()+"&mode=6";
new Thread() {
@Override
public void run() {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(url));
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
response.getEntity().writeTo(out);
out.close();
} catch (IOException e) {
}
String responseString = out.toString();
String[] list = responseString.split("\\|");
for(String f : list)
{
((MyApplication)getApplication()).setSearches(f);
}
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, ((MyApplication)getApplication()).getSearches());
runOnUiThread(new Runnable() {
@Override
public void run() {
((ListView) findViewById(R.id.listView)).setAdapter(adapter);
}
});
((ListView) findViewById(R.id.listView)).setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent intent = new Intent(getApplicationContext(), List.class);
intent.putExtra("message", ((String) ((ListView) findViewById(R.id.listView)).getItemAtPosition(arg2)));
startActivity(intent);
}
});
//..more logic
} else {
//Closes the connection.
try {
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
} catch (IOException e) {
}
}
}
catch (ClientProtocolException e)
{
}
catch (IOException e)
{
}
}
}.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
How do I solve this problem? There were no errors that occurred.
Navigate to the app > res > layout > activity_main. xml and add the below code to that file. Below is the code for the activity_main. xml file.
This example demonstrates how do I dynamically update a ListView in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
Android ListView is a view which groups several items and display them in vertical scrollable list. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database.
You shouldn't be doing getApplicationContext()
, instead try using Search.this
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With