0

How to and where to add button with Toast function in my application.

I know many time this question is asked but still, I am confused so can anyone help.

[Text]

[Button 1] [Button 2]

[Text]

[Button 1][Button 2]

Here is my code :

public class EngineerRecycler extends AppCompatActivity {
String service_id,type, jsonStr;
String compticketid;
private String TAG = MainActivity.class.getSimpleName();
private ListView lv;

// URL to get contacts JSON
private static String urlpending = "http://localhost/players.php";

ArrayList<HashMap<String, String>> contactList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_engineer_recycler );
    Bundle bundle = getIntent().getExtras();
    service_id = bundle.getString( "empid" );
    type = bundle.getString( "type" );
    contactList = new ArrayList<>();
    lv = (ListView) findViewById(R.id.list);
    new GetContacts().execute();
}

private class GetContacts extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected Void doInBackground(Void... arg0) {
        HttpHandler sh = new HttpHandler();

        // Making a request to URL and getting a response

            jsonStr = sh.makeServiceCall( urlpending, service_id );

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                JSONArray contacts = jsonObj.getJSONArray("contacts");

                // looping through All Contacts
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);

                    String name = c.getString("ClientName");
                    String email = c.getString("comp_desc");


                    // tmp hash map for single contact
                    HashMap<String, String> contact = new HashMap<>();

                    // adding each child node to HashMap key => value
                    contact.put("ClientName", name);
                    contact.put("comp_desc", email);

                    // adding contact to contact list
                    contactList.add(contact);
                }
            } catch (final JSONException e) {
                Log.e(TAG, "Json parsing error: " + e.getMessage());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Json parsing error: " + e.getMessage(),
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });

            }
        } else {
            Log.e(TAG, "Couldn't get json from server.");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Couldn't get json from server. Check LogCat for possible errors!",
                            Toast.LENGTH_LONG)
                            .show();
                }
            });

        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);



            ListAdapter adapter = new SimpleAdapter(
                    EngineerRecycler.this, contactList,
                    R.layout.list_pending, new String[]{"ClientName", "comp_desc"}, new int[]{R.id.name,
                    R.id.email);

            lv.setAdapter( adapter );

        }
    }
    }
}

I am able to display the items in listview but when now I want to add button to it.

that too whenever I click on it toast message should pop.

Here is XML file

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
    android:id="@+id/name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
  <LinearLayout
    android:layout_width="351dp"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <Button
        android:id="@+id/getenter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Reached" />
</LinearLayout>
nikhil lohar
  • 139
  • 3
  • 14

2 Answers2

0

welcome to Stackoverflow.

After knowing about your requirement. Here is the solution.

First take a boolean in your model class.

class Model {
  boolean buttonVisible;
  // settter getter
}

Now attach this boolean value to your button visibility in your adapter getView().

holder.button.setVisibility(model.isButtonVisible() ? View.VISIBLE : View.GONE);

Now when you want to change a button visible. Just change this boolean and notify list for data change.

yourList.get(2).setButtonVisible(true);
((BaseAdapter) listView.getAdapter()).notifyDataSetChanged(); 

This will make the 2 index button visible. If you are confused anywhere, please ask me.

This answer is related to your question. Just the difference is- In this answer this guy is attaching CheckBox to Model. And you will attach visibility to Mode.

Suggestion

Use RecyclerView, because that has lot of performance considering feature.

Like notifyItemDataChanged() to notify only item which is changed, whether in ListView all the items would be nofified.

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
0

Add this method

 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            //You can find your button here
           if(view instanceof Button)
            {
                if(view.getId() == R.id.getenter)
                {
                    Toast.makeText(getApplicationContext(),"toast_msg",Toast.LENGTH_LONG).show();
                }
            }


        }
    });

after adapter code.

lv.setadapter(adpater);
Mohamed Mohaideen AH
  • 2,527
  • 1
  • 16
  • 24