根据TextVIew文本更改LinerLayour颜色

时间:2019-07-10 07:49:00

标签: android listview

public class ListItem extends AppCompatActivity {
    ListView listView;
    ListAdapter adapter;
    ProgressDialog loading;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_item);
        listView = (ListView) findViewById(R.id.lv_items);

        getItems();

    }


    private void getItems() {

        loading =  ProgressDialog.show(this,"Loading","please wait",false,true);

        StringRequest stringRequest = new StringRequest(Request.Method.GET, "GooglSheet URL",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        parseItems(response);
                    }
                },

                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }
        );

        int socketTimeOut = 50000;
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeOut, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);

        stringRequest.setRetryPolicy(policy);

        RequestQueue queue = Volley.newRequestQueue(this);
        queue.add(stringRequest);

    }


    private void parseItems(String jsonResposnce) {

        ArrayList<HashMap<String, String>> list = new ArrayList<>();

        try {
            JSONObject jobj = new JSONObject(jsonResposnce);
            JSONArray jarray = jobj.getJSONArray("items");


            for (int i = 0; i < jarray.length(); i++) {

                JSONObject jo = jarray.getJSONObject(i);

       String scriptName = jo.getString("scriptName");
                String breakoutTimeUnformated = jo.getString("breakoutTime");
                String breakoutTime=breakoutTimeUnformated.replace("+0530", "").replace("T", " ");
                String TypeOfCall = jo.getString("TypeOfCall");
                double TriggerPrice = jo.getDouble("TriggerPrice");
                double StopLoss = jo.getDouble("StopLoss");
                double TargetOne = jo.getDouble("TargetOne");
                double TargetTwo = jo.getDouble("TargetTwo");
                String rangeTarget="("+TargetOne+">-<"+TargetTwo+")";
                double ReverseTrade = jo.getDouble("ReverseTrade");




                HashMap<String, String> item = new HashMap<>();
                item.put("breakoutTime",breakoutTime);
                item.put("scriptName", scriptName);
                item.put("TriggerPrice",TypeOfCall+" @\n"+TriggerPrice);
                item.put("StopLoss","SL @\n"+StopLoss);
                item.put("rangeTarget","Target\n"+rangeTarget);
                item.put("ReverseTrade","Reverse\n"+ReverseTrade);


                list.add(item);
                Collections.reverse(list);



            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        adapter = new SimpleAdapter(this,list,R.layout.list_item_row,
                new String[]{"scriptName","breakoutTime","TriggerPrice","StopLoss",
                        "rangeTarget","ReverseTrade"},
                new int[]{R.id.tv_scriptName,R.id.tv_breakoutTime,R.id.tv_TriggerPrice,R.id.tv_StopLoss,R.id.tv_target,R.id.tv_reverse});

        listView.setAdapter(adapter);
        loading.dismiss();
    }

}


    <?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="horizontal"
    android:background="@color/colorPrimary"
    android:id="@+id/resv">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="wrap_content"
        android:id="@+id/l">

        <TextView
            android:id="@+id/tv_breakoutTime"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:text="ForDate"
            android:gravity="center"
            android:layout_weight="1"
            android:ellipsize="end"
            android:maxLines="1"
            android:textSize="15sp"
            android:letterSpacing="0.2"
            android:padding="2sp"
            android:textColor="@android:color/white"
            />

        <TextView
            android:id="@+id/tv_scriptName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:text="Script Name"
            android:gravity="center"
            android:layout_weight="1"
            android:ellipsize="end"
            android:maxLines="1"
            android:textSize="15sp"
            android:padding="2sp"

            android:letterSpacing="0.2"
            android:textColor="@android:color/white"
            android:layout_below="@+id/tv_breakoutTime"

            />


        <TextView
            android:id="@+id/tv_TypeOfCall"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:text="Call Type"
            android:gravity="center"
            android:layout_weight="1"
            android:ellipsize="end"
            android:letterSpacing="0.2"
            android:maxLines="3"
            android:padding="2sp"

            android:textSize="15sp"
            android:textColor="@android:color/white"
            android:layout_below="@+id/tv_scriptName"

            />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content"
        android:layout_below="@+id/l"
        android:id="@+id/l1">



        <TextView
            android:gravity="center"
            android:id="@+id/tv_TriggerPrice"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:text="TriggerPrice"
            android:layout_weight="1"
            android:ellipsize="end"
            android:maxLines="5"
            android:padding="2sp"

            android:textSize="15sp"
            android:textColor="@android:color/white"
            android:letterSpacing="0.2"

            />

        <TextView
            android:gravity="center"
            android:id="@+id/tv_StopLoss"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:text="SL"
            android:layout_weight="1"
            android:ellipsize="end"
            android:maxLines="5"
            android:padding="2sp"

            android:textSize="15sp"
            android:textColor="@android:color/white"
            android:letterSpacing="0.2"


            />



    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content"
        android:layout_below="@+id/l1"
        android:id="@+id/l2">


        <TextView
            android:gravity="center"
            android:id="@+id/tv_target"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:text="Target"
            android:layout_weight="1"
            android:padding="2sp"

            android:ellipsize="end"
            android:maxLines="5"
            android:textSize="15sp"
            android:textColor="@android:color/white"
            android:letterSpacing="0.2"
            />

        <TextView
            android:gravity="center"
            android:id="@+id/tv_reverse"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:text="Reverse"
            android:layout_weight="1"
            android:ellipsize="end"
            android:padding="2sp"
            android:maxLines="5"
            android:textSize="15sp"
            android:textColor="@android:color/white"
            android:letterSpacing="0.2"
            />




    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="@color/colorSell"
        android:layout_below="@+id/l2"/>


        />
</RelativeLayout>

如何根据从TypeOfCall textView获得的文本来更改linearLayout的颜色(如果包含BUY,则如果出售,则其他颜色,然后其他?

我尝试了各种代码结构,但是没有有效的解决方案。

它使用ListView进行AppCompatActivity活动,并使用简单的适配器提取从Google表格获取的数据。

3 个答案:

答案 0 :(得分:0)

您需要定义样式表XML。这是一个示例,它应该位于项目中的res / drawable文件夹中

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
   <solid android:color="#ffffff" />
   <stroke android:width="1dip" android:color="#4fa5d5" />
   <padding android:right="6dp" android:left="6dp" android:bottom="3dp" android:top="3dp" />
   <gradient
            android:endColor="#fa7921"
            android:startColor="#fa7921"
            android:angle="270" />
</shape>

并添加一些逻辑

 if(((TextView) appView.findViewById(R.id.TextViewToCheck)).getText().equals("SELL"))
 {
 appView.findViewById(R.id.l).setBackgroundResource(R.drawable.layout_Sell)
 }
 else 
 appView.findViewById(R.id.l).setBackgroundResource(R.drawable.layout_Buy)

答案 1 :(得分:0)

您将需要为列表视图创建一个自定义适配器。

here is an example

答案 2 :(得分:0)

尝试扩展适配器,所以更换:

        adapter = new SimpleAdapter(this,list,R.layout.list_item_row,
            new String[]{"scriptName","breakoutTime","TriggerPrice","StopLoss",
                    "rangeTarget","ReverseTrade"},
            new int[]{R.id.tv_scriptName,R.id.tv_breakoutTime,R.id.tv_TriggerPrice,R.id.tv_StopLoss,R.id.tv_target,R.id.tv_reverse});

具有:

        adapter = new SimpleAdapter(this,list,R.layout.list_item_row,
            new String[]{"scriptName","breakoutTime","TriggerPrice","StopLoss",
                    "rangeTarget","ReverseTrade"},
            new int[]{R.id.tv_scriptName,R.id.tv_breakoutTime,R.id.tv_TriggerPrice,R.id.tv_StopLoss,R.id.tv_target,R.id.tv_reverse}){
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            LinearLayout linearLayout = view.findViewById(R.id.l);
            TextView textView = view.findViewById(R.id.tv_TypeOfCall);
            String txt = textView.getText().toString();
            if(txt.equals("SELL")){
                linearLayout.setBackgroundColor(Color.GREEN);
            }else{
                linearLayout.setBackgroundColor(Color.BLUE);
            }
            return view;
        }
    };

希望有帮助!