适配器未在listview android

时间:2016-12-21 09:45:10

标签: android listview

您好我正在处理列表视图,我只想在列表视图中设置值。我发布了如下代码。根据SimpleAdapter它只显示内容的名称。我也想显示计数。但是计数没有显示,在应用程序中它只显示textview的名称而不是计数。请建议我解决问题的方法。我还附上了截图。

import java.util.ArrayList;
import android.R.*;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ActionBar;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Folders extends navigation_drawer_class {

    static final String NEW = "new", OVERDUE = "overdue", ASSIGNED = "assigned", TRASH = "trash", SPAM = "spam", NAME = "name", COUNT = "count";

    ListView folders_list;
    String[] folders;
    String[] filter_id = {
        "2",
        "3",
        "4",
        "5",
        "6"
    };
    List < String > folder_count;
    JSONArray quick_view_array;
    JSONObject quick_view_obj, count_obj;
    String new_count, overdue_count, assigned_count, trash_count, spam_count;
    List < HashMap < String, String >> menuItems;
    Dialog dialog;
    String URL;
    Operation op = new Operation();

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.folders);
        new getbrand().execute();

        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        //-----------------------
        getLayoutInflater().inflate(R.layout.folders, frameLayout);
        mDrawerList.setItemChecked(position, true);
        setTitle(listArray[position]);
        //------------------------

        if (Operation.isNetworkAvailable(this)) {
            folders_list = (ListView) findViewById(R.id.folder_display_list);
            new folders().execute();

            folders_list.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView << ? > parent, View view, int position, long id) {
                    // TODO Auto-generated method stub  

                    String fid = filter_id[position];
                    String title = folders[position];
                    Intent i = new Intent(Folders.this, Tickets.class);
                    i.putExtra("filter_id", "&vis_filter_id=" + fid);
                    i.putExtra("title", title);
                    i.putExtra("set_queue", "no");

                    startActivity(i);
                }
            });
        } else {
            Operation.showToast(getApplicationContext(), R.string.no_network);
        }
    }
    private class folders extends AsyncTask < Void, Void, JSONArray > {
        Dialog dialog;
        @Override
        public void onPreExecute() {
            dialog = new Dialog(Folders.this, android.R.style.Theme_Translucent_NoTitleBar);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.progressbar);
            dialog.show();
        }
        @Override
        protected JSONArray doInBackground(Void...params) {
            // TODO Auto-generated method stub
            URL = op.getUrl(getApplicationContext(), "ticket", "quick_view", "");
            quick_view_array = JSONfunctions.getJSONfromURL(URL + "&vis_encode=json", Folders.this);
            return quick_view_array;
        }
        @Override
        public void onPostExecute(JSONArray quick_view_array) {
            super.onPostExecute(quick_view_array);
            try {
                quick_view_obj = quick_view_array.getJSONObject(0);
                count_obj = quick_view_obj.getJSONObject("count");
                folder_count = new ArrayList < String > ();
                folder_count.add(count_obj.getString(NEW));
                folder_count.add(count_obj.getString(OVERDUE));
                folder_count.add(count_obj.getString(ASSIGNED));
                folder_count.add(count_obj.getString(TRASH));
                folder_count.add(count_obj.getString(SPAM));
                folders = getResources().getStringArray(R.array.folders);
                menuItems = new ArrayList < HashMap < String, String >> ();

                for (int i = 0; i < filter_id.length; i++) {
                    HashMap < String, String > map = new HashMap < String, String > ();
                    map.put(NAME, folders[i]);
                    map.put(COUNT, folder_count.get(i));
                    menuItems.add(map);
                }

                SimpleAdapter list = new SimpleAdapter(Folders.this,
                    menuItems,
                    R.layout.folders,
                    new String[] {
                        NAME,
                        COUNT
                    },
                    new int[] {
                        R.id.folder_name, R.id.folder_count
                    }

                );
                folders_list.setAdapter(list);
                dialog.dismiss();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.home, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        String filter_id = null, Tickets_title = null;
        int start_limit = 0, page_no = 1;

        // TODO Auto-generated method stub
        switch (item.getItemId()) {
            case R.id.menu_inbox:
                Intent inbox = new Intent(Folders.this, Tickets.class);
                inbox.putExtra("filter_id", "&vis_filter_id=1");
                inbox.putExtra("title", "Inbox");
                inbox.putExtra("set_queue", "no");
                startActivity(inbox);
                return true;

            case R.id.menu_new_ticket:
                Intent new_ticket = new Intent(Folders.this, New_Ticket_step1.class);
                startActivity(new_ticket);
                return true;

            case R.id.menu_ticket_queue:
                Intent ticket_queue = new Intent(Folders.this, Queues.class);
                ticket_queue.putExtra("set_queue", "set");
                startActivity(ticket_queue);
                return true;

            case R.id.menu_clients:
                Intent clients = new Intent(Folders.this, Client.class);
                startActivity(clients);
                return true;

                /* case R.id.menu_blabby:
                Intent blabby = new Intent(Folders.this,Blabby.class);
                blabby.putExtra("operation","get_blabs");
                blabby.putExtra("filter","");
                blabby.putExtra("title",(String)getString(R.string.blabs));
                startActivity(blabby);
                return true; */
                //-------- Added for separate page

            case R.id.menu_pin:
                Intent pin = new Intent(Folders.this, Pinned_items.class);
                startActivity(pin);
                return true;
                //-------- Added for separate page

            case R.id.menu_settings:
                Intent settings = new Intent(Folders.this, Settings.class);
                startActivity(settings);
                return true;

            case R.id.menu_ticket_search:
                Intent search = new Intent(Folders.this, Search.class);
                search.putExtra("set_queue", "no");
                startActivity(search);
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onBackPressed() {
        moveTaskToBack(true);
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        this.finish();
    }

    private class getbrand extends AsyncTask < Void, Void, JSONArray > {
        Dialog dialog;
        @Override
        public void onPreExecute() {
            dialog = new Dialog(Folders.this, android.R.style.Theme_Translucent_NoTitleBar);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.progressbar);
            dialog.show();
        }

        @Override
        protected JSONArray doInBackground(Void...params) {
            // TODO Auto-generated method stub
            String access = op.getUrl(getApplicationContext(), "ticket", "get_branding", "");
            JSONArray access_denied = JSONfunctions.getJSONfromURL(access + "&vis_encode=json", Folders.this);
            return access_denied;

        }
        @Override
        public void onPostExecute(JSONArray access_denied) {
            super.onPostExecute(access_denied);
            String access_result = access_denied.toString();

            ActionBar ab = getActionBar();
            if (access_result.equals("[\"1\"]")) {
                ab.setTitle(R.string.app_name);
                ab.setIcon(R.drawable.application_icon);
                //  ab.setDisplayShowTitleEnabled(false); 
                //  ab.setDisplayShowHomeEnabled(false);
                //  ab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffffff")));    
            } else {
                ab.setTitle(R.string.nobrand_app_name);
                ab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffffff")));
                ab.setIcon(R.drawable.white3);
            }

            dialog.dismiss();
        }
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="3dp"
    android:weightSum="100">

    <TextView
        android:id="@+id/folder_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_weight="30"
        android:text="TextView"
        android:textColor="#115c28"
        android:textSize="15dp" />

    <TextView
        android:id="@+id/folder_count"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginRight="10dp"
        android:layout_weight="70"
        android:gravity="center"
        android:text="0"
        android:textColor="#115c28"
        android:textSize="20dp"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:orientation="vertical">

        <ListView
            android:id="@+id/folder_display_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            />
    </LinearLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:7)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="3dp"
    android:weightSum="100">

    <TextView
        android:id="@+id/folder_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"

第一个textview占据水平LinearLayout的整个宽度,因此您无法看到第二个textview。

您可以通过制作文字视图wrap_content的宽度来查看计数,但它可能不会与您尝试实现的设计相匹配。

答案 1 :(得分:0)

您好我只需按照以下方式对xml模板进行查找。

            <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="top"
            android:orientation="vertical"
            android:weightSum="100"
            >
            <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
            <!--  -->
            <TextView
            android:id="@+id/folder_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingLeft="30dip"
            android:textSize="15dp" 
            android:layout_weight="1"  
            android:textColor="#115c28"/>

            <TextView
            android:id="@+id/folder_count"
            android:layout_width="0dp"
            android:layout_height="60dp"

            android:textSize="20dp" 
            android:textStyle="bold"
            android:textColor="#115c28"  
            android:gravity="center"
            android:paddingRight="30dip"
            android:paddingLeft="35dip"
            android:layout_weight="1" />
            <!--  -->
            </LinearLayout>   


            <ListView
            android:id="@+id/folder_display_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />


            </LinearLayout>

现在正常工作。