9

I have a broadcast receiver which receives connectivity change broadcast.So it is triggered whenever connectivity change is detected.I have a list view with 10 items.Each item has a EditText which contains the position of that particular item from 0 to 9. When the broadcast is received,a random number is generated from 0 to 9,on basis of random number that is generated,i want to change the color of the text in corresponding textview. How to implement this?

My current code is:

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>
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Prateek Ratnaker
  • 817
  • 11
  • 35

2 Answers2

0

Make a new adapter and set it in your listview using listener callback pattern

Prateek Ratnaker
  • 817
  • 11
  • 35
-1

for starters, if you want to work with a normal adapter, you should implement the patron view holder View holder pattern.

To do what you want you should simply implement the color in your adapter, having the same as property of the object that is implemented.

Ejample (Color can be passed as string or as an app resource)

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

Once you have the property ,you just have to pick up the corresponding element of the adapter from your list, put the color you want and notify you the changes to adapter

example

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

In you adapter

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

This will serve you, I hope it helps

regards

Delari Jesus
  • 411
  • 6
  • 22
  • how to change the color of the field from broadcast receiver? – Prateek Ratnaker Apr 21 '17 at 03:25
  • In your case you would have to create and register the instance of the broadcast in your class or register another receiver in the class you want to change, and in your Onreceiver will have to do a new sendBroadcast (); – Delari Jesus Apr 21 '17 at 14:03
  • The broadcast receivers serve to send internal messages in the applications, from some places of the same to others. Imagine that you have a notification service, which is waiting for a notification, that when arrive, you get updated its main interface, for this you do not need to register a receiver in your activity which wants to update. Therefore this is a common practice. You should register receivers in those places where you receive the warnings. Broadcast receivers are basically for this. – Delari Jesus Apr 22 '17 at 10:19