gpt4 book ai didi

android - 如何在不使用自定义适配器的情况下将子项添加到 ListView

转载 作者:行者123 更新时间:2023-11-30 02:40:00 27 4
gpt4 key购买 nike

在我的 ListView 中,我得到了一个可用的 wi-fi 列表和一些数据,我想要的是将数组项 [0] 显示为项目,将数组项 [2] 显示为子项,而不使用自定义适配器和消耗品 ListView

这是我将数据添加到 ListView 中的代码。

import java.util.List;

import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;

public class MainActivity extends Activity {
WifiManager mainWifiObj;
WifiScanReceiver wifiReciever;
ExpandableListView list;
String wifis[];


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ExpandableListView)findViewById(R.id.ListView1);
mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiReciever = new WifiScanReceiver();
mainWifiObj.startScan();

}
protected void onPause() {
unregisterReceiver(wifiReciever);
super.onPause();
}

protected void onResume() {
registerReceiver(wifiReciever, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
class WifiScanReceiver extends BroadcastReceiver {
@SuppressLint("UseValueOf")
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifiScanList = mainWifiObj.getScanResults();
wifis = new String[wifiScanList.size()];
for(int i = 0; i < wifiScanList.size(); i++){
wifis[i] = ((wifiScanList.get(i)).toString());
}
String filtered[] = new String[wifiScanList.size()];
int counter = 0;
for (String eachWifi : wifis) {
String[] temp = eachWifi.split(",");

filtered[counter] = temp[0].substring(5).trim()+"\n" + temp[2].substring(12).trim()+"\n" +temp[3].substring(6).trim();//0->SSID, 2->Key Management 3-> Strength


counter++;

}
list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, filtered));


}
}
}

最佳答案

您只需使用自定义适配器即可实现此目的。这是我从我为 ListView 创建的自定义适配器为您的问题转换的示例:

import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CustomListAdapter extends BaseAdapter {

private static final int TYPE_ITEM = 0;

private static final int TYPE_SUBITEM = 1;

private static final int TYPE_MAX_COUNT = TYPE_SUBITEM + 1;

private ArrayList<String> listViewItems;

public LayoutInflater mInflater;

public CustomListAdapter(Activity act){
this.listViewItems= new listViewItems<String>();
mInflater = (LayoutInflater) act.getApplicationContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
}

@SuppressLint("InflateParams")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//this finds out the type of the view that is being created
int type = getItemViewType(position);

switch (type) {
case TYPE_ITEM:
if(convertView == null)
convertView = mInflater.inflate(R.layout.listview_item, null);
//the following is an example to change things in subitem
TextView text_of_item = (TextView) convertView.findViewById(R.id.textView_item);
text_of_item.setText("item"+position);
break;

case TYPE_SUBITEM:
if(convertView == null)
convertView = mInflater.inflate(R.layout.listview_subitem, null);
//the following is an example to change things in subitem
TextView text_of_subitem = (TextView) convertView.findViewById(R.id.textView_subitem);
text_of_subitem.setText("subitem"+position);
break;
}

return convertView;
}

@Override
public int getItemViewType(int position) {
//you can achive this part via using integer IDs. Just change your ArrayList<String> to ArrayList<int>

if(listViewItems.get(position).equals("item")){//I suppose you give unique names to your subitems
return TYPE_ITEM;
}else if(listViewItems.get(position).equals("subitem")){//I suppose you give unique names to your subitems
return TYPE_SUBITEM;
}

@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}


@Override
public int getCount() {
return listViewItems.size();
}

@Override
public String getItem(int position) {
return listViewItems.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

public void addItem(String item){
listViewItems.add(item);
notifyDataSetChanged();
}

public void removeItem(int position){
listViewItems.remove(position);
notifyDataSetChanged();
}

}

并且您应该从您的 MainActivity 中调用它,如下所示:

CustomListAdapter  mCustomListAdapter = new CustomListAdapter (this);

ListView mList = (ListView) findViewById(R.id.listview);
mList.setAdapter(mCustomListAdapter);

//when you want to add item to your listview just add the following:
mCustomListAdapter.addItem("item");

//when you want to add subitem to your listview just add the following:
mCustomListAdapter.addItem("subitem");

你的 listview_subitem.xml 也可以是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_subitem"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >

<TextView
android:id="@+id/textView_subitem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />

</LinearLayout>

你的 listview_item.xml 可以是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal" >

<TextView
android:id="@+id/textView_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>

你的 activity_main.xml:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Listview to display slider menu -->
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

关于android - 如何在不使用自定义适配器的情况下将子项添加到 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25927322/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com