listview没有显示所有数据

时间:2013-06-12 05:15:38

标签: android android-listview

这是我的源代码; listview没有显示“状态”。其余的数据是可见的'我不知道问题出在哪里。请帮助我,有一段时间我正试图解决这个问题。它是发送电子邮件并接收的应用程序,这是邮箱源代码。在邮箱中,它应显示状态,来自,专家,主题,价格。

public class MailBox extends ListActivity {
    String userName;
    ListView MailList;
    //////contact JSonArray
    JSONArray contacts = null;
    String x;
    private static final String Status="a";
    String StatusVal="";
    private static final String From="b";
    String FromVal;
    private static final String Expert="c";
    String ExpertVal;
    private static final String Subject="d";
    private static final String Price="e";
    String SubjectVal;
     // JSON Node names
    private static final String TAG_CONTACTS = "list";
    private static final String TAG_ID = "mail_ID";
    private static final String TAG_STATUS = "mail_status";
    private static final String TAG_FROM_ID= "fromID";
    private static final String TAG_TO_ID= "toID";
    private static final String TAG_QUESTION= "questionid";
    private static final String TAG_PRICE= "mail_price";
     String xVal="";
     String arr[];
     Handler handler;
      // Hashmap for ListView
     ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>(); 
     ProgressDialog progressDialog;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mail_box);
        MailList=(ListView) findViewById(R.id.list);
        final Bundle b=getIntent().getExtras();
        if(b!=null)
        {
        userName=b.getString("userName");
        }


        handler = new Handler();
        starttask();
    }
    public void starttask()
    {

        progressDialog = ProgressDialog.show(this,"" , "Logging In");
         Thread thread = new Thread() {
             public void run () {
             String url = "http://onlinesolutionproviders.com/apiosp.php?student_inbox=1&client_login_id="+userName;
                // Creating JSON Parser object   
        JSONParser jParser = new JSONParser();
        JSONObject json = jParser.getJSONFromUrl(url);
        HttpClient httpClient = new DefaultHttpClient();

         HttpContext httpContext= new BasicHttpContext();
        try {
            contacts = json.getJSONArray(TAG_CONTACTS);
            for(int i = 0; i < contacts.length(); i++){
              JSONObject c = contacts.getJSONObject(i);
              String id = c.getString(TAG_ID);
              String name = c.getString(TAG_STATUS);
              String fromid = c.getString(TAG_FROM_ID); 
              String toid = c.getString(TAG_TO_ID);
              String subject = c.getString(TAG_QUESTION); 
              String price = c.getString(TAG_PRICE); 
                HashMap<String, String> map = new HashMap<String, String>();
                map.put(TAG_ID, id);
                map.put(TAG_STATUS, name);//status
                map.put(TAG_FROM_ID, fromid);//from id
                map.put(TAG_TO_ID, toid);//expert name
                map.put(TAG_QUESTION, subject);//subject
                map.put(TAG_PRICE, price);//price
                String URL="http://onlinesolutionproviders.com/apiosp.php?client_email_subject=1&client_login_id="+userName+"&to_id="
                        +toid+"&from_id="+fromid+"&questionid="+id+"&status="+name;
                // Check your log cat for JSON reponse    
                Log.d("url mail box",URL);


                 HttpGet httpGet = new HttpGet(URL);

             try
             {
                HttpResponse response = httpClient.execute(httpGet, httpContext);
                HttpEntity hte=response.getEntity();
                InputStream is= hte.getContent();
                BufferedReader br=new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb= new StringBuilder();
                String line=null;
                while ((line=br.readLine()) != null)
                {
                    sb.append(line + "");
                }
                is.close();
                 x= String.valueOf(Html.fromHtml(sb.toString()));

                Scanner scan= new Scanner(x);

                arr= new String[5];
                int j=0;
                while(scan.hasNextLine())
                {
                    arr[j]=scan.nextLine();
                    j++;
                }
                 map.put(Status, arr[0]);
                 map.put(From, arr[1]);
                 map.put(Expert, arr[2]);
                 map.put(Subject, arr[3]);       
                map.put(Price, arr[4]);   
             }
             catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "OOps! Something went wrong. Restart the Application" , Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "OOps! Something went wrong. Restart the Application" , Toast.LENGTH_LONG).show();
                }

                contactList.add(map);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        } 
            handler.post(new Runnable() {
             public void run() {
                 updateUI();
                 progressDialog.dismiss();



                                }
                            });

                        }

                    };

                    thread.start();


       }
     private void updateUI () {
         SpecialAdapter adapter = new SpecialAdapter(MailBox.this, contactList,
                    R.layout.list_item_mailbox,
                    new String[] { TAG_ID, Status, From, Expert, Subject, Price, TAG_FROM_ID,TAG_TO_ID,TAG_QUESTION, TAG_STATUS }, new int[] {
                           R.id.eID ,R.id.status_heading,  R.id.expert_heading,R.id.eexpert_heading, R.id.subject_heading, R.id.price_heading,
                           R.id.fromid, R.id.toid,R.id.question,R.id.statusid}); 
          setListAdapter(adapter);
          ListView lv = getListView();
            lv.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {

                    /* for(int a = 0; a < parent.getChildCount(); a++)
                     {
                         parent.getChildAt(a).setBackgroundColor(Color.BLACK);
                     }

                     view.setBackgroundColor(Color.RED);*/
                    String mailid = ((TextView) view.findViewById(R.id.eID)).getText().toString();
                    String fromid = ((TextView) view.findViewById(R.id.fromid)).getText().toString();
                    String toid = ((TextView) view.findViewById(R.id.toid)).getText().toString();
                    String question = ((TextView) view.findViewById(R.id.question)).getText().toString();
                    String status = ((TextView) view.findViewById(R.id.status_heading)).getText().toString();
                    String pricetosend = ((TextView) view.findViewById(R.id.price_heading)).getText().toString();
                    Intent in = new Intent(getApplicationContext(), MailBoxDetail.class);
                    in.putExtra("mailid", mailid);
                    in.putExtra("fromid", fromid);
                    in.putExtra("toid", toid);
                    in.putExtra("question", question);
                    in.putExtra("userName", userName);
                    in.putExtra("status", status);
                    in.putExtra("pricetosend", pricetosend);
                    startActivity(in);
                }
            });
    }

这是specialAdapter的代码

package com.OSP.client;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;

public class SpecialAdapter extends SimpleAdapter {
    private int[] colors = new int[] { Color.parseColor("#FFFFFF"), Color.parseColor("#D8D8D8") };

        public SpecialAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
            super(context, items, resource, from, to);
        }

    @Override
        public View getView(int position, View convertView, ViewGroup parent) {
          View view = super.getView(position, convertView, parent);
          int colorPos = position % colors.length;
          view.setBackgroundColor(colors[colorPos]);
          return view;
        }
    }

1 个答案:

答案 0 :(得分:0)

我认为你是Android开发的新手。欢迎。

您需要在row.xml getView方法中自行编写这些SpecialAdapter行布局的自定义实现。您可以引用以下代码来制作你自己的东西:(很简单)

@Override
    public View getView ( int position, View convertView, ViewGroup parent ) {

        /* create a new view of my layout and inflate it in the row */
        convertView = ( RelativeLayout ) inflater.inflate( resource, null );

        /* Extract the city's object to show */
        City city = getItem( position );

        /* Take the TextView from layout and set the city's name */
        TextView txtName = (TextView) convertView.findViewById(R.id.cityName);
        txtName.setText(city.getName());

        /* Take the TextView from layout and set the city's wiki link */
        TextView txtWiki = (TextView) convertView.findViewById(R.id.cityLinkWiki);
        txtWiki.setText(city.getUrlWiki());

        /* Take the ImageView from layout and set the city's image */
        ImageView imageCity = (ImageView) convertView.findViewById(R.id.ImageCity);
        String uri = "drawable/" + city.getImage();
        int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
        Drawable image = context.getResources().getDrawable(imageResource);
        imageCity.setImageDrawable(image);
        return convertView;
    }

您应该浏览developer.android.comGetting Started部分内容。但是这tutorial会让您更快地开始。

如果这对您有任何帮助,请将此标记为您的答案。