问题
I have the following code from the Bluetooth chat example. All I am looking to do is take the data in over bluetooth and display it as strings in the right place constantly in the table layout. Thanks to the BT example most of it is done for me, but changing the layout in the main file has not worked. Am I taking the right approach, or do I need to make a whole new adapter to handle my data and layout requirements? The way I understand the code, the data is being put into an array with the R.layout.message format and then the R.id.in is telling the program how to display the info. Do I need a separate field ID for each cell I want to display the data from the array in? I appreciate any help you can give me.
// Initialize the array adapter for the conversation thread
mConversationArrayAdapter = new ArrayAdapter<String>(this, R.layout.message);
mConversationView = (ListView) findViewById(R.id.in);
mConversationView.setAdapter(mConversationArrayAdapter);
From the layout.message file
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#A4C639"
android:textSize="18sp"
/>
From the layout.main file
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1,2">
<TableRow>
<TextView
android:layout_column="1"
android:text="RPM"
android:padding="3dip"
android:textSize="18sp"
/>
<TextView
android:layout_column="1"
android:text="Gear"
android:gravity="left"
android:padding="3dip"
android:textSize="24sp"
/>
<TextView
android:layout_column="1"
android:text="Temp"
android:gravity="right"
android:padding="3dip"
android:textSize="18sp"
/>
</TableRow>
<TableRow>
<ListView android:id="@+id/in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="1"
/>
<TextView
android:layout_column="1"
android:text=""
android:gravity="right"
android:padding="3dip"
android:textSize="18sp"
/>
<ListView android:id="@+id/in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="1"
/>
<TableRow>
<TextView
android:layout_column="1"
android:text="MPH"
android:padding="3dip"
android:textSize="18sp"
/>
<ListView android:id="@+id/in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="1"
/>
<TextView
android:layout_column="1"
android:text="Battery Voltage"
android:gravity="right"
android:padding="3dip"
android:textSize="18sp"
/>
android:layout_weight="1"
/>
<TextView
android:layout_column="1"
android:text=""
android:gravity="right"
android:padding="3dip"
android:textSize="18sp"
/>
<ListView android:id="@+id/in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="1"
/>
</TableRow>
<!--
<ListView android:id="@+id/in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stackFromBottom="false"
android:transcriptMode="disabled"
android:layout_weight="1"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText android:id="@+id/edit_text_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="bottom"
/>
<Button android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/send"
/>
</LinearLayout>
-->
回答1:
Try starting with the ListView tutorial. Since you are using complicated views as rows, you will eventually need to override the getView
method on your ArrayAdapter
to inflate the row layout and set its views appropriately.
来源:https://stackoverflow.com/questions/5292809/how-to-use-listview-to-populate-a-table-layout