gpt4 book ai didi

android - CustomListView 中的向下滚动问题

转载 作者:太空宇宙 更新时间:2023-11-03 11:09:34 27 4
gpt4 key购买 nike

我正在开发关于 CustomListView 的 Android 应用程序,首先我从 Web 服务解析数据然后我将这些数据添加到 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)

主要 Activity :

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 函数并测试它并反馈给我

 @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;
}

关于android - CustomListView 中的向下滚动问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28187301/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com