ListView Android无法显示项目

时间:2014-07-02 03:28:37

标签: android json listview

我只是尝试自定义Listview android来解析JSON(JSON没问题没问题),这对我来说很奇怪,因为我可以获得该项目,但Listview没有显示该项目,实际上我很困惑。我已经检查了使用方法Toast,它为项目干杯,但它无法显示,这里是我的来源: ListView适配器:

import java.net.URL;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ListViewAdapter extends BaseAdapter{
    private static ArrayList<ItemClass> DataProcessorResult;
    private LayoutInflater myInflater;
    Context mycontext;

    public ListViewAdapter(Context context, ArrayList<ItemClass> results) {
        DataProcessorResult = results;
        myInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        //Log.d("count", String.valueOf(DataProcessorResult.size()));
        return DataProcessorResult.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return DataProcessorResult.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }   

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
    { 
        final ViewHolder holder; 
        final Context mycontext=parent.getContext();       

        if(convertView == null) {
            convertView = myInflater.inflate(R.layout.custom_viewproduct, null);
            holder = new ViewHolder();
            holder.txtItemCode = (TextView) convertView.findViewById(R.id.title);
            holder.txtHarga = (TextView) convertView.findViewById(R.id.harga);
            holder.imgitem= (ImageView) convertView.findViewById(R.id.imgitem);
            convertView.setTag(holder);            
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtItemCode.setText(DataProcessorResult.get(position).getTitle());
        holder.txtHarga.setText(DataProcessorResult.get(position).getHarga());

        Toast.makeText(mycontext, DataProcessorResult.get(position).getTitle(), Toast.LENGTH_SHORT).show();

        if (position % 2 == 1) {convertView.setBackgroundColor(Color.WHITE);} else {convertView.setBackgroundColor(Color.rgb(208,212,208)); }        
        return convertView;
    } 
    static class ViewHolder { 
        TextView txtItemCode;
        TextView txtHarga;
        TextView txtSize;
        TextView txtPrice;
        ImageView imgitem;
    }

}

我的主要计划:

import java.util.ArrayList;
import java.util.Hashtable;

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

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class Saletab extends Activity {
    public static String BaseUrl="";
    final int PROGRESS_DIALOG=1;

    ViewConnection connection;

    ArrayList<ItemClass> itemlist=new ArrayList<ItemClass>();           
    ArrayList<ItemClass> arritem=new ArrayList<ItemClass>();    
    ArrayList<String> arritemcategory;

    ListView listview;
    //ListViewAdapter ListViewAdapter;

    @Override
    protected Dialog onCreateDialog(int id) {
        switch(id)
        {
        case PROGRESS_DIALOG:
            ProgressDialog progress =new ProgressDialog(this);
            progress.setMessage("Loading");
            progress.setTitle("Initializing Please Wait");          
            progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progress.setProgress(0);
            progress.setMax(100);           
            return progress;
        }
        return super.onCreateDialog(id);
    }

    private class GetDeptAyncTask extends AsyncTask<Hashtable<String,String>,Void,String> {             
        @Override
        protected void onPreExecute() {
            showDialog(PROGRESS_DIALOG);
            super.onPreExecute();
        } 
        @Override
        protected String doInBackground(Hashtable<String, String>... params) 
        {
            // TODO Auto-generated method stub
            Hashtable ht=params[0];         

            String json=HelperHttp.getJSONResponseFromURL(BaseUrl, ht);
            if(json!=null) {
                parseJsonString(itemlist,json); 
            }else{
                return "No internet access";
            }
            return json;
        }               

        protected void parseJsonString(ArrayList<ItemClass> itemlistjs,String json){
            try {               
                JSONObject jsonObj = new JSONObject(json);
                JSONArray array = jsonObj.getJSONArray("listitem");             

                ItemClass.setlength(array.length());

                for (int i=0;i<array.length();i++){
                    JSONObject js=array.getJSONObject(i);

                    ItemClass item=new ItemClass(js.getString("IdProperty"),
                            js.getString("NamaSales"),
                            js.getString("Title"),
                            js.getString("Kota"),
                            js.getString("Harga"),
                            js.getString("IdProperty"));
                    itemlistjs.add(item);
                }
            }catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        protected void onPostExecute(String result){
            if(result=="SUCCESS")
            {
            }else{              
                JSONObject jObject = null;
                Context mContext = null;
                try {                   
                    jObject = new JSONObject(result);
                    JSONArray jArray = jObject.getJSONArray("listitem");

                    for (int i=0; i < jArray.length(); i++)
                    {                     
                        JSONObject oneObject=jArray.getJSONObject(i);
                        ItemClass item=new ItemClass(oneObject.getString("IdProperty"),
                                oneObject.getString("NamaSales"),
                                oneObject.getString("Title"),
                                oneObject.getString("Kota"),
                                oneObject.getString("Harga"),
                                oneObject.getString("IdProperty")); 
                        //Toast.makeText(getBaseContext(), oneObject.getString("Title"), Toast.LENGTH_SHORT).show();
                        //Log.d("helo",oneObject.getString("NamaSales"));
                        arritem.add(item);                          
                    }               
                    listview = (ListView) findViewById(R.id.listitem); 
                    Toast.makeText(getBaseContext(), arritem.get(0).toString(), Toast.LENGTH_SHORT).show();
                    listview.setAdapter(new ListViewAdapter(getBaseContext(), arritem));
                }catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            removeDialog(PROGRESS_DIALOG);
            super.onPostExecute(result);
        }       
    }   

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_saletab);

        connection = new ViewConnection(getBaseContext());
        if (connection.isConnectingToInternet())
        {
            BaseUrl="http://92.68.228.81/dummy/item.php?status=J";
            executeAsyncTask();             
        }else {
            // Internet connection is not present
            LayoutInflater li = LayoutInflater.from(Saletab.this);
            final View inputdialogcustom = li.inflate(R.layout.activity_confirm_connection, null);              
            AlertDialog.Builder alert = new AlertDialog.Builder(Saletab.this);                                              
            alert.setView(inputdialogcustom);       

            alert.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
                @Override
                public void onClick(DialogInterface dialog, int which) {                                
                    System.exit(0);
                }
            }); 
            alert.show();
        }       
    }

    private void executeAsyncTask(){
        Hashtable<String,String> ht=new Hashtable<String,String>();
        GetDeptAyncTask async=new GetDeptAyncTask();
        Hashtable[] ht_array={ht};
        async.execute(ht_array);
    }

}

我的布局:

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <!-- HEADER -->
    <LinearLayout
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" 
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:background="#000">
        <TextView
            android:id="@+id/tvlabel"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"          
            android:text="Jumlah data : "
            android:textColor="#FFF">           
        </TextView>             
        <View
            android:layout_width="fill_parent"
            android:layout_height="2dp"
            android:background="#acb0ac" />
    </LinearLayout>
    <!-- FOOTER -->
    <LinearLayout
        android:id="@+id/footer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" 
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:background="#000">
    </LinearLayout>

    <ListView
        android:id="@+id/listitem"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="10dp"
        android:layout_above="@+id/header"
        android:layout_below="@+id/footer">
    </ListView>

</RelativeLayout>

我的自定义布局列表视图

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">    
    <ImageView 
        android:id="@+id/imgitem" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:background="@layout/round_border"
        android:src="@drawable/renticon"> 
    </ImageView> 
    <TextView 
        android:id="@+id/title" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="2dp" 
        android:text="Coba"
        android:textSize="13sp" > 
    </TextView> 
    <TextView 
        android:id="@+id/harga" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="2dp" 
        android:text="Coba"
        android:textSize="13sp" > 
    </TextView>

</RelativeLayout>

请帮帮我?谢谢。

1 个答案:

答案 0 :(得分:2)

乍一看,我可以看到

这两行是问题的原因。他们将列表视图排除在屏幕之外。

android:layout_above="@+id/header"
android:layout_below="@+id/footer"

将上面的行更改为

android:layout_above="@+id/footer"
android:layout_below="@+id/header"