0

以下是我来自数据库的 Json 数据,我想在列表中列出 interest_name(仅当可见为真时)。列表必须是多级的。我使用 Gson 库解析了 json 文件。但我不知道如何使用列表视图制作多级列表。

  {"interest_id":0,"interest_name":"ROOT","visible":false,"children":
            [{"interest_id":1,"interest_name":"Sports","visible":true,"children":[{"interest_id":2,"interest_name":"Archery","visible":true,"children":[]},{"interest_id":3,"interest_name":"Bow Hunting","visible":true,"children":[]}]},{"interest_id":100,"interest_name":"Contry","visible":true,"children":[{"interest_id":101,"interest_name":"Afghanistan","visible":true,"children":[]},{"interest_id":102,"interest_name":"Akrotiri","visible":true,"children":[]}]},{"interest_id":1000,"interest_name":"Education","visible":true,"children":[]},{"interest_id":1200,"interest_name":"Entertainment","visible":true,"children":[]},{"interest_id":1400,"interest_name":"Books","visible":true,"children":[]},{"interest_id":1600,"interest_name":"Services","visible":true,"children":[]},{"interest_id":1800,"interest_name":"Fitness","visible":true,"children":[]},{"interest_id":2000,"interest_name":"Fashion","visible":true,"children":[]},{"interest_id":99999,"interest_name":"Near Me","visible":false,"children":[]}]}

我的代码:

主页.java

 Intent intent = new Intent( Home.this,InterestAddList.class);
             startActivity(intent);
                finish();

InterestAddList.java

public class InterestAddList extends Activity {
ListView intrestListView;
OneOnOneListAdapter adapter;
List<String> intrestList;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.intrest_add);

    intrestListView = (ListView) findViewById(R.id.InterestList);
    //Service Called For retrieving Data 
    retrieveList();

}
public void retrieveList() {

    intrestList = new ArrayList<String>();

    StringBuilder urlc = new StringBuilder(urlPrefix + "gai");
    String url=urlc.toString();
        Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();

        String result = ServiceClient.getInstance().getResponse(url);
        InterestNode ni=gson.fromJson(result, InterestNode.class);

        //for(int i=0;i<ni.length;i++){
            //Log.e("ni", ni.getInterestName());
            //Log.e("ni", String.valueOf(ni.getInterestId()));
        /* how to display interest name */
        intrestList.add(ni.getInterestName());
    }

    adapter = new OneOnOneListAdapter(InterestAddList.this,R.layout.intrest_add_row,intrestList);   
    intrestListView.setAdapter(adapter);

}

private class OneOnOneListAdapter extends ArrayAdapter {

    public OneOnOneListAdapter(Context context, int textViewResourceId,
            List objects) {
        super(context, textViewResourceId, objects);
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        final int aposition=position;
        if (v == null)
        {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.intrest_add_row, null);    
        } 

        TextView intrestText =(TextView)v.findViewById(R.id.IntrestText);
        intrestText.setText(intrestList.get(aposition).toString());

        v.setOnClickListener(new OnClickListener(){


            public void onClick(View v) {

                System.out.println("OnItem CLicked");
                Toast.makeText(InterestAddList.this,"Position clicked:"+intrestList.get(aposition).toString(),Toast.LENGTH_SHORT).show();

                Intent i = new Intent(InterestAddList.this,SubIntrestAddList.class);
                i.putExtra("position", aposition);
                startActivityForResult(i,1);

            }

        });

        return v;
    }   
}}

InterestNode.java

public class InterestNode {
    @SerializedName("interest_id")
    int interestId;
    @SerializedName("interest_name")
    String interestName;
    @SerializedName("visible")
    boolean isVisible;

    transient InterestNode parent;
    @SerializedName("children")
    List<InterestNode> childList = new ArrayList<InterestNode>();

    public List<InterestNode> getChildren(){
        return new ArrayList<InterestNode>(childList);
    }
    public int getInterestId() {
        return interestId;
    }

    public String getInterestName() {
        return interestName;
    }

    public InterestNode getParent() {
        return parent;
    }

    public boolean isVisible() {
        return isVisible;
    }

    public void addChild(InterestNode intNode){
        childList.add(intNode);
    }
    public void setInterestId(int interestId) {
        this.interestId = interestId;
    }

    public void setInterestName(String interestName) {
        this.interestName = interestName;
    }

    public void setParent(InterestNode parent) {
        this.parent = parent;
    }

    public void setVisible(boolean isVisible) {
        this.isVisible = isVisible;
    }

}
4

1 回答 1

1

最好使用Expandable ListView添加多级列表。但是你想使用listview然后按照下面的好教程一步一步实现三级listiview。

Android 多级 ListView 教程

希望对你有帮助!

于 2013-02-23T11:42:13.557 回答