我可以知道爲什麼我的自定義列表視圖適配器看起來像這樣?自定義列表視圖適配器Android
圖片:
代碼:
MainActivity.java
public class MainActivity extends AppCompatActivity {
CurrencyRatesDetails crd = new CurrencyRatesDetails();
TextView inputCurrTV, convertedAmtTV;
ListView currencyLV;
EditText inputAmtET;
ArrayAdapter<String> adapter;
String[] currNameArr = crd.getNames();
String[] currCodeArr = crd.getCodes();
String[] currRateArr = crd.getRates();
Context context;
int index = 0;
//String[] rateCurrArr = {"AUD", "BGN", "BRL", "CAD", "CHF", "CNY"};
//double[] rateArr = {0.944, 1.2824, 2.2842, 0.96158, 0.70946, 4.8624}
Menu myMenu = null;
double rate, amtInput, finalConversion;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
inputAmtET = (EditText) findViewById(R.id.inputAmtET);
convertedAmtTV = (TextView) findViewById(R.id.convertedAmtTV);
inputCurrTV = (TextView) findViewById(R.id.inputCurrTV);
currencyLV = (ListView) findViewById(R.id.currencyLV);
//Resources myRes = this.getResources();
//currArr = myRes.getStringArray(R.array.currencyList);
//adapter = ArrayAdapter.createFromResource(this, R.array.currencyList, android.R.layout.simple_selectable_list_item);
//adapter = new ArrayAdapter<String>(this, android.R.layout.simple_selectable_list_item, currNameArr);
//currencyLV.setAdapter(adapter);
currencyLV.setAdapter(new CustomAdapterLV(this, currNameArr, currCodeArr, currRateArr));
currencyLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
inputCurrTV.setText(currCodeArr[i]);
index = i;
//Getting rate based on selected currency code
//rate = rateArr[i];
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
this.myMenu = menu;
addMenuItems(menu);
getMenuInflater().inflate(R.menu.mainmenu, menu);
return true;
}
private void addMenuItems(Menu menu) {
int index = 200;
menu.add(index, index, index, "Settings");
menu.add(index, index + 1, index + 1, "Add Custom Rate");
menu.add(index, index + 2, index + 2, "Load Default Rates");
}
public boolean onOptionsItemSelected(MenuItem item) {
//getOrder() to get Menu Item at this specific orderId
if (item.getOrder() == 123) {
amtInput = Double.parseDouble(inputAmtET.getText().toString());
//finalConversion = amtInput/rate;
finalConversion = crd.conversion(amtInput, index);
//Formatting converted value to 2d.p
String finalValue = String.format("%.2f", finalConversion);
convertedAmtTV.setText(finalValue);
} else if (item.getItemId() == 201) {
Intent myIntent = new Intent(MainActivity.this, CustomXchangeRate.class);
startActivity(myIntent);
}
return true;
}
定製LV適配器Layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_custom_lvadapter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:weightSum="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0.0dp"
android:weightSum="1"
android:layout_weight="0.13"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:orientation="vertical" >
<TextView
android:id="@+id/currencyNameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:text="text"/>
<TextView
android:id="@+id/currencyCodeTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15px"
android:textSize="18dp"
android:text="text"/>
</LinearLayout>
<LinearLayout
android:layout_width="0.0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:orientation="vertical"
android:layout_gravity="center_horizontal" >
<TextView
android:id="@+id/currencyRateTV"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="text"
android:textSize="30dp"
android:layout_marginLeft="15px"
android:layout_marginTop="15px"
android:gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
定製LV接口Class.java
public class CustomAdapterLV extends BaseAdapter{
String[] resultNames;
String[] resultCodes;
String[] resultRates;
Context context;
private static LayoutInflater inflater = null;
public CustomAdapterLV(MainActivity mainActivity, String[] names, String[]codes, String[] rates) {
resultNames = names;
resultCodes = codes;
resultRates = rates;
context = mainActivity;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return resultNames.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class ViewHolder{
TextView tvName;
TextView tvCode;
TextView tvRate;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder vh = new ViewHolder();
View rowView;
rowView = inflater.inflate(R.layout.activity_custom_lvadapter, null);
vh.tvName = (TextView) rowView.findViewById(R.id.currencyNameTV);
vh.tvCode = (TextView) rowView.findViewById(R.id.currencyCodeTV);
vh.tvRate = (TextView) rowView.findViewById(R.id.currencyRateTV);
vh.tvName.setText(resultNames[position]);
vh.tvCode.setText(resultCodes[position]);
vh.tvRate.setText(resultRates[position]);
return rowView;
}
currNameArr,currCodeArr,currRateArr另一類是硬編碼,不能似乎將它添加英寸
謝謝你們..
它看起來像一個設計問題,它看起來在同一預覽窗口? –
是的,這就是我運行我的activity_main.xml時得到的結果 – domster