我主要佈局main.xml中只有一個Button
,一個EditText
和下面的空ListView
:按鈕按下編程將新行添加到ListView,如何?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
>
<LinearLayout
android:id="@+id/input_area"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<EditText
android:id="@+id/input_field"
android:layout_height="40dip"
android:layout_width="fill_parent"
android:layout_weight="5"
/>
<Button
android:id="@+id/send_btn"
android:layout_width="60dip"
android:layout_height="30dip"
android:text="@string/send"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/output_area"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#006633"
android:visibility="gone"
>
<ListView
android:id="@+id/output_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dip"
>
<!-- When "send" button in above input_area is pressed,
text from EditText field show here programmatically as a new row of the listview-->
</ListView>
</LinearLayout>
</LinearLayout>
正如你看到的上面,有兩個孩子LinearLayout
由主LinearLayout
主持。
的孩子LinearLayout
ID爲input_area
由EditText
和Button
的。
的第二孩子LinearLayout
與ID output_area
是LinearLayout
與空ListView
,其知名度設置爲 「了」。
我要實現的功能非常簡單,那就是在input_area,當用戶輸入一些文字在EditText
領域,按下發送按鈕,然後輸入字符串應該在輸出LinearLayout
作爲顯示以編程方式顯示列表視圖的新行。
我想什麼是下面的Java代碼:
EditText inputTxt = (EditText) findViewById(R.id.input_field);
Button sendBtn = (Button) findViewById(R.id.send_btn);
LinearLayout outputArea = findViewById(R.id.output_area);
//Updated by Saurabh
ListView lv = findViewById(R.id.output_list);
MyListAdapter mAdapter = new MyListAdapter(this, arraylist);
//Update finished
sendBtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
int visibility = outputArea.getVisibility();
if(visibility==View.GONE)
// set ListView visible
outputArea.setVisibility(View.VISIBLE);
//get user input
String userInput = inputTxt.getText().toString(); // show this string in a new row of listview
//BUT how to dynamically add new row to the listview here???
}
});
但我不知道如何將新行添加到列表視圖程序,任何人都可以幫我嗎?
順便說一句,我有另一個佈局xml場(row.xml),它定義了每一行的佈局。
----------------------- UPDATE --------------------- ---------------
每個排列表的包含圖標和文本字符串。我的列表適配器和行佈局顯示如下:
我適配器:
private static class MyListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
ArrayList<String> arraylist;
public MyListAdapter(Context context, ArrayList<String> arraylist) {
mInflater = LayoutInflater.from(context);
this.arraylist = arraylist;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.userInput = (TextView) convertView.findViewById(R.id.user_input);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.icon.setImage(???);
holder.userInput.setText(arraylist.get(position));
return convertView;
}
static class ViewHolder {
ImageView icon;
TextView userInput;
}
}
我的名單行佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<ImageView
android: id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/user_input"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textSize="10dip"/>
</LinearLayout>
@ Saurabh,你能爲這種情況建議一個適當的適配器嗎?我不知道如何將userInput添加到適配器... – Mellon
如果只有文本,然後拿一個字符串arraylist並將其設置爲適配器。如果您提供列表視圖和適配器 – ingsaurabh
@ Saurabh的src,我可以更好地爲您提供幫助,我使用適配器和列表行佈局更新了我的帖子,每行包含一個圖像圖標和一個字符串。我不確定我使用的適配器是否正確,您能否看看?謝謝。 (見上面我的更新) – Mellon