0

已编辑 如果有人遇到同样的问题,我编辑了代码,现在正在工作 首先感谢到目前为止帮助过我的所有人,我是 android 和 java 开发的新手,我在适配器和自定义行方面遇到了非常糟糕的时间。

好的,我已经根据提供的示例更改了代码,现在如下

public class EveryonesDiaries extends ListActivity {

private ProgressDialog m_ProgressDialog = null;
private ArrayList<Every> m_every = null;
private EveryAdapter m_adapter = null;
private Runnable viewEvery;
private String strcline;


SharedPreferences pref;
String userid = "";
String pageNum = "";
String maxNum = "";

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

    m_every = new ArrayList<Every>();
    this.m_adapter = new EveryAdapter(this, R.layout.cellevery, m_every);
    setListAdapter(this.m_adapter);

    pref = getSharedPreferences("pref", MODE_PRIVATE);
    String userid = pref.getString("userid","");
    pageNum = "0";
    maxNum = "3";

    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

    StringBuilder data = new StringBuilder();
    data.append("--0xKhTmLbOuNdArY\r\n");
    data.append("Content-Disposition: form-data; name=\"userid\"\r\n\r\n");
    data.append(userid);
    data.append("\r\n--0xKhTmLbOuNdArY\r\n");
    data.append("Content-Disposition: form-data; name=\"pagenum\"\r\n\r\n");
    data.append(pageNum);
    data.append("\r\n--0xKhTmLbOuNdArY\r\n");
    data.append("Content-Disposition: form-data; name=\"maxnum\"\r\n\r\n");
    data.append(maxNum);
    data.append("\r\n--0xKhTmLbOuNdArY\r\n");
    String sendData = data.toString();

    try{
        String usedServer = Common.diaryHistory();
        strcline = Common.connect(sendData, usedServer);
        Log.d("string", strcline);

    }catch(Exception e){        
        e.printStackTrace();
        alertDialog.setTitle(R.string.title_error);
        alertDialog.setMessage(R.string.connection_error);
        alertDialog.setPositiveButton(R.string.ok_button, null);
        alertDialog.show();
    }

    viewEvery = new Runnable(){
        @Override
        public void run() {
            getEvery();
        }
    };

    Thread thread =  new Thread(null, viewEvery, "MagentoBackground");
    thread.start();
    m_ProgressDialog = ProgressDialog.show(EveryonesDiaries.this,     
          "Please wait...", "Retrieving data ...", true);
}

private Runnable returnRes = new Runnable() {

    @Override
    public void run() {
        Log.d("a3", ""+ m_every.size());
        if(m_every != null && m_every.size() > 0){
            m_adapter.notifyDataSetChanged();
            for(int i=0;i<m_every.size();i++)
            m_adapter.add(m_every.get(i));
        }
        m_ProgressDialog.dismiss();
        m_adapter.notifyDataSetChanged();
    }
};

private void getEvery(){

    try{
        //start to work the JSON array
        JSONArray a1 = new JSONArray(strcline);

        //Start decoding the first cycle of the JSON array
        for(int i=0; i < a1.length(); i++){
            JSONArray a2 =  a1.getJSONArray(i);
            Log.d("a1", String.valueOf(a2));
            String userName = a2.getString(1);
            String diaryDate = a2.getString(4);
            Log.d("a2", userName);
            Log.d("a2", diaryDate);
            Every e1 = new Every();
            e1.setUserName(userName);
            e1.setDiaryDate(diaryDate);
            m_every.add(e1);
            Thread.sleep(5000);
            Log.i("a2", ""+ m_every.size());
        }
    }catch(Exception e){
        Log.e("BACKGROUND_PROC", e.getMessage());
        }
    runOnUiThread(returnRes);
        }

使用如下适配器:

private class EveryAdapter extends ArrayAdapter<Every> {

        private ArrayList<Every> items;

        public EveryAdapter(Context context, int textViewResourceId,      ArrayList<Every> items) {
                super(context, textViewResourceId, items);
                this.items = items;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.cellevery, null);
                }
                Every e = items.get(position);
                Log.e("a2", String.valueOf(items));
                if(e != null){
                    TextView cName = (TextView) v.findViewById(R.id.cellName);
                    TextView cDate = (TextView) v.findViewById(R.id.cellDate);
                    if(cName != null){
                        cName.setText(e.getUserName());
                    if(cDate != null){
                        cDate.setText(e.getDiaryDate());
                    }
                }

                }
                return v;
        }
 }

编辑 它缺少部分代码 *m_every = new ArrayList();* 它开始填充字段,但在第一个字段上停止,并继续显示带有“检索数据...”的可运行文件,直到它用完内存崩溃。

我认为我的问题出在这部分代码上,但我无法弄清楚。请如果有人可以帮助我理解。

private void getEvery(){

    try{
        //start to work the JSON array
        JSONArray a1 = new JSONArray(strcline);

                     // this part was missing
                     m_every = new ArrayList<Every>();

        //Start decoding the first cycle of the JSON array
        for(int i=0; i < a1.length(); i++){
            JSONArray a2 =  a1.getJSONArray(i);
            Log.d("a1", String.valueOf(a2));
            String userName = a2.getString(1);
            String diaryDate = a2.getString(4);
            Log.d("a2", userName);
            Log.d("a2", diaryDate);
            Every e1 = new Every();
            e1.setUserName(userName);
            e1.setDiaryDate(diaryDate);
            m_every.add(e1);
            Thread.sleep(5000);
            Log.i("a2", ""+ m_every.size());
        }
    }catch(Exception e){
        Log.e("BACKGROUND_PROC", e.getMessage());
        }
    runOnUiThread(returnRes);
        }
4

1 回答 1

0

Android 使用 Java Servlet HTTP 请求从 JSON 数据填充 ListView

http://www.mysamplecode.com/2012/07/android-populate-listview-from-json.html

嘿检查下面的链接列表视图。

http://devtut.wordpress.com/2011/06/09/custom-arrayadapter-for-a-listview-android/


http://windrealm.org/tutorials/android/android-listview.php

在此处自定义行链接

http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/

http://android-er.blogspot.in/2010/06/custom-arrayadapter-with-with-different.html


http://theopentutorials.com/tutorials/android/listview/android-custom-listview-with-image-and-text-using-arrayadapter/
于 2012-11-26T07:29:02.303 回答