我从用户那里得到输入,然后将它们放在ListView
中,但是当我点击按钮时,它没有任何错误,打开布局并立即关闭它。当我尝试使用arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, m_list);
时,它会很完美。
public class HomeChat extends AppCompatActivity {
ListView listView;
EditText writeSms;
ArrayAdapter<String> arrayAdapter;
ArrayList<String> m_list = new ArrayList<String>();
String emriUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_chat);
writeSms = (EditText) findViewById(R.id.shkrunSms);
listView = (ListView) findViewById(R.id.listView);
emriUser = getIntent().getExtras().getString("emri");
arrayAdapter = new ArrayAdapter<String>(this,R.layout.list_chat,m_list);
listView.setAdapter(arrayAdapter);
}
public void sentSmsButton(View view) {
String mesazhet = emriUser + ": " + writeSms.getText().toString();
if (writeSms != null && writeSms.length() > 0) {
m_list.add(mesazhet);
arrayAdapter.notifyDataSetChanged();
writeSms.setText("");
}else
{
Toast.makeText(getApplicationContext(),"Something Wrong",Toast.LENGTH_LONG).show();
}
}
}
以下是聊天布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/test123"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:background="#cccccc"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
/>
</LinearLayout>
答案 0 :(得分:2)
问题在于ArrayAdapter
期望其中只有一个TextView
的布局。因此,包含LinearLayout
的{{1}}的布局无法使用。尝试将TextView
更改为:
R.layout.list_chat