gpt4 book ai didi

android - 有没有办法将两个 TextView 添加到 arrayadapter

转载 作者:行者123 更新时间:2023-11-30 03:08:29 24 4
gpt4 key购买 nike

我想在我的 listview 中并排使用两个 textviews 并且还想对它们执行搜索。有没有办法在不创建任何新类的情况下做到这一点?这是我到目前为止所做的:

public class MainActivity extends Activity {
final String[] elename ={"Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon","Nitrogen","Oxygen","Fluorine","Neon","Sodium","Magnesium","Aluminium","Silicon","Phosphorous","Sulphur","Chlorine","Argon","Potassium","Calcium","Scandium","Titanium","Vanadium","Chromium","Manganese","Iron","Cobalt","Nickel","Copper","Zinc","Gallium","Germanium","Arsenic","Selenium","Bromine","Krypton","Rubidium","Strontium","Yttrium","Zirconium","Niobium","Molybdenum","Technetium","Ruthenium","Rhodium","Palladium","Silver","Cadmium","Indium","Tin","Antimony","Tellurium","Iodine","Xenon","Caesium","Barium","Lanthanum","Cerium","Praseodymium","Neodymium","Promethium","Samarium","Europium","gadoliium","Terbium","Dysprosium","Holmium","Erbium","Thulium","Ytterbium","Lutetium","Hafnium","Tantalum","Tungsten","Rhenium","Osmium","Iridium","Platinum","Gold","Mercury","Thallium","Lead","Bismuth","Polonium","Astatine","Radon","Francium","Radium","Actinium","Thorium","Protactinium","Uranium","Neptunium","Plutonium","Americium","Curium","Berkelium","Californium","Einsteinium","Fermium","Mendelevium","Nobelium","Lawrencium","Rutherfordium","Dubnium","Seaborgium","Bohrium","Hassium","Meitnerium","Darmstadtium","Roentgenium","Copernicium","Ununtrium","Ununquadium","Ununpentium","Ununhexium","Ununseptium","Ununoctium"};
final String[] nos = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64","65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86","87","88","89","90","91","92","93","94","95","96","97","98","99","100","101","102","103","104","105","106","107","108","109","110","111","112","113","114","115","116","117","118"};

ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


adapter = new ArrayAdapter<String>(this, R.layout.list_item,R.id.element_name, elename);
final EditText searchBar = (EditText) findViewById(R.id.searchbar);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView navList = (ListView) findViewById(R.id.left_drawer);
final LinearLayout linearLayout = (LinearLayout)findViewById(R.id.left_drawer_layout);
navList.setAdapter(adapter);


navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
//Change view according to numbers

drawer.closeDrawer(linearLayout);
}
});

//filter list view after search instantly
searchBar.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {

}

@Override
public void afterTextChanged(Editable s) {
}
});

//hide the keyboard after search on touch list view
navList.setOnScrollListener(new AbsListView.OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
//hide keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(navList.getWindowToken(), 0);
}

public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});

navList.setEmptyView(findViewById(R.id.empty));

}
}

行:

adapter = new ArrayAdapter<String>this,R.layout.list_item,R.id.element_name,elename); 

final String[] elename 的值分配给布局中的 textview。那么,有什么办法可以再添加一个 textview 并在同一行中为其分配 final String[] nos 的值,或者不必创建一个新类?

对不起,如果这是一个愚蠢的问题,但我是初学者!

编辑:崩溃日志

java.lang.NullPointerException
at com.example.navigationdrawer.CustomAdapter.getView(CustomAdapter.java:34)
at android.widget.AbsListView.obtainView(AbsListView.java:1592)
at android.widget.ListView.makeAndAddView(ListView.java:1782)
at android.widget.ListView.fillDown(ListView.java:705)
at android.widget.ListView.fillFromTop(ListView.java:762)
at android.widget.ListView.layoutChildren(ListView.java:1633)
at android.widget.AbsListView.onLayout(AbsListView.java:1422)
at android.view.View.layout(View.java:7175)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
at android.view.View.layout(View.java:7175)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:702)
at android.view.View.layout(View.java:7175)
at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
at android.view.View.layout(View.java:7175)
at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
at android.view.View.layout(View.java:7175)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1146)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)

编辑:MainActivity 崩溃

java.lang.NullPointerException
at com.Chinmay.navigationdrawer.MainActivity$2.onTextChanged(MainActivity.java:66)
at android.widget.TextView.sendOnTextChanged(TextView.java:6584)
at android.widget.TextView.handleTextChanged(TextView.java:6631)
at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6775)
at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889)
at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:654)
at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:415)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:328)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:79)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)

新更新的 Mainactivity:

public class MainActivity extends Activity {
final String[] elename ={"Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon","Nitrogen","Oxygen","Fluorine","Neon","Sodium","Magnesium","Aluminium","Silicon","Phosphorous","Sulphur","Chlorine","Argon","Potassium","Calcium","Scandium","Titanium","Vanadium","Chromium","Manganese","Iron","Cobalt","Nickel","Copper","Zinc","Gallium","Germanium","Arsenic","Selenium","Bromine","Krypton","Rubidium","Strontium","Yttrium","Zirconium","Niobium","Molybdenum","Technetium","Ruthenium","Rhodium","Palladium","Silver","Cadmium","Indium","Tin","Antimony","Tellurium","Iodine","Xenon","Caesium","Barium","Lanthanum","Cerium","Praseodymium","Neodymium","Promethium","Samarium","Europium","Gadolinium","Terbium","Dysprosium","Holmium","Erbium","Thulium","Ytterbium","Lutetium","Hafnium","Tantalum","Tungsten","Rhenium","Osmium","Iridium","Platinum","Gold","Mercury","Thallium","Lead","Bismuth","Polonium","Astatine","Radon","Francium","Radium","Actinium","Thorium","Protactinium","Uranium","Neptunium","Plutonium","Americium","Curium","Berkelium","Californium","Einsteinium","Fermium","Mendelevium","Nobelium","Lawrencium","Rutherfordium","Dubnium","Seaborgium","Bohrium","Hassium","Meitnerium","Darmstadtium","Roentgenium","Copernicium","Ununtrium","Ununquadium","Ununpentium","Ununhexium","Ununseptium","Ununoctium"};
final String[] nos = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64","65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86","87","88","89","90","91","92","93","94","95","96","97","98","99","100","101","102","103","104","105","106","107","108","109","110","111","112","113","114","115","116","117","118"};
CustomAdapter cus;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final EditText searchBar = (EditText) findViewById(R.id.searchbar);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView navList = (ListView) findViewById(R.id.left_drawer);
final LinearLayout linearLayout = (LinearLayout)findViewById(R.id.left_drawer_layout);
CustomAdapter cus = new CustomAdapter(MainActivity.this,elename,nos);
navList.setAdapter(cus);


navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
//Change view according to numbers

drawer.closeDrawer(linearLayout);
}
});

//filter list view after search instantly
searchBar.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.cus.getFilter().filter(cs);
}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {

}

@Override
public void afterTextChanged(Editable s) {
}
});

//hide the keyboard after search on touch list view
navList.setOnScrollListener(new AbsListView.OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
//hide keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(navList.getWindowToken(), 0);
}

public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});

navList.setEmptyView(findViewById(R.id.empty));

}
}

最佳答案

http://developer.android.com/reference/android/widget/ArrayAdapter.html

你可以按照下面从帕特里克的回答中挑选出来的方式做@ How to construct and display the info in simple_list_item_2?

new ArrayAdapter (context, android.R.layout.simple_list_item_2, android.R.id.text1, list)
{
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text1 = (TextView) view.findViewById(android.R.id.text1);
TextView text2 = (TextView) view.findViewById(android.R.id.text2);
text1.setText(elename[positon]);
text2.setText(nos[position]);
return view;
}
});

或者使用扩展 ArrayAdapter 的 CustomAdapter

  class CustomAdapter extends ArrayAdapter<String>
{
String A[],B[];
LayoutInfalter mInfalter;
public CustomAdapter(Context context, String[] A,String B[])
{
super(context,R.layout.customlayout,A);
this.A = A;
this.B = B;
mInfalter = LayoutInfalter.from(context);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null)
{
convertView = mInflater.inflate(R.layout.customlayout,parent,false);
holder = new ViewHolder();
holder.tv1 = (TextView)convertView.findViewById(R.id.textView1);
holder.tv2 = (TextView)convertView.findViewById(R.id.textView2);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertVire.getTag();
}

holder.tv1.setText(A[positon]);
holder.tv2.setText(B[position]);
return convertVIew;
}
static class ViewHolder
{
TextView tv1,tv2;
}
}

有一个布局 customlayout.xml,其中包含 2 个分别具有 id textView1 和 textView 2 的 TextView 。

然后

 CustomAdapter cus = new CustomAdapter(ActivityName.this,elename,nos);
navList.setAdapter(cus);

关于android - 有没有办法将两个 TextView 添加到 arrayadapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21381129/

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