设置可见性不起作用

时间:2013-05-24 14:42:03

标签: android listview

在我的应用程序中,我有一个listView,我想过滤它。

列表页面以变量filter.equalsIgnoreCase(“”)开头,因此列表显示所有内容,如果!filter.equalsIgnoreCase(“”)列表被过滤。

过滤器页面是一个过滤器列表,并选择一个,它是ImageView的setVisibility(View.VISIBLE)并设置变量filter =“something”。

我确认按钮“DONE”返回列表视图页面。

当我返回过滤器时,我必须保留所选过滤器的标志,但setVisibility(View.VISIBLE)不起作用。

我认为问题与list_row.xml中的事实有关,我将imageView的可见性设置为不可见,但是在onCreate方法内的过滤页面中我

以下是用于弹出过滤器列表的代码:

private void popolaLaLista(final ListView listView1, final String table, Cursor cursor) {

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

    if (cursor.moveToFirst()) {
        do {
            int index = cursor.getColumnIndex("risk");

            String risk = cursor.getString(index);

            String[] tokens = risk.split("- ");

            HashMap<String, String> agentMulti = new HashMap<String, String>();
            agentMulti.put("Risk", tokens[0]);
            agentMulti.put("Explanation", tokens[1]);

            agentsMulti.add(agentMulti);

        } while (cursor.moveToNext());
    }

    String[] keys = { "Risk", "Explanation"};
    int[] ids = {R.id.text1,R.id.text2};
    SimpleAdapter adapterMulti = new SimpleAdapter(this, agentsMulti, R.layout.list_row, keys, ids);

    final ListView listView= (ListView)findViewById(R.id.listView1);

    listView.setAdapter(adapterMulti);

    listView.setClickable(true);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, View arg1, int position, long arg3) {

          ImageView flag = (ImageView) arg1.findViewById(R.id.flag_id);

            if(flag.getVisibility()==View.INVISIBLE){

                for(int i =0; i<parent.getChildCount(); i++ ){

                    ImageView childFlag = (ImageView) parent.getChildAt(i).findViewById(R.id.flag_id);

                    childFlag.setVisibility(View.INVISIBLE);
                }

            flag.setVisibility(View.VISIBLE);

            TextView risk = (TextView) arg1.findViewById(R.id.text1);

            filter = risk.getText().toString().trim();

            }else{

                flag.setVisibility(View.INVISIBLE);
                filter = "";
            }

      }
    });

}

以下是检查我之前选择的过滤器的代码:

private void setFlag(String filter2) {
    ListView lista = (ListView) findViewById(R.id.listView1);

    Adapter adapter = lista.getAdapter();

    int adapterCount = adapter.getCount();

    for (int i = 0; i < adapterCount; i++)
    {

        TextView tv = (TextView) adapter.getView(i, null, null).findViewById(R.id.text1);

        String tvValue = tv.getText().toString().trim();
        Log.i("tv " + i + ", value", tvValue);

        if(tvValue.equalsIgnoreCase(filter2)){

            ImageView iv = (ImageView) adapter.getView(i, null, null).findViewById(R.id.flag_id);

            iv.setVisibility(View.VISIBLE);
        }
    }
}

这是list_row.xml的代码,filterView的单项的布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/id_list_row"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="TITOLO"
        android:textColor="#040404"
        android:textSize="15dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/text1"
        android:layout_marginTop="1dp"
        android:text="sottotitolo"
        android:textColor="#343434"
        android:paddingRight="10dp"
        android:textSize="12dip" />


    <ImageView
        android:id="@+id/flag_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/text2"
        android:visibility="invisible"
        android:src="@drawable/checkmark_filter"
        android:layout_centerVertical="true"/>


</RelativeLayout>

0 个答案:

没有答案