how to display fetched json data into listview using baseadapter

前端 未结 7 826
不知归路
不知归路 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:47

    @Allen Chun , actually its not in my mind currently that what I have changed in my code to run my code perfectly. But I am sharing all my codes which are working perfectly.

    Its my layout code,

    
    
    
    
    
       
    
    
    
    
    
    
    
    

    This is my custom listview layout codes,

    
    
    
    
        />
    
    
    
    
    

    its my activity named "NoticeBoard" .

    public class NoticeBoard extends Activity {
    
    
    ArrayList title_array = new ArrayList();
    ArrayList notice_array = new ArrayList();
    
    ListView list;
    base_adapter3 adapter;
     TextView f,msg;
    
    
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_notice);
        list = (ListView) findViewById(R.id.list_notice2);
        msg=(TextView) findViewById(R.id.err_msg);
        new test_ays().execute();
    }
    
    
    
    
    class test_ays 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);
        if(result!=null) // add this
        {  
         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 base_adapter3(NoticeBoard.this, title_array, notice_array);
          list.setAdapter(adapter);
           // f=(TextView) findViewById(R.id.textTuh);
           // f.setText(title_array);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
           e.printStackTrace();
    
            // tv.setText("error2");
        }
        }
    
        else{
            msg.setText("You need a working data connection...");
        }
    
    
    }
    }
    
    }
    

    And its my custom baseadapter codes,

    public class base_adapter2 extends BaseAdapter {
    
    
        private Activity activity;
        //private ArrayList<HashMap<String, String>> data;
        private static ArrayList title,notice;
        private static LayoutInflater inflater = null;
    
        public base_adapter2(Activity a, ArrayList b) {
          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.bsdi_adapter, null);
    
    
          TextView title2 = (TextView) vi.findViewById(R.id.txt1); // title
          String song = title.get(position).toString();
          title2.setText(song);
    
    
    
        return vi;
    
        }
    
    }
    

提交回复
热议问题