我想在ListView
中显示两行Alert Dialog
。我正在创建具有ListView
的新视图,并将其设置为使用简单Adapter
。我使用hashmap
string
填充简单适配器,我通过JSONArray
。当我显示它时,我希望在代码中显示点击事件,但列表项不响应单击。但是,如果我只将常规string
文字填充到ListView
,则会点击该项目。代码如下:
View view = Testas.this.getLayoutInflater().inflate(R.layout.commands_listview, null);
trackersHash = new ArrayList<HashMap<String, String>>();
trackersAdapter = new SimpleAdapter(Testas.this, trackersHash, R.layout.commands_listview_layout, new String[] {"line1","line2"}, new int[] {R.id.command, R.id.explanation});
ListView trackersListView = (ListView) view.findViewById(R.id.listview);
trackersListView.setItemsCanFocus(true);
trackersListView.setClickable(true);
HashMap<String,String> hashMap;else if (jsonArray.length() > 10)
{
for (int i = 0; i < 10; i++)
{//GETS displayed, but does not respond when i Click on it
hashMap = new HashMap<String, String>();
hashMap.put("line1", jsonArray.getJSONObject(i).getString("licence").trim());
hashMap.put("line2", jsonArray.getJSONObject(i).getString("hwid").trim());
trackersHash.add(hashMap);
}
hashMap = new HashMap<String, String>();//Gets displayed and RESPONDS to Click
hashMap.put("line1", "Note: Query has a result of more than 10 items.");
hashMap.put("line2", "Please clarify car/tracker data to receive more accurate results");
trackersHash.add(hashMap);
}
trackersListView.setAdapter(trackersAdapter);
AlertDialog.Builder builder = new AlertDialog.Builder(Testas.this);
builder.setView(view);
trackersListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
try
{
carReg.setText(jsonArray.getJSONObject(position).getString("licence").trim());
carBrand.setText(jsonArray.getJSONObject(position).getString("brand").trim());
carComp.setText(jsonArray.getJSONObject(position).getString("company").trim());
carID = jsonArray.getJSONObject(position).getString("hwid");
}
catch (JSONException e)
{
e.printStackTrace();
}
Pagrindinis.socketIntent = new Intent(Testas.this, SocketService.class);
Pagrindinis.socketIntent.putExtra("type", 0);
Pagrindinis.socketIntent.putExtra("carId", carID);
startService(Pagrindinis.socketIntent);
Toast.makeText(Testas.this, "Parsing Data...", Toast.LENGTH_SHORT).show();
}
});
builder.setTitle("Select Tracker:");
builder.show();
如果我将string
里面的简单文本放入循环解析中,一切正常,所以我认为从string
解析JSONArray
是错误的,但它们显示为{{1我也不知道那时候错了什么。有任何想法吗?谢谢:))