1

In my android project, I have a ListView which populates the data entered by the user on runtime. Along with this, I want to show some data from an existing JSON file. So I parsed this JSON file and stored it in a String. How can I show both of these in my application?Can I use same the Listview for both ?

This is my code

 // Reading text file from assets folder
            StringBuffer sb = new StringBuffer();
            BufferedReader br = null;
            try 
            {
                br = new BufferedReader(new InputStreamReader(getAssets().open(
                        "jsonshoutdata.txt")));
                String temp;
                while ((temp = br.readLine()) != null)
                    sb.append(temp);
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
            finally 
            {
                try 
                {
                    br.close(); // stop reading
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }

            String myjsonstring = sb.toString();

            // Try to parse JSON
            try {
                //urlist = new ArrayList<HashMap<String, String>>();
                // Creating JSONObject from String
                JSONObject jsonObjMain = new JSONObject(myjsonstring);

                // Creating JSONArray from JSONObject
                JSONArray jsonArray = jsonObjMain.getJSONArray("message");

                ArrayList<String> messages = new ArrayList<String>();
                for (int i = 0; i < jsonArray.length(); i++) {

                    // Creating JSONObject from JSONArray
                    JSONObject jsonObj = jsonArray.getJSONObject(i);

                    // Getting data from individual JSONObject
                    String message = jsonObj.getString("msg");
                    messages.add(message);

                }

This is my customList adapter for json data

customtest adapter = new customtest(Single.this,R.layout.list_single_shout_single,messages); 
            ListView list = (ListView)findViewById(R.id.list_shout_screen);
            list.setAdapter(adapter);
            list.setOnItemClickListener(new AdapterView.OnItemClickListener() 
            {

                 @Override
                        public void onItemClick(AdapterView<?> parent, View view,
                                                int position, long id) {
                            Toast.makeText(Single.this, "TEST.........", Toast.LENGTH_SHORT).show();

                        }
                    });

        }
            catch (JSONException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

This is my customList adapter which i already have

      final CustomListSingle adapter1 = new CustomListSingle(Single.this, web1,web,rounded,imageId2,imageId3,result1,result2);
             //CustomListSingle adapter = new CustomListSingle(Single.this, web1,web,rounded,imageId2,imageId3,result1,result2);
             list_shout_screen=(ListView)findViewById(R.id.list_shout_screen);
             list_shout_screen.setAdapter(adapter1);
             //list_shout_screen.setAdapter(adapter);
             list_shout_screen.setOnItemClickListener(new AdapterView.OnItemClickListener()
                {
                  @Override
                        public void onItemClick(AdapterView<?> parent, View view,
                                                int position, long id) 
                            {
                            TextView txtTitle = (TextView)findViewById(R.id.txt);
                            txtTitle.length();
                            Toast.makeText(Single.this, "Length is " + txtTitle.getMeasuredWidth(), Toast.LENGTH_SHORT).show();
                            txtTitle.getTextSize();
                            }
                }
                );

             //Button click activity......

                final EditText et = (EditText)findViewById(R.id.EditText1);
                final Button imb=(Button)findViewById(R.id.btn_send);
                imb.setOnClickListener(new OnClickListener()
                {
                 @Override
                 public void onClick(View arg0) 
                 { 
                    String str = et.getText().toString();
                      web1.add(str);
                      Toast.makeText(Single.this, "You entered...."+str, Toast.LENGTH_SHORT).show();
                      adapter1.notifyDataSetChanged();
                      scrollMyListViewToBottom();
                      et.setText(" ");

                        }

This is my adapter class

public CustomListSingle(Activity context,ArrayList<String>  web,ArrayList<String> web1,Bitmap rounded,Integer[] imageId2,Integer[] imageId3,Bitmap image1,Bitmap image) 
    {
        super(context, R.layout.list_single_shout_single, web);
        this.context = context;
        this.web = web;
        this.web1 = web1;
        //this.imageId1 = imageId1; 

        this.rounded=rounded;
        this.imageId2 = imageId2;
        this.imageId3 = imageId3;
        this.image=image;
        this.image1=image1;

    }



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

    LayoutInflater inflater = context.getLayoutInflater();
    View rowView= inflater.inflate(R.layout.list_single_shout_single, null, true);



    /*msg*/
    TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);

    //pic
    ImageView imageView1 = (ImageView) rowView.findViewById(R.id.img1);
    ImageView imageView3 = (ImageView) rowView.findViewById(R.id.img);

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.RIGHT;
            txtTitle.setLayoutParams(params);


    txtTitle.setText(web.get(position));

    txtTitle.setBackgroundResource(R.drawable.test);





    imageView1.setImageBitmap(rounded); 


    imageView3.setImageBitmap(image1);


    return rowView;
    }

}

Method for making images circular

public static Bitmap getCircularBitmapFrom(Bitmap bitmap) 
        {
        if (bitmap == null) {
            return null;
        }
        float radius = bitmap.getWidth() > bitmap.getHeight() ? ((float) bitmap
                .getHeight()) / 2f : ((float) bitmap.getWidth()) / 2f;
        if (radius < 0) {
            radius = 0;
        }
        Bitmap canvasBitmap = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        BitmapShader shader = new BitmapShader(bitmap, TileMode.CLAMP,
                TileMode.CLAMP);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setShader(shader);

        Canvas canvas = new Canvas(canvasBitmap);

        canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
                radius, paint);

        return canvasBitmap;
    }

And this

Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.white);
        Bitmap bmp1=BitmapFactory.decodeResource(getResources(), R.drawable.prof_pic_a);
        Bitmap images =  BitmapFactory.decodeResource(getResources
               (),R.drawable.prof_pic_a); 
        Bitmap rounded;

         rounded=getCircularBitmapFrom(images);
         result1=getCircularBitmapFrom(bmp);
         result2=getCircularBitmapFrom(bmp1);
2
  • I usually make a JSONArray out of the String and then extract JSONObjects from it. Commented Apr 1, 2014 at 7:50
  • 1
    Ya you can use same ListView to display both the data. Try adding both the data to the same ArrayList and use this array to your adapter. Commented Apr 1, 2014 at 7:51

1 Answer 1

1

suppose you have this arraylist

 ArrayList<String> messages = new ArrayList<String>();   //add this line on top so it will be accessed globally

and your button code

final Button imb=(Button)findViewById(R.id.btn_send);
            imb.setOnClickListener(new OnClickListener()
            {
             @Override
             public void onClick(View arg0) 
             { 
                String str = et.getText().toString();
                  messages.add(str);
                  Toast.makeText(Single.this, "You entered...."+str, Toast.LENGTH_SHORT).show();
                  adapter1.notifyDataSetChanged();
                  scrollMyListViewToBottom();
                  et.setText(" ");


                  adapter1.notifyDataSetChanged();  //list is the name of your listview


                    }

call notifyDataSetChanged whenever you want to update your listview(after adding data in your arraylist)

Sign up to request clarification or add additional context in comments.

2 Comments

Could u help me please ??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.