mercredi 12 avril 2017

Change color of an random item in ListView when connectivity change is detected

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://ift.tt/nIICcg"
    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://ift.tt/nIICcg"
        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>

Aucun commentaire:

Enregistrer un commentaire