Android Chat-App ListView无法正常工作

时间:2012-03-28 11:22:49

标签: android eclipse web-services chat

我有以下代码

package cyclist.project;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import cyclist.Utility.AppLog;
import cyclist.Utility.common;
import cyclist.Utility.webService;
import cyclist.bean.discussion_item;
import cyclist.dal.Attributes;
import cyclist.dal.DBHelper;
import cyclist.dal.DBService;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class user_discussion  extends Activity{

    ListView events;
    ArrayList<discussion_item> list;
    static EditText et;
    Button btn; 

    webService webService;
    cyclist.Utility.common common;
    DBService dbService;
    DBHelper dbHelper;
    Attributes attributes;

    public void onCreate(Bundle savedInstanceState) {

        webService = new cyclist.Utility.webService(this);
        common = new common();
        dbHelper = new DBHelper(this);
        dbService = new DBService(dbHelper);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_discussion);
        events = (ListView) findViewById(R.id.lv_user_discussion_list);
        et = (EditText) findViewById(R.id.lv_list_events_listT);
        btn = (Button) findViewById(R.id.lv_list_events_list);
        list = new ArrayList<discussion_item>();

        showTime();
        setList();

        events.setAdapter( new ListAdapter(user_discussion.this, list) );
        // new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list)
    }

    public void register(View view) {
        Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER");
        intent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
        intent.putExtra("sender", "myemail@gmail.com");
        startService(intent);
        et.setText("");
    }

    public void showTime() {
//      ListAdapter la = new ListAdapter(user_discussion.this, list);
//      la.notifyDataSetChanged();
//      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
//      String string = prefs.getString(AUTH, "n/a");
//      Toast.makeText(this, string, Toast.LENGTH_LONG).show();
//      Log.d("C2DM RegId", string);

    btn.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            String url = common.setChatInUrl(et.getText().toString() );
            AppLog.logString("url= "+ url);
            String response = webService.getWebResponse(url);               
            setList();
            et.setText("");
            // register(btn);
            }
        });
    }

    private void setList(){
        JSONObject json;
        try {
            json = new JSONObject(JSON.getJSONfromURL("http://myservices/ws_cyc/events_service.php?action=get_chats"));
            JSONArray miles = json.getJSONArray("cyc");
            JSONObject rec;
            JSONObject rec2 = null; 
            discussion_item item = null;

                for (int i=0; i<miles.length(); i++){
                    rec = miles.getJSONObject(i);
                    rec2 = rec.getJSONObject("cycObject");
                    item = new discussion_item(rec2.getString("Chats").toString());
                    list.add(item);
                }                                   
            } catch (JSONException e) {
                e.printStackTrace();
            }   
    }

    @SuppressWarnings("unused")
    private class ListAdapter extends ArrayAdapter<discussion_item> {

        private LayoutInflater inflater;

        public ListAdapter( Context context, List<discussion_item> list ) {
          super( context, R.layout.user_discussion_left_item, R.id.txt_event_item_title, list );
          inflater = LayoutInflater.from(context);       // Cache the LayoutInflate to avoid asking for a new one each time.
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            ViewHolder holder;
//          TextView textView;          
            discussion_item event = (discussion_item) this.getItem(position); 
            AppLog.logString(String.valueOf(position));
            holder = new ViewHolder();

            if ( convertView == null ) {
                if(position%2 == 0){
                    convertView = inflater.inflate(R.layout.user_discussion_left_item, null);
                    holder.textView = (TextView) convertView.findViewById( R.id.txt_event_item_title); 
                    holder.textView.setText(event.getTitle());
                }
                else{
                    convertView = inflater.inflate(R.layout.user_discussion_right_item, null);
                    holder.textView = (TextView) convertView.findViewById( R.id.txt_event_item_title); 
                    holder.textView.setText(event.getTitle());
                }                   
            }
            return convertView;         
        }
      }

     public void NavigateToResult(Context context, Class<?> targetClass) {
            Intent intent = new Intent(context, targetClass);
            startActivity(intent);
            finish();
        }
        @Override 
        public boolean onKeyDown(int keyCode, KeyEvent event)  
        { 

             if (keyCode == KeyEvent.KEYCODE_BACK) 
             {
                 NavigateToResult(this, login.class);
                    return false;           
             }  
          else
              return super.onKeyDown(keyCode, event); 


        }
        static class ViewHolder
        {
            public TextView textView;
            //ImageView imgIcon;
        }
}

我正在使用PHP JSON Web Service来检索聊天列表。问题是我的自定义列表没有正确显示,即它只显示4行聊天

0 - 你好 1 - 你好 2 - 怎么茹? 3 - 很好。

其余部分未显示,即

4 - 今天的计划是什么 5 - 没什么 6 ......等等...

而不是显示4-on,它重复那些0-3索引聊天。 请帮忙。我哪里弄错了? 当我通过在Web浏览器中运行来调试我的Web服务时,它将返回完整的聊天列表。

2 个答案:

答案 0 :(得分:0)

@ Ollie C. Thanx指出了这个事实......然而另一个用户@ code.google.com帮助我解决了我的问题..我问的唯一问题是我是否犯了错误..它是模糊的,但Genius告诉我我应该重新检查我的 ListAdapter类

事实证明我在 getView()方法中犯了错误。

再次感谢您的建议。

答案 1 :(得分:0)

每次循环开始时都需要获取每个对象 如果你创建JSONObject rec;外部循环出现此问题

使用:

 for(int i=0; i<miles.length(); i++){
               JSONObject  rec = miles.getJSONObject(i);
               JSONObject rec2 = rec.getJSONObject("cycObject");
               item = new discussion_item(rec2.getString("Chats").toString());
               list.add(item);

             }