how to display fetched json data into listview using baseadapter

前端 未结 7 825
不知归路
不知归路 2020-11-29 07:22

I am new to android and java.Recently I am having problem with displaying fetched json data into listview using baseadapter. At first I have used this code

          


        
7条回答
  •  生来不讨喜
    2020-11-29 07:29

    First u need t create row_listitem.xml file like:

    
    
    
    
    
    
    
        
    
        
    
    
    
    
    
    

    Now, u need to create Custom BaseAdapter like:

      public class BaseAdapter2 extends BaseAdapter {
    
    private Activity activity;
    // private ArrayList<HashMap<String, String>> data;
    private static ArrayList title,notice;
    private static LayoutInflater inflater = null;
    
    public BaseAdapter2(Activity a, ArrayList b, ArrayList bod) {
        activity = a;
        this.title = b;
        this.notice=bod;
    
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
    }
    
    public int getCount() {
        return title.size();
    }
    
    public Object getItem(int position) {
        return position;
    }
    
    public long getItemId(int position) {
        return position;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null)
            vi = inflater.inflate(R.layout.row_listitem, null);
    
        TextView title2 = (TextView) vi.findViewById(R.id.txt_ttlsm_row); // title
        String song = title.get(position).toString();
        title2.setText(song);
    
    
        TextView title22 = (TextView) vi.findViewById(R.id.txt_ttlcontact_row2); // notice
        String song2 = notice.get(position).toString();
        title22.setText(song2);
    
    return vi;
    
    }
    

    }

    Now, u can set up your main activity like:

    public class MainActivity extends Activity {
    
    ArrayList title_array = new ArrayList();
    ArrayList notice_array = new ArrayList();
    ListView list;
    BaseAdapter2 adapter;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = (ListView) findViewById(R.id.listView1);
        new TheTask().execute();
    }
    
    class TheTask extends AsyncTask {
    
        @Override
        protected String doInBackground(Void... params) {
            String str = null;
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(
                        "http://10.0.2.2/BSDI/show.php");
                HttpResponse response = httpclient.execute(httppost);
                str = EntityUtils.toString(response.getEntity());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            return str;
    
        }
    
    
    
    @Override
    protected void onPostExecute(String result) {
    
        super.onPostExecute(result);
    
        String response = result.toString();
        try {
    
    
            JSONArray new_array = new JSONArray(response);
    
            for (int i = 0, count = new_array.length(); i < count; i++) {
                try {
                    JSONObject jsonObject = new_array.getJSONObject(i);
                    title_array.add(jsonObject.getString("title").toString());
                    notice_array.add(jsonObject.getString("notice").toString());
    
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
    
            adapter = new BaseAdapter2(MainActivity.this, title_array, notice_array);
            list.setAdapter(adapter);
    
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            // tv.setText("error2");
        }
    
    }
    }
    
     }
    

提交回复
热议问题