ListView数组越界,不显示哪行错误

给自己充充电 2016-09-25 10:02:54
package Adpater;

import java.security.acl.LastOwnerException;
import java.util.ArrayList;
import java.util.HashMap;

import com.gjr.myapp.R;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import models.ConmentInfo;
import models.ContentInfo;
import utils.BitMapUtil;

public class ConmentAdapter extends BaseAdapter {

private HashMap<Integer, Object> hmDatas;
private ArrayList<ConmentInfo> conmentInfos;
private ContentInfo contentInfo;
private String middleText;
private Context context;
private LayoutInflater mInflater;
public static final int TOP = 0x000001;
public static final int MIDDLE = 0x000002;
public static final int BOTTOM = 0x000003;
int count = 0;
public ConmentAdapter(Context context, HashMap<Integer, Object> hmDatas) {
// TODO Auto-generated constructor stub
super();
this.context = context;
this.contentInfo = (ContentInfo) hmDatas.get(TOP);
this.middleText = (String) hmDatas.get(MIDDLE);
this.conmentInfos = (ArrayList<ConmentInfo>) hmDatas.get(BOTTOM);

mInflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
Log.d("SJY", conmentInfos.size() + "count");
return conmentInfos.size() + 2;
}

@Override
public Object getItem(int position) {
int type = getItemViewType(position);
if (type == TOP) {
return contentInfo;
} else if (type == MIDDLE) {
return middleText;
}
Log.d("SJY", "getItem");
return conmentInfos.get(position-2);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public int getItemViewType(int position) {
if (position == 0) {
return TOP;
} else if (position == 1) {
return MIDDLE;
}
return BOTTOM;
}

@Override
public int getViewTypeCount() {
return 3;
}
ViewHolderTHEME viewHolderTHEME = null;
ViewHolderTitle viewHolderTitle = null;
ViewHolderConment viewHolderConment = null;
View view = null;
@Override
public View getView(int position, View convertView, ViewGroup parent) {

count++;
Log.d("SJY", count + "lll");

view = convertView;
int type = getItemViewType(position);
if (view == null) {
switch (type) {
case TOP:
view = mInflater.inflate(R.layout.content_item_layout, null);
viewHolderTHEME = new ViewHolderTHEME();
viewHolderTHEME.logoImage = (ImageView) view.findViewById(R.id.logoImage);
viewHolderTHEME.cardImage = (ImageView) view.findViewById(R.id.iv_cardImage);
viewHolderTHEME.userName = (TextView) view.findViewById(R.id.userName);
viewHolderTHEME.date = (TextView) view.findViewById(R.id.date);
viewHolderTHEME.content = (TextView) view.findViewById(R.id.content);
viewHolderTHEME.upNum = (TextView) view.findViewById(R.id.upNum);
viewHolderTHEME.downNum = (TextView) view.findViewById(R.id.downNum);
viewHolderTHEME.shareNum = (TextView) view.findViewById(R.id.shareNum);
viewHolderTHEME.conmentNum = (TextView) view.findViewById(R.id.conmentNum);
// viewHolderTHEME.upImageButton = (ImageButton) convertView.findViewById(R.id.upImage);
// viewHolderTHEME.downImageButton = (ImageButton) convertView.findViewById(R.id.downImage);
// viewHolderTHEME.conmentImageButton = (ImageButton) convertView.findViewById(R.id.contentImage);
// viewHolderTHEME.imageButtonListener = new
// ImageButtonListener(context, position, conmentInfos,
// viewHolderTHEME);
// viewHolderTHEME.upImageButton.setFocusable(false);
// viewHolderTHEME.downImageButton.setFocusable(false);
// viewHolderTHEME.conmentImageButton.setFocusable(false);
view.setTag(viewHolderTHEME);
break;
case MIDDLE:
view = mInflater.inflate(R.layout.coment_middletitle_layout, null);
viewHolderTitle = new ViewHolderTitle();
viewHolderTitle.tvTitle = (TextView) view.findViewById(R.id.tv_MiddleTitle);
view.setTag(viewHolderTitle);
break;
case BOTTOM:
view = mInflater.inflate(R.layout.conment_item_layout, null);
viewHolderConment = new ViewHolderConment();
viewHolderConment.logoImage = (ImageView) view.findViewById(R.id.logoImage);
viewHolderConment.userName = (TextView) view.findViewById(R.id.userName);
viewHolderConment.content = (TextView) view.findViewById(R.id.content);
viewHolderConment.date = (TextView) view.findViewById(R.id.tv_date);
view.setTag(viewHolderConment);
break;

}

}
else
{
switch (type) {
case TOP:
viewHolderTHEME = (ViewHolderTHEME) view.getTag();
break;
case MIDDLE:
viewHolderTitle = (ViewHolderTitle) view.getTag();
break;
case BOTTOM:
viewHolderConment = (ViewHolderConment) view.getTag();
break;
}

}
switch (type) {
case TOP:
// if (contentInfo.getCardIamge() != null) {
// viewHolderTHEME.cardImage.setImageBitmap(BitMapUtil.getBitmap(contentInfo.getCardIamge()));
// } else {
// viewHolderTHEME.cardImage.setImageBitmap(null);
// }
viewHolderTHEME.userName.setText(contentInfo.getUserName());
viewHolderTHEME.date.setText(contentInfo.getDate());
viewHolderTHEME.content.setText(contentInfo.getContent());
viewHolderTHEME.upNum.setText(contentInfo.getUpNum());
viewHolderTHEME.downNum.setText(contentInfo.getDownNum());
viewHolderTHEME.shareNum.setText(contentInfo.getShareNum());
viewHolderTHEME.conmentNum.setText(contentInfo.getConmentNum());
break;

case MIDDLE:
viewHolderTitle.tvTitle.setText(middleText);
break;
case BOTTOM:
Log.d("SJY", String.valueOf(position) + "bottom");
ConmentInfo conmentInfo = conmentInfos.get(position - 2);
// if(conmentInfo.getLogoImage() != null){
// viewHolderConment.logoImage.setBackground(conmentInfo.getLogoImage());
// }else{
// viewHolderConment.logoImage.setBackground(null);
// }
viewHolderConment.userName.setText(conmentInfo.getUserName());
viewHolderConment.content.setText(conmentInfo.getContent());
viewHolderConment.date.setText(conmentInfo.getDate());
break;
}

return view;
}

static class ViewHolderTHEME {
ImageView logoImage;
ImageView cardImage;
TextView userName;
TextView date;
TextView content;
TextView upNum;
TextView downNum;
TextView shareNum;
TextView conmentNum;
// ImageButton upImageButton;
// ImageButton downImageButton;
// ImageButton conmentImageButton;
// ImageButtonListener imageButtonListener;
// int position;
}

static class ViewHolderTitle {
TextView tvTitle;
}

static class ViewHolderConment {
ImageView logoImage;
TextView userName;
TextView content;
TextView date;
}

public void addConmentInfo(ConmentInfo conmentInfo){
conmentInfos.add(conmentInfo);
this.notifyDataSetChanged();
}


}


错误提示:




...全文
260 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
刘兆贤 2016-09-26
  • 打赏
  • 举报
回复
getitem多加了2
给自己充充电 2016-09-25
  • 打赏
  • 举报
回复
activity的代码:
package com.gjr.myapp;

import java.util.ArrayList;
import java.util.HashMap;

import Adpater.ConmentAdapter;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.hardware.input.InputManager;
import android.os.Bundle;
import android.provider.ContactsContract.DataUsageFeedback;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import models.ConmentInfo;
import models.ContentInfo;
import utils.BitMapUtil;
import utils.DBHelper;
import utils.DateUtil;

public class ConmentActivity extends Activity implements OnClickListener {

	private ImageView logoImage;
	private ImageView ivCardImage;
	private TextView userName;
	private TextView date;
	private TextView content;
	private TextView upNum;
	private TextView downNum;
	private TextView shareNum;
	private TextView conmentNum;
	private ListView conmentListView;
	private ArrayList<ConmentInfo> conmentInfos = new ArrayList<ConmentInfo>();
	private Button sendButton;
	private EditText conmentEditText;
	private ConmentAdapter conmentAdapter;
	private String user;
	private ContentInfo contentInfo;
	private Bitmap cardImage;
	private DBHelper dbHelper;
	private HashMap<Integer, Object> hmDatas = new HashMap<Integer, Object>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		dbHelper = new DBHelper(ConmentActivity.this);
		setContentView(R.layout.activity_conment);
		Bundle bundle = getIntent().getExtras();
		contentInfo = (ContentInfo) bundle.getSerializable("contentInfo");
		user = bundle.getString("userName", "zzzz");
//		logoImage = (ImageView) findViewById(R.id.logoImage);
//		ivCardImage = (ImageView) findViewById(R.id.iv_cardImage);
//		userName = (TextView) findViewById(R.id.userName);
//		date = (TextView) findViewById(R.id.date);
//		content = (TextView) findViewById(R.id.content);
//		upNum = (TextView) findViewById(R.id.upNum);
//		downNum = (TextView) findViewById(R.id.downNum);
//		shareNum = (TextView) findViewById(R.id.shareNum);
//		conmentNum = (TextView) findViewById(R.id.conmentNum);
//		if (contentInfo != null) {
//			userName.setText(contentInfo.getUserName());
//			date.setText(contentInfo.getDate());
//			content.setText(contentInfo.getContent());
//			upNum.setText(contentInfo.getUpNum());
//			downNum.setText(contentInfo.getDownNum());
//			shareNum.setText(contentInfo.getShareNum());
//			conmentNum.setText(contentInfo.getConmentNum());
//			if(contentInfo.getCardIamge() != null){
//            ivCardImage.setImageBitmap(BitMapUtil.getBitmap(contentInfo.getCardIamge()));
//			}
//		}
		
		hmDatas.put(ConmentAdapter.TOP, contentInfo);	
		hmDatas.put(ConmentAdapter.MIDDLE, new String("熱門評論"));
		hmDatas.put(ConmentAdapter.BOTTOM, conmentInfos);
		conmentListView = (ListView) findViewById(R.id.conmentListView);
		conmentAdapter = new ConmentAdapter(ConmentActivity.this, hmDatas);
		conmentListView.setAdapter(conmentAdapter);
        
		Cursor cursor = dbHelper.findAllConmentByName(contentInfo.getUserName());
		while(cursor.moveToNext()){
			ConmentInfo conmentInfo = new ConmentInfo();
			conmentInfo.setUserName(cursor.getString(cursor.getColumnIndex("conmentname")));
			conmentInfo.setContent(cursor.getString(cursor.getColumnIndex("conment")));
			conmentInfo.setDate(cursor.getString(cursor.getColumnIndex("date")));
			conmentInfos.add(conmentInfo);
		}
		conmentAdapter.notifyDataSetChanged();
		
		conmentEditText = (EditText) findViewById(R.id.conmentEditText);		
		sendButton = (Button) findViewById(R.id.sendButton);
		sendButton.setOnClickListener(this);
		
		

	}


	@Override
	public void onClick(View v) {
		ConmentInfo conmentInfo = new ConmentInfo(null, user,conmentEditText.getText().toString(),DateUtil.getSystemDate());
		conmentAdapter.addConmentInfo(conmentInfo);
		dbHelper.addConment(contentInfo.getUserName(), user, conmentEditText.getText().toString(),DateUtil.getSystemDate());
		Toast.makeText(ConmentActivity.this, "发表成功", Toast.LENGTH_SHORT).show();
	
		InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		inputMethodManager.hideSoftInputFromWindow(conmentEditText.getWindowToken(), 0);
		conmentEditText.setText("");
	}
}
给自己充充电 2016-09-25
  • 打赏
  • 举报
回复
这是一个用listView做的帖子评论的东西,发一条新评论或者滚动ListVIew就出错

80,490

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧