我在ListView项目中单击按钮时遇到一个问题

时间:2017-08-21 05:20:12

标签: android

我有一个由{Shortlist'按钮组成的ListView,如果有人点击该按钮,则应更改按钮文本。 按钮Shortlist通货膨胀,其中shortlist [position] .toString()值来自服务器,如果按钮文本被列入候选名单,则它将显示在按钮上,按钮将不会被按下。如果按钮文本是候选名单,那么它将在按钮上显示为候选名单并按下按钮。

以下是我的代码。

public class Profile_Match_custom_List  extends ArrayAdapter<String>  {
private static final String url ="http://10.0.2.2/xp/shortlist1.php";
private static final String url1 = "http://10.0.2.2/xp/express_intrest.php";
private static final String KEY_MATRI_ID_TO="matriID_to";
private static final String KEY_MATRI_ID_BY="matriID_by";

SessionManager session;
public String matri_id_to, matri_id_by, str_gender;

String str;

public Button btnSort;
private NetworkImageView imageView;
private ImageLoader imageLoader;
private final String[] ids;
private String[] ages;
private String[] heights;
public String[] communities;
public String[] castes;
public String[] educations;
public String[] occupations;
public String[] incomes;
public String[] pics;
public String[] locations;
public String[] shortlist;
public String[] expressinterest;
private Activity context;


public Profile_Match_custom_List(Activity context, String[] ids, String[] ages,
                                 String[] heights, String[] communities, String[] castes,
                                 String[] educations, String[] occupations, String[]incomes,
                                 String[]pics, String[] locations, String[] shortlist, String[] expressinterest) {
    super(context, R.layout.custom_profile_match,ids);
    this.ids = ids;
    this.ages = ages;
    this.heights = heights;
    this.communities = communities;
    this.castes = castes;
    this.educations = educations;
    this.occupations = occupations;
    this.incomes = incomes;
    this.pics = pics;
    this.locations = locations;
    this.shortlist = shortlist;
    this.expressinterest = expressinterest;
    this.context = context;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    final View listViewItem = inflater.inflate(R.layout.custom_profile_match, null, true);

    // Session class instance
    session = new SessionManager(getContext());
    session.checkLogin();

    // get user data from session
    HashMap<String, String> user = session.getUserDetails();
    matri_id_by = user.get(SessionManager.KEY_EMAIL);
    str_gender = user.get(SessionManager.KEY_GENDER);

    String url1 = "https://www.maangal.com/thumb/thumb_";
    String url = url1 + pics[position];

    imageView = (NetworkImageView) listViewItem.findViewById(R.id.offer_image);
    imageLoader = CustomVolleyRequest.getInstance(this.getContext()).getImageLoader();
    if(str_gender.equals("Male")) {
        imageLoader.get(url, ImageLoader.getImageListener(imageView, R.drawable.image, R.drawable.girl));
    }
    else {
        imageLoader.get(url, ImageLoader.getImageListener(imageView, R.drawable.image, R.drawable.boy));
    }
    imageView.setImageUrl(url,imageLoader);
    TextView textViewId = (TextView) listViewItem.findViewById(R.id.textViewId);
    TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName);

    textViewId.setText(ids[position]);
    textViewName.setText( ages[position]+" years"+" , "+heights[position]+" cm"+", "+communities[position]+" : "+castes[position]+" , "+educations[position]+" , "+occupations[position]+" , "+incomes[position]+", "+locations[position]);


    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(v.getContext(), BlankActivity.class);
            Toast.makeText(getContext(), ids[position], Toast.LENGTH_LONG).show();
            i.putExtra("id", ids[position]);
            v.getContext().startActivity(i);
        }
    });

    /*Button Shortlist inflation, where the shortlist[position].toString() 
    value comes from server and if the button text is shortlisted then it 
    will shown on button and button will not pressed. If the button text is 
    shortlist then it will show on button as shortlist and button should be 
    pressed.*/  


    btnSort =(Button) listViewItem.findViewById(R.id.btnshort);
    str = shortlist[position].toString();
    Log.e("------shortlisted------",str);
    if(str.equalsIgnoreCase("shortlisted")) {
        btnSort.setText(str);
        btnSort.setBackgroundColor(Color.parseColor("#FF307668"));
        btnSort.setEnabled(false);
  }
   else
   {
       btnSort.setText(str);
  }
    btnSort.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            matri_id_to=ids[position];
            shortlist();
        }
    });




    Button btnChat =(Button) listViewItem.findViewById(R.id.btnchat);
    btnChat.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            matri_id_to=ids[position];
            Toast.makeText(getContext(),"sent express intrest",Toast.LENGTH_LONG).show();
            express_Intrest();
        }
    });

    Button declineButton = (Button)listViewItem.findViewById(R.id.declineButton);
    declineButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        }
    });
    return listViewItem;
}

  //shortlist method.
 private void shortlist(){
    StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>     () {
        @Override
        public void onResponse(String response) {
            Log.e("***short response----",response.trim());
            if(response.trim().equalsIgnoreCase("success")) {
                btnSort.setBackgroundColor(Color.parseColor("#FF307668"));
                btnSort.setText("shortlisted");
                Toast.makeText(getContext(), "shortlisted", Toast.LENGTH_LONG).show();
                btnSort.setEnabled(false);

            }
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getContext(), error.toString(), Toast.LENGTH_LONG).show();
                }
            }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put(KEY_MATRI_ID_BY,matri_id_by);
            params.put(KEY_MATRI_ID_TO,matri_id_to);
            return params;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(getContext());
    requestQueue.add(stringRequest);
}

public void express_Intrest(){
    StringRequest stringRequest1 = new StringRequest(Request.Method.POST, url1, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            Log.e("response",response);
            Toast.makeText(getContext(), response, Toast.LENGTH_LONG).show();
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getContext(), error.toString(), Toast.LENGTH_LONG).show();
                }
            }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();

            params.put(KEY_MATRI_ID_BY,matri_id_by);
            params.put(KEY_MATRI_ID_TO,matri_id_to);

            return params;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(getContext());
    requestQueue.add(stringRequest1);
  }
 }

1 个答案:

答案 0 :(得分:1)

您没有刷新列表数据。保存点击已完成的位置,然后根据此位置获取或放置数据。

btnSort.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        matri_id_to=ids[position]; //You can totally discard this line as you can get this value directly on getParams from selectedPosition.
        selectedPosition = position;
        shortlist();
    }
});

//短名单方法。

private void shortlist(){
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>     () {
    @Override
    public void onResponse(String response) {
        Log.e("***short response----",response.trim());
        if(response.trim().equalsIgnoreCase("success")) {

            shortlist[selectedPosition] = "shortlisted";
            notifyDataSetChanged(); //this will refresh your list and your code for disabling the button will work
        }
    }
},
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getContext(), error.toString(), Toast.LENGTH_LONG).show();
            }
        }) {
    @Override
    protected Map<String, String> getParams() {
        Map<String, String> params = new HashMap<String, String>();
        params.put(KEY_MATRI_ID_BY,matri_id_by);
        params.put(KEY_MATRI_ID_TO,matri_id_to);
        return params;
    }
};
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(stringRequest);
}

执行shortlist[selectedPosition] = "shortlisted"并调用notifyDataSetChanged()后,您的适配器将刷新并调用getView。您设置btnSort的if..else代码将起作用,并将在候选名单数组中找到selectedPosition作为&#34;列入短名单&#34;并将产生你想要的输出。