如何制作像Facebook这样的聊天应用?

时间:2013-12-30 23:27:41

标签: android json

我正在开发一款聊天应用。我想为我的listview创建一个很酷的设计。我的代码正在运行,但我想添加一些类似于Facebook设计的设计,但我有一个问题。

我不能这样做:即一个用户ID = 52而另一个用户= 5293。如果用户有Id = 52,则左边的textarea引力和右边的另一个,这里我的代码它没有看到我的if语句每次打印else语句我不知道为什么我真的把我的ID在数组中,但实际上这是相同的结果。

   public void LoadMessage() {

            // TODO Auto-generated method stub
            DatabseHandler d = new DatabseHandler(EchangingMessage.this);
            String me = d.getData();

            TextView you_id = (TextView) findViewById(R.id.you_id);

        String you = you_id.getText().toString().trim();

        Log.i("ID  :", me + " : " + you);

        List<NameValuePair> params = new ArrayList<NameValuePair>();

        params.add(new BasicNameValuePair("me", me));
        params.add(new BasicNameValuePair("you", you));

        JSONObject json = jParser.makeHttpRequest(url_daftar_rs, "POST",
                params);

        try {

            int success = json.getInt(TAG_SUCCESS);
            Log.i("success :", "" + success);

            if (success == 1) {

                daftar_rs = json.getJSONArray(TAG_DAFTAR_RS);
                for (int i = 0; i < daftar_rs.length(); i++) {
                    JSONObject c = daftar_rs.getJSONObject(i);
                    String id_rs = c.getString(TAG_ID_RS);
                    String nama_rs = c.getString(TAG_NAMA_RS);
                    String link_image_rs = "http://10.0.2.2/www/Android_Login_Secure/Images/upload/big/"
                            + c.getString(TAG_LINK_IMAGE_RS);


                    String message_rs = c.getString(TAG_MESSAGE_RS);
                    String time_rs = c.getString(TAG_TIME_RS);

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

                    map.put(TAG_ID_RS, id_rs);
                    map.put(TAG_NAMA_RS, nama_rs);
                    map.put(TAG_LINK_IMAGE_RS, link_image_rs);
                    map.put(TAG_MESSAGE_RS, message_rs);
                    map.put(TAG_TIME_RS, time_rs);
                    DaftarRS.add(map);

                }
            } else {
                    finish();
            }
        } catch (Exception e) {
            Log.e("Error", "COnnection:" + e.toString());
        }

        runOnUiThread(new Runnable() { 
            public void run() {
                // updating listview 
                SetListViewAdapter(DaftarRS);
                } 
            });
    }


public class ListAdapterSendMessage extends BaseAdapter {

public String POST_TEXT;
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
public final static String you_id = null;

int count = 0;

public ListAdapterSendMessage(Activity a,
        ArrayList<HashMap<String, String>> d) {
    activity = a;
    data = d;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader = new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    // TODO Auto-generated method stub
    return data.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.item_list_send, null);
    }

    HashMap<String, String> daftar_rs = new HashMap<String, String>();
    daftar_rs = data.get(position);

    String me = daftar_rs.get(MyMessages.TAG_LINK_IMAGE_RS);

    if (me == "52") {
        vi = inflater.inflate(R.layout.list_row_even, null);

        TextView name_rs = (TextView) vi.findViewById(R.id.senderMessage);

        TextView Des_rs = (TextView) vi.findViewById(R.id.message);
        ImageView thumb_image = (ImageView) vi
                .findViewById(R.id.imageSender);
        TextView Time_rs = (TextView) vi.findViewById(R.id.senderTime);

        name_rs.setText(daftar_rs.get(MyMessages.TAG_NAMA_RS));
        // link_image_rs.setText(daftar_rs.get(MainActivity.TAG_LINK_IMAGE_RS));
        // alamat_rs.setText(daftar_rs.get(MainActivity.TAG_ALAMAT_RS));
        Des_rs.setText(daftar_rs.get(MyMessages.TAG_MESSAGE_RS));
        Time_rs.setText(daftar_rs.get(MyMessages.TAG_TIME_RS));
        imageLoader.DisplayImage(
                daftar_rs.get(MyMessages.TAG_LINK_IMAGE_RS), thumb_image);

    } else {


        vi = inflater.inflate(R.layout.list_row_odd, null);

        TextView name_rs = (TextView) vi.findViewById(R.id.senderMessage);

        TextView Des_rs = (TextView) vi.findViewById(R.id.message);
        ImageView thumb_image = (ImageView) vi
                .findViewById(R.id.imageSender);
        TextView Time_rs = (TextView) vi.findViewById(R.id.senderTime);

        name_rs.setText(daftar_rs.get(MyMessages.TAG_NAMA_RS));
        // link_image_rs.setText(daftar_rs.get(MainActivity.TAG_LINK_IMAGE_RS));
        // alamat_rs.setText(daftar_rs.get(MainActivity.TAG_ALAMAT_RS));
        Des_rs.setText(daftar_rs.get(MyMessages.TAG_MESSAGE_RS));
        Time_rs.setText(daftar_rs.get(MyMessages.TAG_TIME_RS));
        imageLoader.DisplayImage(
                daftar_rs.get(MyMessages.TAG_LINK_IMAGE_RS), thumb_image);

    }

    return vi;

}
}

2 个答案:

答案 0 :(得分:3)

对于这个问题,我们假设我的id = 1而你id = 2

if (id==1) {
     inflater.inflate(R.layout.item_list_right, parent, false);

else
{
      inflater.inflate(R.layout.item_list_left, parent, false);
}

这里你需要不同的布局,其中一个是item_list_right,另一个是item_list_left 我像这样解决了这个问题

答案 1 :(得分:0)

观看this 示例,我认为它会对您有所帮助。