今天才知道,昨天一个想法错了,之前以为一个活动只有一个布局,没想到还有嵌套的子布局
数组数据要通过适配器,由构造函数把数据传入,Java这个部分的知识还没学到,现在不清楚
学习的进度太慢了,很多要学的,失落但是还是要试试
这里包含两个效果,简单的文本数组数据显示是注释掉的
1.布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id = "@+id/view"
>
</ListView>
</LinearLayout>
<?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">
<ImageView
android:id = "@+id/image_anim"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id = "@+id/anim_name"
android:layout_gravity="center"
android:layout_marginLeft="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<pre name="code" class="html"> <EditText
android:id = "@+id/infor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "type"/>
<Button
android:id = "@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text = "list"
android:onClick="viewlistsend"/>
2.类
package com.example.a111.one;
/**
* Created by 111 on 2016/5/31.
*/
public class ABCD {
private String name;
private int manageid;
public ABCD(String na,int id){
name = na;
manageid = id;
}
public String getName(){
return name;
}
public int getManageid(){
return manageid;
}
}
import java.util.List;
/**
* Created by 111 on 2016/5/31.
*/
public class ABCDAdapter extends ArrayAdapter<ABCD>{
private int resourceid;
public ABCDAdapter(Context context, int textViewResourceId, List<ABCD> objects){
super(context,textViewResourceId,objects);
resourceid = textViewResourceId;
}
public View getView(int position, View convertView, ViewGroup parent){
ABCD abcd = getItem(position);
View view = LayoutInflater.from(getContext()).inflate(resourceid,null);
ImageView imageView = (ImageView)view.findViewById(R.id.image_anim);
TextView textView = (TextView)view.findViewById(R.id.anim_name);
imageView.setImageResource(abcd.getManageid());
textView.setText(abcd.getName());
return view;
}
}
3.活动
public class ActivityC extends AppCompatActivity {
private EditText inform;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_c);
inform = (EditText) findViewById(R.id.infor);
Intent intent = getIntent();
/*Bundle data = intent.getBundleExtra("data");
String in_data = data.getString("infor");*/
String in_data = intent.getStringExtra("infor");
inform.setText(in_data);
}
public void viewlistsend(View view){
Intent viewlist = new Intent(this,Activity_viewlist.class);
startActivity(viewlist);
}
}
package com.example.a111.one;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class Activity_viewlist extends AppCompatActivity {
//private String []data = {"AAA","BBB","CCC","DDD","EEE","FFF","GGG","HHH","LLL","JJJ",
// "KKK","MMM","NNN","WWW","RRR","YYY","TTT","QQQ"};
private List<ABCD> animlist = new ArrayList<ABCD>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
//ListView listView = (ListView) findViewById(R.id.view);
//listView.setAdapter(adapter);
initanim();
ABCDAdapter abcdAdapter = new ABCDAdapter(this,R.layout.activity_activity_viewlist,animlist);
ListView listView = (ListView) findViewById(R.id.view);
listView.setAdapter(abcdAdapter);
}
private void initanim(){
ABCD a = new ABCD("AAA",R.drawable.d);
animlist.add(a);
ABCD b = new ABCD("BBB",R.drawable.d);
animlist.add(b);
ABCD c = new ABCD("CCC",R.drawable.d);
animlist.add(c);
ABCD d = new ABCD("DDD",R.drawable.d);
animlist.add(d);
ABCD e = new ABCD("EEE",R.drawable.e);
animlist.add(e);
ABCD f = new ABCD("RRR",R.drawable.d);
animlist.add(f);
ABCD g = new ABCD("TTT",R.drawable.d);
animlist.add(g);
ABCD q = new ABCD("YYY",R.drawable.d);
animlist.add(q);
ABCD r = new ABCD("UUU",R.drawable.d);
animlist.add(r);
ABCD t = new ABCD("OOO",R.drawable.e);
animlist.add(t);
ABCD y = new ABCD("QQQ",R.drawable.d);
animlist.add(y);
ABCD h = new ABCD("SSS",R.drawable.d);
animlist.add(h);
ABCD m = new ABCD("ZZZ",R.drawable.d);
animlist.add(m);
ABCD n = new ABCD("KKK",R.drawable.d);
animlist.add(n);
}
}
4.结果