【发布时间】:2020-06-18 07:56:53
【问题描述】:
您好,在下面的代码中动态实现了自动完成 textview 并在本地保存响应。现在如果正在搜索字母“a”,那么它会显示并将 settext 设置为自动完成 textview。当我选择文本时,settext 工作正常获取 ID
Onitemclick Toast 消息未显示。
谁能帮我解决我的错误
private void fetchProductnameJSON(){
autoproduct_name.setHint(Html.fromHtml ( "Item Name"+""));
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Write code for your refresh logic
sessionId = getActivity().getIntent().getStringExtra("sessionId");
String operation = "syncModuleRecords";
String module = "Products";
String syncToken = "";
String mode = "public";
final GetNoticeDataService service = RetrofitInstance.getRetrofitInstance().create(GetNoticeDataService.class);
/** Call the method with parameter in the interface to get the notice data*/
Call<SyncModule> call = service.GetSyncModuleList(operation, sessionId, module, syncToken, mode);
/**Log the URL called*/
Log.i("URL Called", call.request().url() + "");
call.enqueue(new Callback<SyncModule>() {
@Override
public void onResponse(Call<SyncModule> call, Response<SyncModule> response) {
Log.e("response", new Gson().toJson(response.body()));
if (response.isSuccessful()) {
Log.e("response", new Gson().toJson(response.body()));
SyncModule syncModule = response.body();
Gson g = new Gson();
String jsonAllProducts = g.toJson(syncModule);
tinydb.putString("jsonAllProducts",jsonAllProducts);
workingOnResponseProduct(syncModule);
}
}
@Override
public void onFailure(Call<SyncModule> call, Throwable t) {
}
// progressDialog.dismiss();
});
}
}, 0);
return ;
}
private void workingOnResponseProduct(SyncModule syncModule) {
autoproduct_name.setHint(Html.fromHtml ( "Item Name"+""));
String success = syncModule.getSuccess();
if (success.equals("true")) {
SyncResults results = syncModule.getResult();
Sync sync = results.getSync();
ArrayList<SyncUpdated> syncUpdateds = sync.getUpdated();
for (SyncUpdated syncUpdated : syncUpdateds) {
String unitprice="";
product_id = syncUpdated.getId();
ArrayList<SyncBlocks> syncBlocks = syncUpdated.getBlocks();
for (SyncBlocks syncBlocks1 : syncBlocks) {
String label = syncBlocks1.getLabel();
//Basic Information
if (label.equals("Product Details")) {
ArrayList<SynFields> synFields = syncBlocks1.getFields();
for (SynFields synFields1 : synFields) {
String name = synFields1.getName();
values = synFields1.getValue();
if (name.equals("productname")) {
productname = String.valueOf(values);
product_name.add(productname);
Log.d("products",String.valueOf(product_name.size()));
RecordsProducts records = new RecordsProducts(product_id,productname);
recordsListProduct.add(records);
}
}
}
//pricing information
else if (label.equals("Pricing Information")) {
ArrayList<SynFields> synFields = syncBlocks1.getFields();
for (SynFields synFields1 : synFields) {
String name = synFields1.getName();
values = synFields1.getValue();
if (name.equals("unit_price")) {
unitprice = String.valueOf(values);
Log.d("unitprice",unitprice);
//
}
}
}
}
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1, product_name);
autoproduct_name.setAdapter(adapter);
autoproduct_name.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(position+1 > 0) {
RecordsProducts recordsProducts=recordsListProduct.get(position);
String product_id=recordsProducts.getId();
Log.d("productid",product_id);
String productnames=recordsProducts.getProductname();
autoproduct_name.setText(productnames);
Toast.makeText(getContext(),"selected"+product_id,Toast.LENGTH_LONG).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
TableRow tbrow0 = new TableRow(getContext());
Resources resource = getContext().getResources();
tbrow0.setLayoutParams(getLayoutParams());
tbrow0.addView(getTextView(0, "Product Name", Color.WHITE, Typeface.NORMAL, resource.getColor(R.color.tabs1)));
tbrow0.addView(getTextView(0, "Quantity", Color.WHITE, Typeface.NORMAL,resource.getColor(R.color.tabs1)));
tbrow0.addView(getTextView(0, "Unit Price", Color.WHITE, Typeface.NORMAL, resource.getColor(R.color.tabs1)));
tbrow0.addView(getTextView(0, "Total", Color.WHITE, Typeface.NORMAL, resource.getColor(R.color.tabs1)));
stk.addView(tbrow0,getLayoutParams());
//String[] namesList = p.split(",");
//String[] priceList = listprices.split(",");
}
【问题讨论】:
-
您好,您在哪里添加了 OnItemClickListener?它是项目选择侦听器,其工作方式不同。
-
setOnItemSelectedListener
-
那是不同的,当你选择一些东西时它会起作用。您需要将其更改为 adapter.OnItemClickListener 。
-
请查看我的答案,它现在应该可以解决您的问题。 :)
-
这能回答你的问题吗? setOnItemClickListener on custom ListView
标签: android android-toast