我使用由JSON填充的ListView,这部分有效,但是当我点击列表项时,点击不起作用。
我已阅读过有关的事情
setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
但我不明白该把它放在哪里。
这是我的代码
这个片段显示在MainActivity的TabLayout中
public class Tab3 extends Fragment {
private View v;
private ListView listView;
private ArrayList<CustomModel> mCustomArrayList = new ArrayList<CustomModel>();
private AdapterCustom adapter1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String equipeJSONString=((MainActivity)getActivity()).equipeJSONString;
try {
JSONObject obj = new JSONObject(equipeJSONString);
JSONArray equipe = obj.getJSONArray("equipe");
for (int i = 0; i < equipe.length(); i++) {
JSONObject c = equipe.getJSONObject(i);
//stock les valeurs du Json dans des vars
String nom = c.getString("nom");
String photo = c.getString("photo");
String texte = c.getString("texte");
mCustomArrayList.add(new CustomModel(nom, texte,photo));
}
} catch (Throwable t) {
Log.e("Tab3", "Erreur Could not parse malformed JSON : \"" + equipeJSONString + "\"");
}
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
v =inflater.inflate(R.layout.tab_3,container,false);
// v.setBackgroundColor(Color.BLACK);
listView = (ListView) v.findViewById(R.id.customlist);
adapter1 = new AdapterCustom(getActivity(), mCustomArrayList);
// Assign adapter to ListView
listView.setAdapter(adapter1);
// ListView Item Click Listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.v("Tab3","click on Item");
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getActivity().getApplicationContext(),
"Position :" + itemPosition + " ListItem : " + itemValue, Toast.LENGTH_LONG)
.show();
Intent myIntent = new Intent(getActivity(),CustomActivity.class);
startActivity(myIntent);
}
});
return v;
}
}
编辑:我使用String []
进行了测试如果我使用String []填充它有效的列表,我可以点击。
在OnCreateView上测试代码:
String[] values2 = new String[] { "Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
"Android Example",
"List View Source Code",
"List View Array Adapter",
"Android Example List View"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, android.R.id.text1, values2);
当我使用AdapterCustom填充时出了什么问题?
答案 0 :(得分:0)
listView.setClickable(true);//in fragment
@Override
public boolean isEnabled(int position) //in adapter
{
return true;
}
答案 1 :(得分:0)
你可以在onItemClick
内使用一个开关盒
像
switch(position)
{
case 0:
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getActivity().getApplicationContext(),
"Position :" + itemPosition + " ListItem : " + itemValue, Toast.LENGTH_LONG)
.show();
Intent myIntent = new Intent(getActivity(),CustomActivity.class);
startActivity(myIntent);
}
答案 2 :(得分:0)
好的,我发现了,我有一个
.setOnClickListener(new View.OnClickListener() {
处理点击的CustomAdapter
。
进行这些测试有助于我找到解决方案。