Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从主活动更改ListView字段属性_Android_Listview_Android Listview_Baseadapter - Fatal编程技术网

Android 从主活动更改ListView字段属性

Android 从主活动更改ListView字段属性,android,listview,android-listview,baseadapter,Android,Listview,Android Listview,Baseadapter,我正在开发一个Android应用程序。在我的主要活动中,我必须实现一个列表。下面是我页面的示例形状 |----------------------| \ | |Button| | \ |----------------------| \ |listview row1 | \ \ |listview row1 | \ \---------Screen |listview row1

我正在开发一个Android应用程序。在我的主要活动中,我必须实现一个列表。下面是我页面的示例形状

|----------------------| \
|  |Button|            |  \
|----------------------|   \                  
|listview row1         | \   \
|listview row1         |  \   \---------Screen
|listview row1         | / --/----- ListView 
|                      |/   /
|                      |  /
|                      | /
|______________________|/

该按钮位于我的“活动”页面中,listview行正在baseadapter中创建。listview包含一个textview。现在,当我从“活动”中单击该按钮时,我必须更改textview背景颜色,下次单击该按钮时,textview颜色将保留旧颜色。我如何才能做到?。我在getview()方法中声明了textview。

可能还有其他方法,但我会在OnClick方法中循环浏览按钮的列表行。比如:

在“活动”字段定义中:

    static final int colourA=Color.argb(255,255,0,0);
    static final int colourB=Color.argb(255,0,255,0);
    int currentColour=colourA;
在活动OnCreate中:

        Button myButton = (Button) findViewById(R.id.myButton); 
        final ListView myListView = (ListView) findViewByID(R.id.myListView);
        //change myButton to your button id, and myListView to your ListView id
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //This is the code to toggle the colours, you can do pretty much whatever you want here though
                if (currentColour==colourA){
                    currentColour=colourB;
                } else {
                    currentColour=colourA;
                }

                //This cycles through all the root views in the ListView. If you want to change the
                //colour of only one view in the row layout, in the for loop use 
                //rowView.findViewById(R.id.myViewInRow).setBackgroundColor(currentColour);
                //instead, to get the relevant view in the row
                View rowView;
                for (int i=0;i<myListView.getChildCount();i++){
                    rowView=myListView.getChildAt(i);
                    rowView.setBackgroundColor(currentColour);
                }
            }
        });
Button myButton=(Button)findviewbyd(R.id.myButton);
最终ListView myListView=(ListView)findViewByID(R.id.myListView);
//将myButton更改为按钮id,将myListView更改为ListView id
setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
//这是切换颜色的代码,你可以在这里做任何你想做的事情
如果(CurrentColor==colorA){
currentColour=colorB;
}否则{
currentColor=colorA;
}
//这将循环浏览ListView中的所有根视图
//行布局中只有一个视图的颜色,供循环使用
//rowView.findViewById(R.id.myViewInRow).setBackgroundColor(CurrentColor);
//而是获取行中的相关视图
视图行视图;

对于(int i=0;i我终于得到了解决方案。这可能对其他人有帮助。但我并不关心我的代码质量

步骤1)

我在我的活动中加入了变量

static int hidestate=0;
在单击隐藏按钮的方法中,我写下了这个

hide_btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if (hidestate==0) {

                hidestate=1;

                sMSConversationAdapter.notifyDataSetChanged();
                hide_btn.setText("Show All");

            }else {
                hidestate=0;

                sMSConversationAdapter.notifyDataSetChanged();
                hide_btn.setText("Hide All");
            }

        }
    });
步骤2) 下面是我的BaseAdapter类getView()


感谢朋友们的帮助。

是否要更改所有textview背景颜色?@skyrift..我使用的是自定义基本适配器。如果您只想更改行的背景颜色,则无论使用的适配器是什么,此操作都应该有效-它在该级别上运行,并且具有ListView本身。尽管从解决方案中,您可以下面的ted看起来你想隐藏布局的一部分,而不是改变它的颜色。
public View getView(final int position, View convertView, ViewGroup parent) {

    View vi=convertView;
    final TextView message;

    if(convertView==null)
        vi = inflater.inflate(R.layout.smsconversation_row, null);

    RelativeLayout nws=(RelativeLayout)vi.findViewById(R.id.all);
    message=(TextView)vi.findViewById(R.id.snt_txt);

    if (SMSConversationHome.hidestate==1) {
        message.setVisibility(View.INVISIBLE);

    }
    else{

        message.setVisibility(View.VISIBLE);

    }
}