如何在Listview行中插入语音气泡?

时间:2015-04-26 15:34:44

标签: android listview

我是构建Android应用程序的新手,有很多我不知道的事情。我一直在寻找几个小时,关于如何在我的TextViews programmaticaly中插入Speech Bubbles,我找不到任何可以帮助我的东西,因为我使用Json文件从我的数据库中获取数据,我正在使用Java打印所有数据。

我试图获取我的listview行布局,并在那里插入可绘制的图像,但它没有用。

我想找一个好的教程,解释如何在我的情况下这样做。

继承我的代码。

public class ChatRoomActivity extends ListActivity {

    private ProgressDialog pDialog;
    private ImageView img;
    private TextView text;

    // URL to get contacts JSON

    private static String url = "http://192.168.1.67/example/chat_room.php";

    // JSON Node names
    private static final String TAG_CONTACTS = "contacts";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "username";
    private static final String TAG_MESSAGE = "reply";
    private static final String TAG_IMAGE = "pic_url";
    private static final String TAG_USERID = "user_id";

    // contacts JSONArray
    JSONArray contacts = null;

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> contactList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chatlist);

        Intent intent = getIntent();
        String id = intent.getStringExtra("teste");

        // Toolbar links
        View myLayout = findViewById( R.id.include1 ); // root View id from that link
        View myView = myLayout.findViewById( R.id.toolbar_title ); // id of a view contained in the included file
        View myView2 = myLayout.findViewById( R.id.toolbar_title2 );

         myView.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {

                NewSession app = (NewSession) getApplication();
                String username = app.getUsername();

                Intent in = new Intent(getApplicationContext(),
                                       UserProfile.class);

                in.putExtra("teste", username);

                startActivity(in);
             }
         }); 


        contactList = new ArrayList<HashMap<String, String>>();
        ListView lv = getListView();


        // Listview on item click listener
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
            }
        });

        // Calling async task to get json
        new GetContacts().execute(id);
    }


    /**
     * Async task class to get json by making HTTP call
     * */
    private class GetContacts extends AsyncTask<String, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            // Showing progress dialog
            pDialog = new ProgressDialog(ChatRoomActivity.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();
        }

        protected Void doInBackground(String... arg0) {


            NewSession app = (NewSession) getApplication();
            String username = app.getUsername();
            String getinfo = (String)arg0[0];
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url + "?id=" + username + "&id2=" + getinfo, ServiceHandler.GET);

            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    // Getting JSON Array node
                    contacts = jsonObj.getJSONArray(TAG_CONTACTS);

                    // looping through All 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_NAME);
                        String userid = c.getString(TAG_USERID);
                        String message = c.getString(TAG_MESSAGE);
                        String pic_url_get = c.getString(TAG_IMAGE);

                        HashMap<String, String> contact = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        contact.put(TAG_ID, id);
                        contact.put(TAG_MESSAGE, message);
                        contact.put(TAG_NAME, name + ", " + name);
                        contact.put(TAG_IMAGE, "http://www.*****.com/images/" + userid + "/" + pic_url_get);


                        // adding contact to contact list
                        contactList.add(contact);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);


            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            /**
             * Updating parsed JSON data into ListView
             * */


            costumeadapter adapter =  new costumeadapter(
                            ChatRoomActivity.this,
                            contactList,
                            R.layout.chat_row,
                            new String[]{ TAG_MESSAGE },
                            new int[]{ R.id.name });

            setListAdapter(adapter);

        }

    }

    //Costume adapter
    public class costumeadapter extends SimpleAdapter{

    public costumeadapter(ChatRoomActivity chatRoomActivity,
                ArrayList<HashMap<String, String>> contactList, int listItem,
                String[] strings, int[] is) {
        super(chatRoomActivity, contactList, listItem, strings, is);
            // TODO Auto-generated constructor stub
        }


    //User's avatar

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

          // here you let SimpleAdapter built the view normally.
          View v = super.getView(position, convertView, parent);

          // Then we get reference for Picasso

          if(img == null){
             img = (ImageView) v.findViewById(R.id.list_image);
             v.setTag(img); // <<< THIS LINE !!!!
          }
          // get the url from the data you passed to the `Map`


          String url = (String) ((Map)getItem(position)).get(TAG_IMAGE);
          // do Picasso
          Picasso.with(v.getContext()).load(url).into(img);
          //img.setTag(url);
          // return the view
          return v;

       }
    }


}

0 个答案:

没有答案