我创建了一个自定义适配器来填充我拥有的项目,具体取决于有多少元素,但它只填充第一项,我无法弄清楚为什么
main.xml中
<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textSize="25sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ListView
android:id="@+id/customList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/textView" />
</RelativeLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="@+id/item_text_2"
android:layout_width="116dp"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="Default Text 2"
android:layout_gravity="right"
android:layout_below="@+id/item_text_1"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/item_text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="Default Text 1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/item_text_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="Default Text 3"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="42dp" />
自定义类
public class CustomClass {
private String txt1, txt2, txt3;
public CustomClass(String txt1, String txt2, String txt3) {
this.txt1 = txt1;
this.txt2 = txt2;
this.txt3 = txt3;
}
public String getTxt1() {
return txt1;
}
public String getTxt2() {
return txt2;
}
public String getTxt3() {
return txt3;
}
}
MainActivity
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<CustomClass> lst_stuff = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateCustomClass();
populateListView();
}
private void populateCustomClass()
{
lst_stuff.add(new CustomClass("First", "I Got", "FIRST!!!!!"));
lst_stuff.add(new CustomClass("Second", "How did you...", "you know what GG"));
lst_stuff.add(new CustomClass("Third", "I Got", "Last?!! ... I am just too good to be first"));
}
private void populateListView()
{
ArrayAdapter<CustomClass> adapter = new CustomAdapter();
ListView lst = (ListView) findViewById(R.id.customList);
lst.setAdapter(adapter);
}
private class CustomAdapter extends ArrayAdapter<CustomClass>
{
public CustomAdapter()
{
super(MainActivity.this, R.layout.item, lst_stuff);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View itemView = convertView;
if (itemView == null)
itemView = getLayoutInflater().inflate(R.layout.item, parent, false);
try
{
CustomClass customClass = lst_stuff.get(position);
TextView textView_1 = (TextView) findViewById(R.id.item_text_1);
textView_1.setText(customClass.getTxt1());
TextView textView_2 = (TextView) findViewById(R.id.item_text_2);
textView_2.setText(customClass.getTxt2());
TextView textView_3 = (TextView) findViewById(R.id.item_text_3);
textView_3.setText(customClass.getTxt3());
}
catch (NullPointerException exception)
{
Toast.makeText(MainActivity.this, "Exception", Toast.LENGTH_SHORT).show();
exception.printStackTrace();
}
return itemView;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
是否有遗漏或出错的地方?
答案 0 :(得分:1)
一切似乎都没问题,但我会改变方法getView()的内容:
reaver -i mon0 -b F8:7B:7A:69:9F:0F -c <channel> -vv
我认为你必须首先获得inflater的一个实例,然后用你的布局(R.layout.item)来扩展你的视图(rowView)。
祝你好运!