请原谅我提出这个问题,但我是Android开发的新手,虽然我正在尝试浏览developer.android.com网站上的基础知识,大多数样本(尽管他们说他们是'为Android Studio构建的)尚未设置为使用Gradle,因此除非您将它们迁移到Gradle,否则不会构建,当您这样做时它要么不做任何事情,要么您会遇到大量错误。
我一直在努力学习如何创建列表。
我已经从不同来源阅读了大量有关此内容的教程。我已经阅读了Android文档。我甚至已经阅读了Android示例的源代码,尽管我无法构建其中的一些内容。
我已经尝试编写自己的代码,但我不了解在Android中创建List控件/元素/视图以及绑定/插入数据后的逻辑(缺少更好的词)。
我正在尝试创建一个CardView列表。我不了解如何创建List(任何类型)并在运行时将项插入其中(例如,在foreach循环中)。我认为CardView是一个自己的列表,但事实证明它只是另一个控件/视图,有点像列表项,你在列表中添加了许多。我已经读过你应该把CardView放在RecyclerView中(我认为是一个列表)。但是我尝试过的所有样本都因为错误而无法构建。所以这只会增加混乱,同时试图了解事情的运作方式。
如何在for / foreach循环中创建RecyclerView列表并在运行时将CardView项添加到其中,例如?
答案 0 :(得分:3)
我已经举了一个例子。我回家后会更新解释。
<强> activity_main.xml中强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:padding="16dp"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Title"
android:id="@+id/etTitle" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Description"
android:id="@+id/etDescription" />
<Button
android:id="@+id/btnAddItem"
android:text="Add Item"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@android:color/transparent"
android:layout_weight="1"
android:id="@+id/lvList" />
</LinearLayout>
<强> list_row_layout.xml 强>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_margin="8dp"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/txtTitle"
android:padding="12dp"
android:layout_width="match_parent"
android:textColor="@android:color/black"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txtDescription"
android:padding="12dp"
android:layout_width="match_parent"
android:textColor="@android:color/black"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
<强> ListData.java 强>
public class ListData {
String title;
String Description;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
}
<强> ListAdapter 强>
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
public class ListAdapter extends BaseAdapter {
ArrayList<ListData> myList = new ArrayList<ListData>();
Context context;
public ListAdapter(Context context, ArrayList<ListData> myList) {
this.myList = myList;
this.context = context;
}
@Override
public int getCount() {
return myList.size();
}
@Override
public ListData getItem(int position) {
return myList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ListViewHolder view = (ListViewHolder) convertView;
if (view == null) {
view = new ListViewHolder(context);
}
ListData log = getItem(position);
view.setLog(log);
return view;
}
}
<强> ListViewHolder 强>
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ListViewHolder extends LinearLayout {
Context mContext;
ListData mLog;
public ListViewHolder(Context context) {
super(context);
mContext = context;
setup();
}
public ListViewHolder(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
setup();
}
private void setup() {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.list_row_layout, this);
}
public void setLog(ListData log) {
mLog = log;
TextView tvTitle = (TextView) findViewById(R.id.txtTitle);
tvTitle.setText(mLog.getTitle() + "");
TextView tvDescription = (TextView) findViewById(R.id.txtDescription);
tvDescription.setText(mLog.getDescription() + "");
}
}
<强> MainActivity 强>
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
Button btnAddItem;
ListView lvList;
ArrayList<ListData> myList = new ArrayList<ListData>();
ListAdapter listAdapter;
EditText etTitle, etDescription;
String title = "", description = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvList = (ListView) findViewById(R.id.lvList);
listAdapter = new ListAdapter(MainActivity.this, myList);
lvList.setAdapter(listAdapter);
etTitle = (EditText) findViewById(R.id.etTitle);
etDescription = (EditText) findViewById(R.id.etDescription);
btnAddItem = (Button) findViewById(R.id.btnAddItem);
btnAddItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
title = etTitle.getText().toString();
description = etDescription.getText().toString();
if (title.length() == 0) {
title = "Not Title";
}
if (description.length() == 0) {
description = "No Description";
}
ListData mLog = new ListData();
mLog.setTitle(title);
mLog.setDescription(description);
myList.add(mLog);
listAdapter.notifyDataSetChanged();
}
});
lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListData mLog = listAdapter.getItem(position);
Toast.makeText(MainActivity.this, "Title: " + mLog.getTitle() + " Description: " + mLog.getDescription(), Toast.LENGTH_LONG).show();
}
});
}
}
<强>摇篮强>
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "your application id"
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:cardview-v7:21.+'
}
应该是什么样子