我从API中获取一些数据并在列表视图中填充它,但是当数据填充时,列表视图被搞砸了。
我的listview_row.xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:autofit="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textSize="15sp"
android:textStyle="bold"
android:text="BTC/AUD"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textSize="15sp"
android:textStyle="bold"
android:text="LTC/BTC"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginTop="5dp"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/custom_background"
android:orientation="horizontal"
android:weightSum="3">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:orientation="vertical">
<me.grantland.widget.AutofitTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="Time"
android:textStyle="bold"
autofit:minTextSize="6sp" />
<me.grantland.widget.AutofitTextView
android:id="@+id/tvTimeBtcToAud"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:maxLines="1"
autofit:minTextSize="6sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:orientation="vertical">
<me.grantland.widget.AutofitTextView
android:id="@+id/tvVolBtcToAud"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="Vol"
android:textStyle="bold"
autofit:minTextSize="6sp" />
<me.grantland.widget.AutofitTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:maxLines="1"
autofit:minTextSize="6sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:orientation="vertical">
<me.grantland.widget.AutofitTextView
android:id="@+id/tvPriceBtcToAud"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="Price"
android:textStyle="bold"
autofit:minTextSize="6sp" />
<me.grantland.widget.AutofitTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:maxLines="1"
autofit:minTextSize="6sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/custom_background"
android:orientation="horizontal"
android:weightSum="3">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:orientation="vertical">
<me.grantland.widget.AutofitTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="Time"
android:textStyle="bold"
autofit:minTextSize="6sp" />
<me.grantland.widget.AutofitTextView
android:id="@+id/tvTimeLtcToBtc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:maxLines="1"
autofit:minTextSize="6sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:orientation="vertical">
<me.grantland.widget.AutofitTextView
android:id="@+id/tvVolLtcToBtc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="Vol"
android:textStyle="bold"
autofit:minTextSize="6sp" />
<me.grantland.widget.AutofitTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:maxLines="1"
autofit:minTextSize="6sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:orientation="vertical">
<me.grantland.widget.AutofitTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="Price"
android:textStyle="bold"
autofit:minTextSize="6sp" />
<me.grantland.widget.AutofitTextView
android:id="@+id/tvPriceLtcToBtc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:maxLines="1"
autofit:minTextSize="6sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
当我在Android Studio中打开设计选项卡时,我看到这正是我想要的:
活动代码_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.byteshaft.tradealerterbtcm.MainActivity"
android:id="@+id/ll_all_data">
<ListView
android:id="@+id/lv_all_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:divider="@null">
</ListView>
</LinearLayout>
我的MainActivity代码,我将数据提取并填充到列表视图中:
public class MainActivity extends AppCompatActivity {
ListView lvAllData;
ListAdapter listAdapter;
ArrayList<Integer> ListIds;
HashMap<Integer, ArrayList<String>> hashMapList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvAllData = (ListView) findViewById(R.id.lv_all_data);
getBtcToAud();
}
private void getBtcToAud() {
HttpRequest request = new HttpRequest(MainActivity.this);
request.setOnReadyStateChangeListener(new HttpRequest.OnReadyStateChangeListener() {
@Override
public void onReadyStateChange(HttpRequest request, int readyState) {
switch (readyState) {
case HttpRequest.STATE_DONE:
switch (request.getStatus()) {
case HttpURLConnection.HTTP_OK:
onDataFetchSuccess(request.getResponseText());
break;
default:
onDataFetchFailed(request.getResponseText());
break;
}
}
}
});
request.setOnErrorListener(new HttpRequest.OnErrorListener() {
@Override
public void onError(HttpRequest request, int readyState, short error, Exception exception) {}
});
request.open("GET", "https://api.btcmarkets.net/market/BTC/AUD/trades");
request.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
request.send();
}
private void onDataFetchSuccess(String response) {
Toast.makeText(MainActivity.this, "Data fetch successful", Toast.LENGTH_SHORT).show();
Log.i("data", "" + response);
ListIds = new ArrayList<>();
hashMapList = new HashMap<>();
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < 20 ; i++) {
JSONObject jsonObject = new JSONObject(jsonArray.get(i).toString());
int id = jsonObject.getInt("tid");
Log.e("object", "" + jsonObject);
if (!ListIds.contains(id)) {
ListIds.add(id);
ArrayList<String> tempArrayListData = new ArrayList<>();
tempArrayListData.add(jsonObject.getString("date"));
tempArrayListData.add(jsonObject.getString("amount"));
tempArrayListData.add(jsonObject.getString("price"));
hashMapList.put(id, tempArrayListData);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
listAdapter = new ListAdapter(MainActivity.this, R.layout.listview_row,
ListIds);
lvAllData.setAdapter(listAdapter);
}
private void onDataFetchFailed(String response) {
Toast.makeText(MainActivity.this, "Data fetch failed", Toast.LENGTH_SHORT).show();
Log.e("failed", "" + response);
}
private class ListAdapter extends ArrayAdapter<String> {
ArrayList<Integer> arrayListIds;
ListAdapter(Context context, int resource, ArrayList<Integer> ids) {
super(context, resource);
arrayListIds = ids;
}
@Override
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater layoutInflater = getLayoutInflater();
convertView = layoutInflater.inflate(R.layout.listview_row, parent, false);
viewHolder.tvTimeBtcToAud = convertView.findViewById(R.id.tvTimeBtcToAud);
viewHolder.tvVolBtcToAud = convertView.findViewById(R.id.tvVolBtcToAud);
viewHolder.tvPriceBtcToAud = convertView.findViewById(R.id.tvPriceBtcToAud);
AutofitHelper.create(viewHolder.tvTimeBtcToAud);
AutofitHelper.create(viewHolder.tvVolBtcToAud);
AutofitHelper.create(viewHolder.tvPriceBtcToAud);
// viewHolder.tvTimeLtcToBtc = convertView.findViewById(R.id.tvTimeLtcToBtc);
// viewHolder.tvVolLtcToBtc = convertView.findViewById(R.id.tvVolLtcToBtc);
// viewHolder.tvPriceLtcToBtc = convertView.findViewById(R.id.tvPriceLtcToBtc);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tvTimeBtcToAud.setText(hashMapList.get(arrayListIds.get(position)).get(0));
viewHolder.tvVolBtcToAud.setText(hashMapList.get(arrayListIds.get(position)).get(1));
viewHolder.tvPriceBtcToAud.setText(hashMapList.get(arrayListIds.get(position)).get(2));
// viewHolder.tvTimeLtcToBtc.setText();
// viewHolder.tvVolLtcToBtc.setText();
// viewHolder.tvPriceLtcToBtc.setText();
return convertView;
}
@Override
public int getCount() {
return arrayListIds.size();
}
}
private static class ViewHolder {
AutofitTextView tvTimeBtcToAud;
AutofitTextView tvVolBtcToAud;
AutofitTextView tvPriceBtcToAud;
AutofitTextView tvTimeLtcToBtc;
AutofitTextView tvVolLtcToBtc;
AutofitTextView tvPriceLtcToBtc;
}
我希望我的布局看起来像下图:
我知道我做的事情非常糟糕,但我无法弄清楚。 有什么建议吗?
答案 0 :(得分:1)
在列表项的代码标题中,在listview_row.xml文件中定义。而不是将标题代码移动到activity_main.xml。仅在listview_row.xml文件中添加列表项详细信息。
在您的代码中,您将填充每行的标题和数据。一旦你移动标题部分主xml文件。
//在listview_row.xml中添加 id
<LinearLayout
android:id="@+id/list_heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textSize="15sp"
android:textStyle="bold"
android:text="BTC/AUD"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textSize="15sp"
android:textStyle="bold"
android:text="LTC/BTC"/>
</LinearLayout>
</LinearLayout>
稍后在ViewHolder类中读取此ID TextView listHeading;
现在尝试评论此标题,如果位置> 0,则如下:
if(position==0){
viewHolder.listHeading.setText(hashMapList.get(arrayListIds.get(position)).get(0));
}
在xml的for ListView中使分隔符高度为0。
最后,您需要对时间,价值和价格标题进行这些更改