这个 android/java 的东西还是比较新的。我现在使用扩展BaseAdapter
来显示数组的内容。调用适配器时,我试图用它发送布尔值。发送时,首先将变量设置为我尝试设置的值,但是当进入 getView() 函数时,该值已重置为 false 或 0(取决于我使用的是 bool 还是 int。是否有谁知道发生了什么?
我的代码如下:
MainActivity.java
public class MainActivity extends Activity {
boolean run = false;
public String msg;
boolean out;
private ArrayList<String> arrayList = new ArrayList<String>();
ListView listView;
private RowAdapter radapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radapter = new RowAdapter(getApplicationContext(), R.layout.row, out, arrayList);
listView = (ListView)findViewById(R.id.list);
listView.setAdapter(radapter);
new Thread(new ClientThread()).start();
}
public void onClick(View view) {
try {
EditText et = (EditText) findViewById(R.id.EditText01);
String str = et.getText().toString();
printWriter = new PrintWriter(new BufferedWriter(
newOutputStreamWriter(socket.getOutputStream())),
true);
printWriter.println(str);
out = true;
AddChat(str);
radapter.notifyDataSetChanged();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
class ClientThread implements Runnable {
@Override
public void run() {
run = true;
try{
if(msg!=null){
AddChat(msg);
out = false;
MainActivity.this.runOnUiThread(newRunnable(){
@Override
public void run(){
radapter.notifyDataSetChanged();
}
});
}
}
}catch(Exception e){
e.printStackTrace();
}
}catch(UnknownHostException e1){
e1.printStackTrace();
}catch(IOException e1){
e1.printStackTrace();
}
}
}
public void AddChat(String msg){
arrayList.add(msg);
}
}
行适配器.java
public RowAdapter(Context context, int textViewResourceId, boolean out,ArrayList<String> arrayList) {
super();
this.textViewResourceId = textViewResourceId;
this.arrayList = arrayList;
this.context = context;
this.out=out;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(textViewResourceId, parent, false);
}
String s = arrayList.get(position);
if (out) {
if (s != null) {
TextView label = (TextView) convertView.findViewById(R.id.label);
TextView message = (TextView) convertView.findViewById(R.id.message);
if ((message!= null) &&(label!=null)) {
label.setText("");
message.setText(s);
}
convertView.setBackgroundResource(R.drawable.yellow);
((RelativeLayout) convertView).setGravity(Gravity.RIGHT);
}
}
if (!out) {
if (s != null) {
TextView label = (TextView)convertView.findViewById(R.id.label);
TextView message = (TextView)convertView.findViewById(R.id.message);
if ((message!= null) &&(label!=null)) {
label.setText("");
message.setText(s);
}
}
}
long timestamp = System.currentTimeMillis();
String readableTimestamp = (String)DateUtils.getRelativeTimeSpanString(timestamp);
TextView date = (TextView)convertView.findViewById(R.id.textTime);
date.setText(readableTimestamp);
return convertView;
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}