自定义列表视图中的滚动问题

4

我正在开发一个关于CustomListView的安卓应用程序,首先我会从网络服务中解析数据,然后将这些数据添加到ListView中,但是当我向下拉动scrollView时,我遇到了以下错误:

at android.widget.ListView.makeAndAddView(ListView.java:1792)
at android.widget.ListView.fillDown(ListView.java:676)
at android.widget.ListView.fillGap(ListView.java:640)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4911)
at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:2923)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:3234)
at android.view.View.dispatchTouchEvent(View.java:5553)

主活动:

public class MainActivity extends Activity implements OnRefreshListener {
    ListView list;
    String[] web;
    Integer[] imageId = { R.drawable.enveloppe, R.drawable.enveloppe,
            R.drawable.enveloppe, R.drawable.enveloppe, R.drawable.enveloppe,
            R.drawable.enveloppe, R.drawable.enveloppe

    };

    SwipeRefreshLayout swipeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list = (ListView) findViewById(R.id.list);

        web = new String[13];

        swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);

        AsyncCallBanner banner = new AsyncCallBanner();
        // Call execute

        banner.execute();

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Toast.makeText(MainActivity.this,
                        "You Clicked at " + web[+position], Toast.LENGTH_SHORT)
                        .show();

            }
        });

        // TODO Auto-generated catch block

        // TODO Auto-generated catch block

    }

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeLayout.setRefreshing(false);
            }
        }, 5000);
    }

    // ////////// for Parsing data
    private class AsyncCallBanner extends AsyncTask<Void, Void, String> {

        ProgressDialog dialog;

        @Override
        protected String doInBackground(Void... params) {
            Log.i("TAG", "doInBackground");
            Connect();
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            Log.i("TAG", "onPostExecute");
            // txt1.setText(Icerik);

            CustomList adapter = new CustomList(MainActivity.this, web, imageId);

            list.setAdapter(adapter);

            dialog.dismiss();
        }

        @Override
        protected void onPreExecute() {
            Log.i("TAG", "onPreExecute");
            dialog = new ProgressDialog(MainActivity.this);
            dialog.setMessage("Lütfen bekleyiniz");

            dialog.setIndeterminate(true);

            dialog.show();
        }

    }

    private void Connect() {
        // TODO Auto-generated method stub

        JSONObject returndata = null;
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(
                "http://api.androidhive.info/contacts/");
        httppost.setHeader("Content-type", "application/json");
        JSONObject jsonparameter = new JSONObject();

        try {

            // jsonparameter.put("AnketID", "3");

            httppost.setEntity(new StringEntity(jsonparameter.toString(),
                    "UTF-8"));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            String responseString = EntityUtils.toString(entity);

            Log.i("@banner_responseString", "" + responseString);

            try {
                returndata = new JSONObject(responseString);
                JSONArray jsonMainNode = returndata.optJSONArray("contacts");
                int lengthJsonArr = jsonMainNode.length();
                Log.i("@lengthJson", "" + lengthJsonArr);
                int sorusayisi = lengthJsonArr;

                for (int i = 0; i < sorusayisi; i++) {

                    if (i == sorusayisi)
                        break;
                    JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);

                    try {

                        web[i] = jsonChildNode.optString("name").toString(); // URL

                    } catch (Exception e) {

                    }
                }

            } catch (JSONException e) {

            }

        } catch (Exception e) {

        }

    }

}

自定义类:

public class CustomList extends ArrayAdapter<String> {

    private final Activity context;
    private final String[] web;
    private final Integer[] imageId;

    public CustomList(Activity context, String[] web, Integer[] imageId) {
        super(context, R.layout.list_single, web);
        this.context = context;
        this.web = web;
        this.imageId = imageId;

    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.list_single, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);

        ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
        txtTitle.setText(web[position]);

        imageView.setImageResource(imageId[position]);
        return rowView;
    }
}

你应该使用由getView()方法返回的视图来填充你的项目布局。 - Fatih Santalu
我尝试了,但是仍然得到了相同的错误。 public View getView(int position, View view, ViewGroup parent) { LayoutInflater inflater = context.getLayoutInflater(); view = inflater.inflate(R.layout.list_single, null, true); TextView txtTitle = (TextView) view.findViewById(R.id.txt); ImageView imageView = (ImageView) view.findViewById(R.id.img); txtTitle.setText(web[position]); imageView.setImageResource(imageId[position]); return view; } - Diego
view = inflater.inflate(R.layout.list_single, parent, false); 视图 = inflater.inflate(R.layout.list_single, parent, false); - Fatih Santalu
@santalu,我尝试了,但它不起作用。 - Diego
1个回答

0
请将您的getView函数更改如下并进行测试,然后给我反馈。
 @Override
    public View getView(int position, View view, ViewGroup parent) {
TextView txtTitle=null;
ImageView imageView=null;
 LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (view== null) {
                view= mInflater.inflate(R.layout.list_single, null);

        txtTitle = (TextView) view.findViewById(R.id.txt);

        imageView = (ImageView) view.findViewById(R.id.img);
 }
        txtTitle.setText(web[position]);

        imageView.setImageResource(imageId[position]);
        return view;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接