【发布时间】:2016-07-17 00:44:15
【问题描述】:
我正在使用列表视图创建多级可扩展列表视图。所以我希望在单击它时更改列表视图的指示器。我在可绘制文件夹中有两个 png 图像。单击事件无法正常工作。单击减号时不会出现。我指的是 this。 我的主要活动
public class MainActivity extends Activity {
List<NLevelItem> lists;
ListView listView;
RequestQueue requestQueue1, requestQueue2;
int[] firstcatid = null,secondcatid = null;
String firsttitle[] = null,secondtitle[] = null;
int j = 0;
//ArrayList<Group> list,list2,list3;
NLevelItem parent;
Boolean b=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView1);
lists = new ArrayList<NLevelItem>();
// Random rng = new Random();
final LayoutInflater inflater = LayoutInflater.from(this);
requestQueue1= Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://www.fasr.com/cntrldata/ezproductfristcategory.php",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
//list = new ArrayList<Group>();
JSONArray ja = response.getJSONArray("hi");
JSONArray ja2 = response.getJSONArray("hi2");
JSONArray ja3 = response.getJSONArray("hi3");
firsttitle = new String[ja.length()];
firstcatid = new int[ja.length()];
JSONObject jsonObject2 = null, jsonObject3 = null, jsonObject;
for (int i = 0; i < ja.length(); i++) {
jsonObject = ja.getJSONObject(i);
// Log.e("hie",jsonObject2.getInt("second_category_id")+"");
//jsonObject3 = ja3.getJSONObject(i);
firstcatid[i] = jsonObject.getInt("first_category_id");
firsttitle[i] = jsonObject.getString("first_category_name");
}
secondcatid = new int[ja2.length()];
for (int i = 0; i < ja2.length(); i++) {
jsonObject2 = ja2.getJSONObject(i);
secondcatid[i] = jsonObject2.getInt("second_category_id");
}
for (int i = 0; i < ja.length(); i++) {
final NLevelItem grandParent = new NLevelItem(new SomeObject(firsttitle[i]), null, new NLevelView() {
@Override
public View getView(NLevelItem item) {
final View view = inflater.inflate(R.layout.list_item, null);
TextView tv = (TextView) view.findViewById(R.id.textView);
final ImageView iv=(ImageView)view.findViewById(R.id.imageView);
//tv.setBackgroundColor(Color.GREEN);
iv.setVisibility(View.VISIBLE);
/*
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"hello",Toast.LENGTH_SHORT).show();
if(!b){
iv.setBackgroundResource(R.drawable.ic_action_minus);
b=true;
}else {
b=false;
iv.setBackgroundResource(R.drawable.ic_action_add);
}
}
});*/
String name = (String) ((SomeObject) item.getWrappedObject()).getName();
tv.setText(name);
return view;
}
});
lists.add(grandParent);
//int numChildren = rng.nextInt(4) + 1;
for (int j = 0; j < ja2.length(); j++) {
jsonObject2 = ja2.getJSONObject(j);
if (firstcatid[i] == jsonObject2.getInt("first_category_id")) {
parent = new NLevelItem(new SomeObject(jsonObject2.getString("second_category_name")), grandParent, new NLevelView() {
@Override
public View getView(NLevelItem item) {
View view = inflater.inflate(R.layout.list_item, null);
TextView tv = (TextView) view.findViewById(R.id.textView);
//tv.setBackgroundColor(Color.YELLOW);
tv.setPadding(20,0,0,0);
String name = (String) ((SomeObject) item.getWrappedObject()).getName();
tv.setText(name);
return view;
}
});
lists.add(parent); } // int grandChildren = rng.nextInt(5) + 1;
for (int k = 0; k < ja3.length(); k++) {
jsonObject3 = ja3.getJSONObject(k);
if (secondcatid[j] == jsonObject3.getInt("second_category_id")) {
NLevelItem child = new NLevelItem(new SomeObject(jsonObject3.getString("third_category_name")), parent, new NLevelView() {
@Override
public View getView(NLevelItem item) {
View view = inflater.inflate(R.layout.list_item, null);
TextView tv = (TextView) view.findViewById(R.id.textView);
//tv.setBackgroundColor(Color.GRAY);
tv.setPadding(40,0,0,0);
String name = (String) ((SomeObject) item.getWrappedObject()).getName();
tv.setText(name);
return view;
}
});
lists.add(child);}
}
}
}
NLevelAdapter adapter = new NLevelAdapter(lists);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
((NLevelAdapter) listView.getAdapter()).toggle(arg2);
((NLevelAdapter) listView.getAdapter()).getFilter().filter();
}
});
// Toast.makeText(getApplicationContext(),url[2],Toast.LENGTH_SHORT).show();
//output.setText(data);
//ExpListItems = list;
// ExpAdapter = new ExpandListAdapter(getApplicationContext(), ExpListItems);
// ExpandList.setAdapter(ExpAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
}
}
);
requestQueue1.add(jsonObjectRequest);
}
class SomeObject {
public String name;
public SomeObject(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
我的列表项 xml 是
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listItemContainer">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="15sp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:textColor="#020202" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:visibility="invisible"
android:layout_gravity="right"
android:layout_marginLeft="5dp"
android:src="@drawable/ic_action_add"
android:layout_alignParentEnd="false"
android:layout_alignParentRight="true" />
</RelativeLayout>
【问题讨论】:
标签: android json listview android-studio onclicklistener