检测到连接更改时,更改ListView中随机项的颜色

时间:2017-04-12 09:28:08

标签: android listview design-patterns android-broadcastreceiver

我有一个接收连接变化广播的广播接收器。因此每当检测到连接变化时就会触发它。我有一个包含10个项目的列表视图。每个项目都有一个EditText,其中包含该特定项目从0到9的位置当接收到广播时,根据生成的随机数生成从0到9的随机数,我想改变相应文本视图中文本的颜色。怎么实现这个?

我目前的代码是:

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.app.prateek.listviewbroadcastreceiver">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

MyReceiver.java

package com.app.prateek.listviewbroadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import java.util.Random;

public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.

        int min = 0;
        int max = 9;

        Random r = new Random();
        int highlight = r.nextInt(max - min + 1) + min;
        Toast.makeText(context, ""+highlight, Toast.LENGTH_SHORT).show();

        //change color of that particular object with generated random ID in listview
    }
}

MainActivity.java

package com.app.prateek.listviewbroadcastreceiver;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

    ListView listView;
    IdAdapter idAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listView);
        idAdapter=new IdAdapter(getApplicationContext(),R.layout.row_layout);
        listView.setAdapter(idAdapter);
        for(int i=0;i<10;i++)
        {
            idAdapter.add(new Id(i));
        }
    }
}

IdAdapter.java

package com.app.prateek.listviewbroadcastreceiver;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;



public class IdAdapter extends ArrayAdapter {
    List list = new ArrayList();

    public IdAdapter(Context context, int resource) {
        super(context, resource);
    }

    @Override
    public void add(Object object) {
        super.add(object);
        list.add(object);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Nullable
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row;
        row = convertView;
        DataHandlerRow dataHandlerRow;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.row_layout, parent, false);
            dataHandlerRow = new DataHandlerRow();
            dataHandlerRow.editTextId = (EditText) row.findViewById(R.id.editTextId);
            row.setTag(dataHandlerRow);
        } else {

            dataHandlerRow = (DataHandlerRow) row.getTag();

        }
        Id id = (Id) this.getItem(position);
        dataHandlerRow.editTextId.setText(id.getId() + "");
        return row;
    }

    static class DataHandlerRow {
        EditText editTextId;
    }
}

Id.java

public class Id {
    private int id;

    public Id(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

row_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/editTextId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:textColor="@color/colorAccent"
        android:layout_centerHorizontal="true"
        android:textSize="20dp"/>

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

创建一个新的适配器并使用侦听器回调模式在列表视图中设置它

答案 1 :(得分:-1)

对于初学者,如果你想使用普通的适配器,你应该实现顾客观点持有人View holder pattern

要做你想做的事,你应该简单地在适配器中实现颜色,与实现的对象的属性相同。

Ejample(颜色可以作为字符串或app资源传递)

public class Id {
    private int id;
    private String  color;

    public setColor(String color)
    {
    this.color= color;
    }
    public getColor()
    {
     return color;
    }
    public Id(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

拥有该属性后,您只需从列表中选择适配器的相应元素,输入所需的颜色并通知您对适配器的更改

示例

((Iadapter)listview.getAdapter()).list.get(position_random).setColor("#ffa500");
((Iadapter)listview.getAdapter()).notifydatasetchanged();

在适配器中

 dataHandlerRow.editTextId.setTextColor(Color.parseColor(id.getColor()));

这将为您服务,我希望它有所帮助

问候