如何使用下面的照片获取ListView。 我用了这段代码:
enter code here public class ListClass extends Activity
{
ListView listView;
String[] values = { "A", "B", "C", "D" };
List<String> arrayList;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
arrayList = new ArrayList<String>();
Collections.addAll(arrayList, values);
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(arrayAdapter);
}
}
结果:pic 1
如何像pic 2一样添加项目?
答案 0 :(得分:2)
以下是自定义列表视图的一个很好的示例:) http://www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter
创建 - 列表项的模型 - 列出项目布局 - 自定义适配器
答案 1 :(得分:2)
您应该创建一个基础适配器,并在此权限中定义您的项目类型..我认为此链接将为您提供帮助
答案 2 :(得分:1)
我已经使用过此代码了。它奏效了。
public class Amordad extends ListActivity
{
private String[] SHIFT;
private String[] DAY_NIGHT;
private String[] NUM;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.general_layout);
refresh();
}
private void refresh()
{
int s = 12;
SHIFT = new String[s];
DAY_NIGHT = new String[s];
NUM = new String[s];
// 1 to s=12
for (int i = 0; i < s; i = i + 1)
{
NUM[i] = (i + 1) + "";
}
for (int i = 0; i < s; i += 4)
{
SHIFT[i] = "A";
SHIFT[i + 1] = "A";
}
for (int i = 3; i < s; i += 4)
{
SHIFT[i] = "B";
SHIFT[i - 1] = "B";
}
for (int i = 0; i < s; i += 2)
{
DAY_NIGHT[i] = "Day";
}
for (int i = 1; i < s; i += 2)
{
DAY_NIGHT[i] = "Night";
}
setListAdapter(new AA());
}
class AA extends ArrayAdapter<String>
{
public AA()
{
super(Amordad.this, R.layout.row_general_i, DAY_NIGHT);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
LayoutInflater in = getLayoutInflater();
View row = in.inflate(R.layout.row_general_i, parent, false);
TextView numbers = (TextView) row.findViewById(R.id.numbers);
TextView shifts = (TextView) row.findViewById(R.id.shifts);
TextView day_night = (TextView) row.findViewById(R.id.day_night);
numbers.setText(NUM[position]);
shifts.setText(SHIFT[position]);
day_night.setText(DAY_NIGHT[position]);
// I set color for "A" Blue (hex code) and "B" Red
// I used toString().contains here and equals ... witch
// one is true or better!? I don`t know.
if (SHIFT[position].toString().contains("A"))
{
shifts.setTextColor(Color.parseColor("#0000FF"));
} else if (SHIFT[position].equals("B"))
{
shifts.setTextColor(Color.RED);
} else
{
}
// I set color for "Day" Gray (hex code)
if (DAY_NIGHT[position].equals("Day"))
{
day_night.setTextColor(Color.parseColor("#555555"));
} else if (SHIFT[position].equals("Night"))
{
day_night.setTextColor(Color.RED);
}
return (row);
}
}
}
结果的 !Result Code 1
答案 3 :(得分:0)
ArrayAdapter不会这样做,你需要一个自定义的BaseAdapter。
等教程它将涉及:
class MyAdapter extends BaseAdapter
ViewHolder
LinearLayout