ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-10-07 23:27:07  阅读:319  来源: 互联网

标签:items android listview custom-adapter


对于另一个活动中的另一个listView,项目的文本颜色是黑色的,就像它应该的那样.但是,在另一个活动中,当新项目创建时,在新线程中使用setAdapter时,如果我想要黑色,则文本颜色为白色.以下是Layout和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.

标签:items,android,listview,custom-adapter
来源: https://codeday.me/bug/20191007/1869317.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有