Java Android ListView-某些元素的位置错误
我使用AsyncTask解析JSON并在ListView中加载(文本、图像和其他),但加载后的一些元素位置错误或重复。如何修复它 例如: 一, 二, 二, 三, 四, 四, 代码异步任务:Java Android ListView-某些元素的位置错误,java,android,listview,android-asynctask,Java,Android,Listview,Android Asynctask,我使用AsyncTask解析JSON并在ListView中加载(文本、图像和其他),但加载后的一些元素位置错误或重复。如何修复它 例如: 一, 二, 二, 三, 四, 四, 代码异步任务: public class AsyncTaskParseJson extends AsyncTask<String, String, String> { final String TAG = "AsyncTaskParseJson.java"; // contac
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
final String TAG = "AsyncTaskParseJson.java";
// contacts JSONArray
JSONArray dataJsonArr = null;
@Override
protected void onPreExecute() {}
@Override
protected String doInBackground(String... arg0) {
try {
// instantiate our json parser
JsonParser jParser = new JsonParser();
// get json string from url
JSONObject json = jParser.getJSONFromUrl(URL);
// get the array of users
dataJsonArr = json.getJSONArray("data");
// loop through all users
for (int i = 0; i < dataJsonArr.length(); i++) {
JSONObject c = dataJsonArr.getJSONObject(i);
// Storing each json item in variable
String nickname = c.getString("nickname");
products.add(new Product(nickname));
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String strFromDoInBg) { SetAdapter(); }
}
公共类AsyncTaskParseJson扩展AsyncTask{
final String TAG=“AsyncTaskParseJson.java”;
//联系JSONArray
JSONArray dataJsonArr=null;
@凌驾
受保护的void onPreExecute(){}
@凌驾
受保护的字符串doInBackground(字符串…arg0){
试一试{
//实例化我们的json解析器
JsonParser jParser=新的JsonParser();
//从url获取json字符串
JSONObject json=jParser.getJSONFromUrl(URL);
//获取用户数组
dataJsonArr=json.getJSONArray(“数据”);
//遍历所有用户
for(int i=0;i
我使用简单的BaseAdapter,代码:
public class BoxAdapter extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<Product> objects;
String name, rating, desc, date;
long date_time;
Integer rate;
ImageView imageID;
TextView textDesc;
BoxAdapter(Context context, ArrayList<Product> products) {
ctx = context;
objects = products;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return objects.size();
}
@Override
public Object getItem(int position) {
return objects.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.item, parent, false);
}
Product p = getProduct(position);
textDesc = (TextView) view.findViewById(R.id.desc);
if (p.username.contains("null"))
{
name = "Автор: Неизвестен";
}
else
{
name = "Автор: " + p.username;
}
if(!p.description.contains("null"))
{
desc = p.description.replaceAll("<br />", "");
desc = desc.replaceAll(""", "");
}
date_time = Long.parseLong(p.created_at);
Date dates = new Date(date_time*1000L);
SimpleDateFormat time = new SimpleDateFormat("dd.MM в HH:mm");
time.setTimeZone(TimeZone.getTimeZone("UTC+4"));
date = time.format(dates);
((TextView) view.findViewById(R.id.name)).setText(name);
((TextView) view.findViewById(R.id.rating)).setText(p.rating);
((TextView) view.findViewById(R.id.desc)).setText(desc);
((TextView) view.findViewById(R.id.date)).setText(date);
return view;
}
Product getProduct(int position) {
return ((Product) getItem(position));
}
}
公共类BoxAdapter扩展了BaseAdapter{
上下文ctx;
拉平机;
阵列列表对象;
字符串名称、等级、描述、日期;
长时间;
整数利率;
ImageView-imageID;
TextView textDesc;
BoxAdapter(上下文、ArrayList产品){
ctx=上下文;
对象=产品;
lInflater=(LayoutInflater)ctx
.getSystemService(上下文布局\充气机\服务);
}
@凌驾
public int getCount(){
返回objects.size();
}
@凌驾
公共对象getItem(int位置){
返回对象。获取(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图=转换视图;
如果(视图==null){
视图=lInflater.充气(R.layout.item,父项,false);
}
产品p=getProduct(位置);
textDesc=(TextView)view.findViewById(R.id.desc);
如果(p.username.contains(“null”))
{
name=“Аццззцц”;
}
其他的
{
name=“Ацц”:+p.username;
}
如果(!p.description.contains(“null”))
{
desc=p.description.replaceAll(“
,”);
desc=desc.replaceAll(“”,“”);
}
date\u time=Long.parseLong(p.created\u at);
日期日期=新日期(日期时间*1000L);
SimpleDateFormat时间=新SimpleDateFormat(“dd.MMöHH:MM”);
time.setTimeZone(TimeZone.getTimeZone(“UTC+4”));
日期=时间。格式(日期);
((TextView)view.findviewbyd(R.id.name)).setText(name);
((TextView)view.findviewbyd(R.id.rating)).setText(p.rating);
((TextView)view.findviewbyd(R.id.desc)).setText(desc);
((TextView)view.findviewbyd(R.id.date)).setText(date);
返回视图;
}
Product getProduct(内部位置){
返回((产品)获取项目(位置));
}
}
你有两个getitem函数,你可以删除其中一个为什么你使用两个getitem方法只使用一个方法来占据listview的位置。我删除了,但这并不能解决我的问题(