gpt4 book ai didi

android - 在 UI 线程中使用 setAdapter 时,listView 中的项目有白色文本

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:51:15 26 4
gpt4 key购买 nike

对于另一个 Activity 中的另一个 listView,项目的文本颜色是黑色的,就像它应该的那样。但是,在另一个 Activity 中,当新项目创建时在新线程中使用 setAdapter 时,当我希望它为黑色时,文本颜色为白色。以下是布局和 Java 代码的内容:

<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代码:

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);
}
}

我该如何解决这个问题?没有发生任何错误。

最佳答案

您不应该使用 getApplicationContext(),而是尝试使用 Search.this

关于android - 在 UI 线程中使用 setAdapter 时,listView 中的项目有白色文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25466131/

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