我正在尝试将我的Android Studio项目上的ListView更改为RecycleView,因为我从包含大量字符串的数据库导入数据。我有一个适用于我的ListView的适配器,但我从不使用RecycleView,所以我不知道如何使用它。 我搜索了很多主题,但我真的无法理解如何使用它。 我希望有人可以帮助我; 这是我的代码:
fragment_one.xml (我的主要是,我已经添加了RecycleView而不是ListView,同样是@id)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4">
<Button
android:id="@+id/frammentoUno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gestione Generale"
android:onClick="FragmentOneClick"
android:layout_weight="1"/>
<Button
android:id="@+id/frammentoDue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ultimi Allarmi"
android:onClick="FragmentTwoClick"
android:layout_weight="3"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Gestione Generale"
android:id="@+id/textView3"
android:layout_gravity="center_horizontal" />
<!-- A RecyclerView with some commonly used attributes -->
<android.support.v7.widget.RecyclerView
android:id="@+id/GetAllAllarmiListView"
android:scrollbars="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:dividerHeight="1dp"/>
FrammentoUno.java
public class FrammentoUno extends AppCompatActivity {
private ListView GetAllAllarmiListView;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient mClient;
private Uri mUrl;
private String mTitle;
private String mDescription;
private Button frammentoUno, frammentoDue;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_one);
addListenerOnButton();
this.GetAllAllarmiListView = (ListView) this.findViewById(R.id.GetAllAllarmiListView);
new GetAllAllarmiTask().execute(new ApiConnector());
mClient = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
mUrl = Uri.parse("android-app://com.example.andrea/http/host_path");
mTitle = "Standard Poodle";
mDescription = "The Standard Poodle stands at least 18 inches at the withers";
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.get_all_allarmi_list_view_cell, null);
final CheckBox visto = (CheckBox) textEntryView.findViewById(R.id.visto);
visto.setOnClickListener(btnListener);
}
private View.OnClickListener btnListener = new View.OnClickListener() {
@Override
public void onClick(View visto) {
if (visto.isClickable()) {
startActivity(new Intent(FrammentoUno.this, SQL_visto.class));
Toast.makeText(FrammentoUno.this,
"Visto", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(FrammentoUno.this,
"Non Visto", Toast.LENGTH_LONG).show();
}
}
};
public void addListenerOnButton() {
frammentoUno = (Button) findViewById(R.id.frammentoUno);
frammentoDue = (Button) findViewById(R.id.frammentoDue);
frammentoUno.setOnClickListener(new View.OnClickListener() {
//Run when button is clicked
@Override
public void onClick(View v) {
Intent i = new Intent(FrammentoUno.this, FrammentoUno.class);
startActivity(i);
Toast.makeText(FrammentoUno.this, "Allarmi Generali",
Toast.LENGTH_LONG).show();
}
});
frammentoDue.setOnClickListener(new View.OnClickListener() {
//Run when button is clicked
@Override
public void onClick(View v) {
Intent i = new Intent(FrammentoUno.this, FrammentoDue.class);
startActivity(i);
Toast.makeText(FrammentoUno.this, "Controllo Ultimi Allarmi",
Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.MENU_1:
/*
Codice di gestione della voce MENU_1
*/
startActivity(new Intent(this, ControlloSbarre.class));
return true;
case R.id.MENU_2:
/*
Codice di gestione della voce MENU_2
*/
startActivity(new Intent(this, LoginActivity.class));
return true;
}
return false;
}
public void setListAdapter(JSONArray jsonArray) {
this.GetAllAllarmiListView.setAdapter(new GetAllAllarmiListViewAdapter(jsonArray, this));
}
private class GetAllAllarmiTask extends AsyncTask<ApiConnector, Long, JSONArray> {
@Override
protected JSONArray doInBackground(ApiConnector... params) {
return params[0].GetAllAllarmi();
}
@Override
protected void onPostExecute(JSONArray jsonArray) {
setListAdapter(jsonArray);
}
}}
GetAllAllarmiListViewAdapter.java
public class GetAllAllarmiListViewAdapter extends BaseAdapter {
private JSONArray dataArray;
private Activity activity;
private static LayoutInflater inflater = null;
public GetAllAllarmiListViewAdapter(JSONArray jsonArray, Activity a)
{
this.dataArray = jsonArray;
this.activity = a;
inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return this.dataArray.length();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// set up convert view if it is null
ListCell cell;
if (convertView == null)
{
convertView = inflater.inflate(R.layout.get_all_allarmi_list_view_cell, null);
cell = new ListCell();
cell.nomeParcheggio = (TextView) convertView.findViewById(R.id.id_park);
cell.data = (TextView) convertView.findViewById((R.id.data));
cell.stato = (TextView) convertView.findViewById((R.id.stato));
cell.descrizione = (TextView) convertView.findViewById((R.id.id_descrizione));
cell.targa = (TextView) convertView.findViewById((R.id.targa));
cell.azione = (TextView) convertView.findViewById((R.id.azione));
cell.dispositivo = (TextView) convertView.findViewById((R.id.dispositivo));
convertView.setTag(cell);
}
else
{
cell = (ListCell) convertView.getTag();
}
//change the data of cell
try {
JSONObject jsonObject = this.dataArray.getJSONObject(position);
cell.nomeParcheggio.setText(jsonObject.getString("nome"));
cell.data.setText (jsonObject.getString("data_al"));
cell.stato.setText(jsonObject.getString("stato"));
cell.descrizione.setText(jsonObject.getString("descrizione"));
cell.targa.setText(jsonObject.getString("targa"));
cell.azione.setText(jsonObject.getString("azione"));
if (jsonObject.getString("azione")=="1")
{
cell.dispositivo.setText(jsonObject.getString("varco"));
}
else if (jsonObject.getString("azione")== "2"){
cell.dispositivo.setText(jsonObject.getString("cassa"));
}
}
catch (JSONException e)
{
e.printStackTrace();
}
return convertView;
}
private class ListCell
{
private TextView nomeParcheggio;
private TextView data;
private TextView stato;
private TextView descrizione;
private TextView targa;
private TextView azione;
private TextView dispositivo;
}}
get_all_allarmi_list_view_cell.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="3">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nome Park"
android:id="@+id/id_park"
android:layout_gravity="center_horizontal"
android:padding="5dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data"
android:id="@+id/data"
android:layout_gravity="center_horizontal"
android:padding="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stato"
android:id="@+id/stato"
android:layout_gravity="center_horizontal"
android:padding="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Descrizione"
android:id="@+id/id_descrizione"
android:layout_gravity="center_horizontal"
android:padding="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Targa"
android:id="@+id/targa"
android:layout_gravity="center_horizontal"
android:padding="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Azione"
android:id="@+id/azione"
android:layout_gravity="center_horizontal"
android:padding="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dispositivo"
android:id="@+id/dispositivo"
android:layout_gravity="center_horizontal"
android:padding="5dp"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="@+id/visto"
android:onClick="clickHandler"/>
的AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.andrea.gestionesbarre">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_citiware"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Frammenti_LogIn_Menu.LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.andrea.FrammentoUno">
</activity>
<activity android:name="com.example.andrea.FrammentoDue"></activity>
<!--
ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information.
-->
<activity android:name=".Frammenti_LogIn_Menu.ControlloSbarre">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.andrea.FrammentoUno" />
</activity>
</application>
我已经在我的gradle中添加了依赖项。
感谢您的帮助
答案 0 :(得分:0)
您必须在适配器类中进行更改。这是我的示例类,它是适用于回收站视图的适配器。
public class NavigationDrawerAdapter extends RecyclerView.Adapter<NavigationDrawerAdapter.MyViewHolder> {
List<NavDrawerItem> data = Collections.emptyList();
private LayoutInflater inflater;
private Context context;
public NavigationDrawerAdapter(Context context, List<NavDrawerItem> data) {
this.context = context;
inflater = LayoutInflater.from(context);
this.data = data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.nav_drawer_row, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
NavDrawerItem current = data.get(position);
holder.title.setText(current.getTitle());
holder.iv.setImageResource(current.getIcon().getResourceId(position,1));
}
@Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView title;
ImageView iv;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
iv = (ImageView) itemView.findViewById(R.id.nav_drawer_row_icon);
}
}
}
将此行放在您的java代码中
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
答案 1 :(得分:0)
我修改了你的完整适配器类,如下所示:
def decorator(func):
def wrapped(a, b, c):
return c + func(a, b)
return wrapped
@decorator
def add(a, b):
return (a+b)
现在修改活动中的setListAdapter()方法,如下所示:
public class GetAllAllarmiListViewAdapter extends RecyclerView.Adapter<GetAllAllarmiListViewAdapter.MyViewHolder>{
private JSONArray dataArray;
private Activity activity;
private static LayoutInflater inflater = null;
// Provide a suitable constructor (depends on the kind of dataset)
public GetAllAllarmiListViewAdapter(JSONArray jsonArray, Activity a) {
this.dataArray = jsonArray;
this.activity = a;
inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.get_all_allarmi_list_view_cell, parent, false);
// set the view's size, margins, paddings and layout parameters
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
try {
JSONObject jsonObject = this.dataArray.getJSONObject(position);
holder.nomeParcheggio.setText(jsonObject.getString("nome"));
holder.data.setText (jsonObject.getString("data_al"));
holder.stato.setText(jsonObject.getString("stato"));
holder.descrizione.setText(jsonObject.getString("descrizione"));
holder.targa.setText(jsonObject.getString("targa"));
holder.azione.setText(jsonObject.getString("azione"));
if (jsonObject.getString("azione")=="1")
{
holder.dispositivo.setText(jsonObject.getString("varco"));
}
else if (jsonObject.getString("azione")== "2"){
holder.dispositivo.setText(jsonObject.getString("cassa"));
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return dataArray.length();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
// each data item is just a string in this case
private TextView nomeParcheggio;
private TextView data;
private TextView stato;
private TextView descrizione;
private TextView targa;
private TextView azione;
private TextView dispositivo;
public MyViewHolder(View v) {
super(v);
nomeParcheggio = (TextView) v.findViewById(R.id.id_park);
data = (TextView) v.findViewById((R.id.data));
stato = (TextView) v.findViewById((R.id.stato));
descrizione = (TextView) v.findViewById((R.id.id_descrizione));
targa = (TextView) v.findViewById((R.id.targa));
azione = (TextView) v.findViewById((R.id.azione));
dispositivo = (TextView) v.findViewById((R.id.dispositivo));
v.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int position = getAdapterPosition();
// It will be called if you click on a list item. variable position contains the position of the item in the adapter. You can do whatever you want with the position..
}
}
}
在活动中不要忘记用RecyclerView替换listView:
public void setListAdapter(JSONArray jsonArray) {
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
this.GetAllAllarmiListView.setLayoutManager(layoutManager);
this.GetAllAllarmiListView.setAdapter(new GetAllAllarmiListViewAdapter(jsonArray, this));
}
在onCreate()方法中初始化recyclerView:
private RecyclerView GetAllAllarmiListView;