2016-12-14 32 views
5

Tôi có thể biết tại sao bộ điều hợp danh sách tùy chỉnh của tôi trông như thế này?Bộ điều hợp ListView tùy chỉnh Android

Ảnh:

enter image description here

Codes:

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; 
} 

Tuỳ chỉnh LV Adaptor 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> 

Tuỳ chỉnh LV Adaptor 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 được mã hóa trong một lớp khác, không thể dường như để thêm nó vào.

Thanks guys ..

+1

Nó trông giống như một vấn đề thiết kế, Liệu nó trông giống nhau trong cửa sổ xem trước? –

+1

có đó là những gì tôi nhận được khi tôi chạy activity_main.xml của mình – domster

Trả lời

1

Tôi thử, và tôi không có biên tập viên, vì vậy hãy cho tôi phản hồi.

Hãy thử sử dụng bố trí này: TextView của mình là nhỏ hơn so với chiều cao của văn bản có thể

<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:orientation="vertical"> 
<!-- I removed the weightsum because this linearlayout is containing only a single item. If you have more items tell me and I will edit it --> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:weightSum="1" 
    android:orientation="horizontal" > 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.7" 
     android:orientation="vertical" > 
     <!-- replaced width with 0dp also here --> 

     <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="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> 
+0

Sẽ dùng thử ngay bây giờ nhờ Misley – domster

+0

OMG IT WORKS. THANK YOU MISLEY !!! – domster

+0

@domster ahahah chào mừng, tôi đã thêm các giải thích bên trong bố cục như ý kiến ​​để kiểm tra câu trả lời đầy đủ hơn :) –

1

Đây là vấn đề do cân nhắc cho lượt xem văn bản trong bố cục. Ở đây, chế độ xem văn bản được cắt xén vì trọng lượng ... không cần phải thêm trọng lượng..chỉ loại bỏ nó và cho chiều cao là "nội dung bọc".

Các vấn đề liên quan