尝试在我的列表视图中显示带有 Toast 功能的按钮



如何以及在哪里添加buttonToastfunction在我的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>

添加此方法

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

欢迎来到Stackoverflow。

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

首先在模型类中获取一个布尔值。

class Model {
boolean buttonVisible;
// settter getter
}

现在将此布尔值附加到适配器中的按钮可见性getView().

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

现在,当您要更改可见的按钮时。只需更改此布尔值 并通知数据更改列表。

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

这将使 2 索引按钮可见。如果你感到困惑 任何地方,请问我。

这个答案与您的问题有关。只是不同的是 - 在这个答案中,这家伙将复选框附加到模型。您将为模式附加可见性。

建议

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

就像notifyItemDataChanged()只通知已更改的项目一样,是否在 ListView 中通知所有项目都会被通知。

最新更新