尝试在我的 ListView 中显示带有 toast 功能的按钮

try to display button with toast function in my ListView

如何以及在何处添加 button with Toast function 在我的 application.

我知道这个问题被问了很多次,但我仍然很困惑,所以谁能帮忙。

[文字]

[按钮 1][按钮 2]

[文字]

[按钮 1][按钮 2]

这是我的代码:

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 );

        }
    }
    }
}

我可以显示 listview 中的项目,但现在我想向其中添加 button

每当我点击它时也会弹出 toast 消息。

这是XML文件

<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>

欢迎使用 Whosebug。

了解您的需求后。这是解决方案。

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 与您的问题有关。不同之处在于 - 在这个答案中,这个人将 CheckBox 附加到模型。您将为模式附加可见性。

建议

使用RecyclerView,因为考虑到特性,它有很多性能。

喜欢notifyItemDataChanged()只通知被改变的项目,是否在ListView中通知所有项目。

添加这个方法

 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();
                }
            }


        }
    });

在适配器代码之后。

lv.setadapter(adpater);