Android本地公共类textview在构造函数上创建
我是新来的。请帮我做这个 我的问题是: 我创建了listview.xmlAndroid本地公共类textview在构造函数上创建,android,constructor,android-listview,textview,Android,Constructor,Android Listview,Textview,我是新来的。请帮我做这个 我的问题是: 我创建了listview.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:gravity="left|center" android:layout_width="wrap_content" android:paddingBottom="5dp" andr
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:gravity="left|center"
android:layout_width="wrap_content"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:paddingLeft="5dp">
<TextView android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#FFFF00"
android:textIsSelectable="true">
</TextView>
<TextView android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textColor="#0099CC"
android:textIsSelectable="true">
</TextView>
当我从此类创建对象时:
listview_row obListview = new listview_row();
obListview.Text1.setText(bundle.getString("first_name"));
obListview.Text2.setText(bundle.getString("last_name"));
obListview.Text1始终等于null,并且正在引发NullObjectExection。我怎样才能解决这个问题。
我以为我可以在构造函数上创建textview,但我想我错了
PS:我想用两个文本视图创建Listview,我使用了android.R.layout.simple\u list\u item\u 1,我可以在两个活动之间毫无问题地传递数据。现在我尝试显示first_name | last_name@first screen listview
有关更多信息,请参见我的onActivityResult:
public void onActivityResult(int requestCode, int resultCode, Intent intent){
if ( resultCode == RESULT_OK ){
Bundle extras = intent.getExtras();
if (extras != null){
switch (requestCode){
case Activity_New:
add_new_row( extras );
break;
case Activity_Edit:
edit_row( extras );
break;
}}
}
}
这是“添加新行”:
公共无效添加\新\行(捆绑){
//比尔吉列里尼·凯·埃迪约鲁兹
int sayi=headerray.size();
listview_row obListview=新建listview_row();
obListview.Text1.setText(bundle.getString(“名字”);
obListview.Text2.setText(bundle.getString(“姓氏”)
我用于详细信息数组:
private ArrayList<mtable_row> itemArray;
public class mtable_row{
int index;
String first_name;
String last_name;
String birth_date;
}
private ArrayList itemrarray;
公共类mtable_行{
整数指数;
字符串名;
字符串last_name;
字符串出生日期;
}
我的主要目标是使用两个阵列:
第一个是标题,第二个是项目。
标头有两个字段第一个\u名称和第二个\u名称
我喜欢在我的主屏幕上显示这个数组。要这样做,你必须膨胀一个
布局
文件以传递给你的类,并使用它来获得像这样的ID
Text1 = (TextView) layout.findViewById(R.id.TextView01);
其中,layout
是您传递给构造函数的膨胀的layout
。我没有尝试过这种方法,但是类似的方法可能会起作用。但是,除非您需要这样做,否则只需将这些视图
保留在活动中可能会更容易
public class YourActivity extends Activity {
TextViw Text1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
Text1 = (TextView) findViewById(R.id.TextView01);
Text2 = (TextView) findViewById(R.id.TextView02);
.....
}
}
你得到的是一个NPE
,因为据我所知,你还没有膨胀这些视图存在的布局
文件
也是一个次要的事情,但是你应该坚持不要混淆或混淆别人。类名应该是骆驼箱,变量名应该是混合的。
我想用两个textview创建Listview
为此,我建议您有一个自定义listview。定义一个自定义适配器。为每一行膨胀一个自定义布局
您可以将值传递给CustomAdapter的构造函数
CustomAdapter cus = new CustomAdapter(this);
ListView lv= (ListView) findViewById(R.id.lv);
lv.setAdapter(cus);
public class CustomAdapter extends ArrayAdapter {
Context c;
LayoutInfator mInflater;
public CustomAdapter(CustomListView customListView) {
super(customListView, 0);
// TODO Auto-generated constructor stub
this.mInflater = LayoutInflater.from(customListView);
c=customListView;
}
public int getCount() {
return 20; //listview item count
}
public Object getItem(int arg0) {
return arg0;
}
public long getItemId(int arg0) {
return arg0;
}
public View getView(final int arg0, View arg1, ViewGroup arg2) {
final ViewHolder vh;
vh= new ViewHolder();
if(arg1==null )
{
arg1=mInflater.inflate(R.layout.customlayout, arg2,false);
vh.tv1= (TextView)arg1.findViewById(R.id.textView1);
vh.tv2= (TextView)arg1.findViewById(R.id.textView2);
}
else
{
arg1.setTag(vh);
}
vh.tv1.setText("hello");
vh.tv2.setText("hello");
return arg1;
}
static class ViewHolder
{
TextView tv1,tv2;
}
}
在两个活动之间传递数据后,在第二个活动中检索数据。每个屏幕都有自己的UI。您不能像所做的那样引用UI元素
第二个活动有两个文本视图
public class SecondActivity extends Activity{
TextView tv1;
TextView tv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
tv1 = (TextView) findViewById(R.id.TextView01);
tv2 = (TextView) findViewById(R.id.TextView02);
Bundle extras = getIntent().getExtras();
if (extras != null) {
tv1.setText(extras.getString("first_name"));
tv2.setText(extras.getString("last_name"));
}
}
}
listview\u行
中的Text1
和Text2
指的是什么?您显然没有设置它们。。。。
CustomAdapter cus = new CustomAdapter(this);
ListView lv= (ListView) findViewById(R.id.lv);
lv.setAdapter(cus);
public class CustomAdapter extends ArrayAdapter {
Context c;
LayoutInfator mInflater;
public CustomAdapter(CustomListView customListView) {
super(customListView, 0);
// TODO Auto-generated constructor stub
this.mInflater = LayoutInflater.from(customListView);
c=customListView;
}
public int getCount() {
return 20; //listview item count
}
public Object getItem(int arg0) {
return arg0;
}
public long getItemId(int arg0) {
return arg0;
}
public View getView(final int arg0, View arg1, ViewGroup arg2) {
final ViewHolder vh;
vh= new ViewHolder();
if(arg1==null )
{
arg1=mInflater.inflate(R.layout.customlayout, arg2,false);
vh.tv1= (TextView)arg1.findViewById(R.id.textView1);
vh.tv2= (TextView)arg1.findViewById(R.id.textView2);
}
else
{
arg1.setTag(vh);
}
vh.tv1.setText("hello");
vh.tv2.setText("hello");
return arg1;
}
static class ViewHolder
{
TextView tv1,tv2;
}
}
public class SecondActivity extends Activity{
TextView tv1;
TextView tv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
tv1 = (TextView) findViewById(R.id.TextView01);
tv2 = (TextView) findViewById(R.id.TextView02);
Bundle extras = getIntent().getExtras();
if (extras != null) {
tv1.setText(extras.getString("first_name"));
tv2.setText(extras.getString("last_name"));
}
}
}