Search code examples
androidandroid-layoutandroid-listviewandroid-image

change image on table list view android


i want to change particular image on table list view, when user click on any image view of list view then dialog open and if user click on OK button OK Dialog then particular image should be changed. i have created code but it not working properly.

public void Display_Description(String get_Desc) {



    setDESCRIPTION = get_Desc;

    TableLayout tbl = new TableLayout(Job_Description.this);
    mylist = new ArrayList<String>();

    Document doc = XMLfunctions.XMLfromString(JobSearch.line);

    // Parent node is JOB
    NodeList nodes = doc.getElementsByTagName("JOB");

    for (int i = 0; i < nodes.getLength(); i++) {
        Element e = (Element) nodes.item(i);
        pass_value.add(XMLfunctions.getValue(e, "id"));

        if (!("null").equals(XMLfunctions.getValue(e, "location"))) {
            mylist.add(XMLfunctions.getValue(e, "location"));
            city_name.add(XMLfunctions.getValue(e, "location"));


        } else {
            mylist.add(" ");
        }
        if (!("null").equals(XMLfunctions.getValue(e, "title"))) {
            mylist.add(XMLfunctions.getValue(e, "title"));
            business_name.add(XMLfunctions.getValue(e, "title"));



        } else {
            mylist.add(" ");
        }
        if (!("null").equals(XMLfunctions.getValue(e, "state"))) {
            mylist.add(XMLfunctions.getValue(e, "state"));
            state_name.add(XMLfunctions.getValue(e, "state"));



        } else {
            mylist.add(" ");
        }

        if (!("null").equals(XMLfunctions.getValue(e, "company"))) {
            mylist.add(XMLfunctions.getValue(e, "company"));
            company_name.add(XMLfunctions.getValue(e, "company"));


        } else {
            mylist.add(" ");
        }
        TableRow tr = new TableRow(this);
        tr.setId(i + 1);
        tr.setOnClickListener(this);
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        TableRow.LayoutParams tr_params = new TableRow.LayoutParams(
                LayoutParams.FILL_PARENT, 155);
        ll.setLayoutParams(tr_params);
        ll.setBackgroundResource(R.drawable.job_match_bg);

        TextView tvBusiness = new TextView(this);
        TextView tvLocation = new TextView(this);
        TextView tvState = new TextView(this);
        TextView tvCompany = new TextView(this);
        img = new ImageView(this);
        TextView tvEmptyText = new TextView(this);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                1000, 1);
        tvEmptyText.setLayoutParams(params);
        //tvEmptyText.setBackgroundResource(R.drawable.line);


        img.setLayoutParams(new TableRow.LayoutParams(28,28));
        //img.setPadding(left, top, right, bottom);
        //img.setId(i*i+2);
        img.setBackgroundResource(R.drawable.starapplied);
        tvCompany.setText("Company:  "
                + XMLfunctions.getValue(e, "company"));
        tvCompany.setTypeface(null, Typeface.BOLD);
        tvCompany.setTextColor(Color.BLACK);
        tvCompany.setPadding(5, 5, 5, 5);

        tvBusiness.setText("Business : " +XMLfunctions.getValue(e, "title"));
        tvBusiness.setTypeface(null, Typeface.BOLD);
        tvBusiness.setTypeface(null, Typeface.BOLD);
        tvBusiness.setTextColor(Color.BLACK);

        tvBusiness.setPadding(5, 5, 5, 5);


        tvState.setText("State:  " + XMLfunctions.getValue(e, "state"));
        tvState.setTypeface(null, Typeface.BOLD);
        tvState.setTextColor(Color.BLACK);

        tvState.setPadding(5, 5, 5, 5);

        tvLocation
                .setText("City:  " + XMLfunctions.getValue(e, "location"));
        tvLocation.setTypeface(null, Typeface.BOLD);
        tvLocation.setTextColor(Color.BLACK);
        tvLocation.setPadding(5, 5, 5, 5);

        img.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
             try
             {
                  AlertDialog.Builder builder = new AlertDialog.Builder(Job_Description.this);
                        builder.setTitle("Save this Job ?");
                       // builder.setMessage("whould You like to save job");
                        builder.setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog, int which) {
                            // startActivity(new Intent(Job_Description.this,JobSearch.class));
                                img.setBackgroundResource(R.drawable.save);

                             Toast.makeText(getApplicationContext(),"Button Clicked",Toast.LENGTH_LONG).show();
                          Log.e("info", "OK");
                         }
                        });
                        builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog, int whichButton) {
                                // Canceled.
                                  dialog.cancel();
                              }
                            });
                        builder.show();
             }catch(Exception e)
             {
                 Log.d("Image clicked", e.toString());
             }
            }
        });

        ll.addView(tvCompany);
        ll.addView(tvBusiness);
        ll.addView(img);
        ll.addView(tvState);
        ll.addView(tvLocation);
        ll.addView(tvEmptyText);
       // img.setOnClickListener(this);
        tr.addView(ll);
        tbl.addView(tr);
        // saved job click listener


    }
    mylist = null;
    doc = null;
    nodes = null;
    scv.addView(tbl);


}

Sorry for Bad english.......


Solution

  • you can directly setimage like

    img.setImageResource(R.drawable.apple);