列表视图行上的隐藏/显示按钮单击无法正常工作

时间:2015-09-10 06:34:07

标签: android listview button

我想在第一次列表视图行上显示一个按钮,并在第二次点击时隐藏相同的内容。第一次点击时可以看到该按钮,但第二次点击时不会看不见。我尝试了两种方法,因为它们都有相同的问题。一个是评论。

   public class ContactsFragment extends Fragment {
   private ProgressDialog pDialog;
   List<HashMap<String, String>> fetch2 = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser object
JSONParser jsonParser = new JSONParser();



ArrayList<HashMap<String, String>> inboxList;

// products JSONArray
JSONArray inbox = null;

// Inbox JSON url
private static final String INBOX_URL = "http://api.androidhive.info/mail/inbox.json";

// ALL JSON node names
private static final String TAG_MESSAGES = "messages";
private static final String TAG_ID = "id";
private static final String TAG_FROM = "from";
private static final String TAG_EMAIL = "email";
private static final String TAG_SUBJECT = "subject";
private static final String TAG_DATE = "date";
private static final String BU_STRING = "date";

ListView lv;
private Boolean shouldVisible=false;
Button  button1 ;
public static final String TAG = LibraryPagerAdapter.class.getSimpleName();
protected JSONArray mTasksData; 


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.inbox_list, container, false);
    System.out.println("inside on create view");
    lv = (ListView)rootView.findViewById(R.id.list);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
            System.out.println("I clicked row item");
            button1= (Button)v.findViewById(R.id.button1);
            button1.setVisibility(button1.isShown() ? View.GONE : View.VISIBLE);
            /*if(!shouldVisible)//IF IT IS`          invISIBLE
            {
                System.out.println("value is"+shouldVisible);
                button1.setVisibility(View.VISIBLE);
                shouldVisible = false;
            }
            else //IF IT IS VISIBLE
            {
                System.out.println(shouldVisible);
                button1.setVisibility(View.INVISIBLE);
                shouldVisible = true;
            }
            Toast.makeText(getActivity(), "YOU CLICKED ITEM "+pos,Toast.LENGTH_SHORT).show();*/
        }
    });

    bindListView();

    return rootView;

}

public void bindListView() {
    new LoadInbox().execute();
}


class LoadInbox extends AsyncTask<String, String, String> {



    @SuppressWarnings("static-access")
    /*protected void onPreExecute() {
        getActivity().setProgressBarIndeterminateVisibility(true); 
        ProgressDialog progressDialog = new ProgressDialog(getActivity()); 
        progressDialog.show(getActivity(), "Please wait", " loading...");
    }*/
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        // getting JSON string from URL
        JSONObject json = jsonParser.makeHttpRequest(INBOX_URL, "GET",
                params);

        // Check your log cat for JSON reponse
        Log.d("Inbox JSON: ", json.toString());


        try {
            inbox = json.getJSONArray(TAG_MESSAGES);



            // looping through All messages
            for (int i = 0; i < inbox.length(); i++) {
                System.out.println(inbox.length());
                JSONObject c = inbox.getJSONObject(i);

                // Storing each json item in variable
                String id = c.getString(TAG_ID);
                System.out.println(id);
                String from = c.getString(TAG_FROM);

                String subject = c.getString(TAG_SUBJECT);
                System.out.println(subject);
                String date = c.getString(TAG_DATE);
                System.out.println(date);
                if(subject.length() > 23){
                    subject = subject.substring(0, 22) + "..";
                }
                // creating new HashMap
                HashMap<String, String> libraryInfo = new HashMap<String, String>();
                libraryInfo = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                libraryInfo.put(TAG_ID, id);
                libraryInfo.put(TAG_FROM, from);
                libraryInfo.put(TAG_SUBJECT, subject);
                libraryInfo.put(TAG_DATE, date);

                // adding HashList to ArrayList
                fetch2.add(libraryInfo);
                System.out.println(fetch2.toString());
            }

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

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {

        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                try{
                    // dismiss the dialog after getting all products
                    SimpleAdapter adapter = new SimpleAdapter(getActivity(), fetch2, R.layout.inbox_list_item, new String[] { TAG_FROM, TAG_SUBJECT,BU_STRING },
                            new int[] { R.id.from, R.id.subject, R.id.button1 });   

                    lv.setAdapter(adapter);
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        });
        getActivity().setProgressBarIndeterminateVisibility(false); 
    }

    protected JSONArray doInBackground(Object... params) {
        // TODO Auto-generated method stub
        return null;
    }

inbox.list.xml

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:drawSelectorOnTop="false"
    android:text="contactslist"
    android:textColor="#ffffff"
    />

收件箱列表item.xml

  <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout   xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<!-- From Label -->

<TextView
    android:id="@+id/from"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="4dip"
    android:paddingLeft="8dip"
    android:paddingTop="8dip"
    android:textColor="#ffffff"
    android:textSize="20dip"
    android:textStyle="bold" />

<!-- Mail Subject -->

<TextView
    android:id="@+id/subject"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/from"
    android:paddingBottom="6dip"
    android:paddingLeft="8dip"
    android:textSize="15dip" />

<!-- Mail date -->

    <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:padding="8dip"
    android:textColor="#fff"
    android:text="call"
    android:visibility="invisible"
    android:textSize="13sp" />

    </RelativeLayout>

0 个答案:

没有答案