在UI线程中使用setAdapter时listView中的项目具有白色文本 [英] Items in listView have white text when using setAdapter in UI thread

查看:87
本文介绍了在UI线程中使用setAdapter时listView中的项目具有白色文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于另一个活动中的另一个listView,项目的文本颜色为黑色,应该是黑色.但是,在另一个活动中,当新项创建时在新线程中使用setAdapter时,当我希望它为黑色时,文本颜色为白色.这是Layout和Java代码的内容:

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

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.

推荐答案

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

You shouldn't be doing getApplicationContext(), instead try using Search.this.

这篇关于在UI线程中使用setAdapter时listView中的项目具有白色文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆