如何在我的button中添加Toast function并将其添加到何处。
我知道很多时候这个问题被问到了,但我还是很困惑,也有人能帮上忙。
文本
按钮1
文本
按钮1
这是我的代码:
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>发布于 2018-09-10 08:04:59
添加此方法
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);发布于 2018-09-10 07:28:32
欢迎来到斯塔克沃夫。
知道了你的要求之后。这是解决办法。
首先,在模型类中取一个布尔值。
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索引按钮可见。如果你在任何地方感到困惑,请问我。
这个答案与你的问题有关。不同之处在于--在这个答案中,这个家伙正在把CheckBox附加到模型上。你会把能见度附加到模式上。
建议
使用RecyclerView,因为考虑到特性,这具有很大的性能。
与notifyItemDataChanged()一样,只通知已更改的项,是否在ListView中所有的项都将被公示。
https://stackoverflow.com/questions/52252680
复制相似问题