我正在开发一个项目,用户在运行时将数据输入数据库,recyclelerview应该从数据库中获取数据。最初,在用户输入数据之前,recyclerview将为空。但问题是当用户输入数据时第一项是recyclerview正在更新并显示第一项。但是当用户输入第二项时,recyclerview没有更新它只显示第一项。
我的活力:
public class Add_Details extends AppCompatActivity {
TextView personname,personmobile,Date;
long id;
RecyclerView recyclerView;
EditText detailName,detailAmount;
DatabaseHandler databaseHandler;
AddDetails_adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_details);
databaseHandler = new DatabaseHandler(this);
detailName = (EditText)findViewById(R.id.Detail_name);
detailAmount = (EditText)findViewById(R.id.Detail_Amount);
Toolbar toolbar = (Toolbar)findViewById(R.id.adddetail_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(Html.fromHtml("<font color='#FFFFFF'>Add Details </font>"));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
personname = (TextView)findViewById(R.id.Detail_personName);
personmobile = (TextView)findViewById(R.id.Detail_personMobile);
Date = (TextView)findViewById(R.id.adddetail_date);
Intent received = getIntent();
String pername = received.getExtras().getString("Name");
String permobile = received.getExtras().getString("Mobile");
id = received.getExtras().getLong("Id");
String date = getDate();
personname.setText(pername);
personmobile.setText(permobile);
Date.setText(date);
recyclerView = (RecyclerView)findViewById(R.id.adddetails_listView);
upDateListview();
}
更新Listview方法
public void upDateListview()
{
if (getDetails()!= null)
{
List<Details> pdetailslist = getDetails();
adapter = new AddDetails_adapter(pdetailslist,this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
添加详细信息方法:此方法在用户输入数据后执行
public void addDetails(View view)
{
if (!detailName.getText().equals(null)||!detailAmount.getText().equals(null))
{
String name = detailName.getText().toString();
String amount = detailAmount.getText().toString();
String date = Date.getText().toString();
Details details = new Details(name,amount,date);
databaseHandler.addDetail(details,id);
adapter.notifyDataSetChanged();
upDateListview();
}
detailName.getText().clear();
detailAmount.getText().clear();
detailName.requestFocus();
}
getDetails方法
public List<Details> getDetails()
{
if (databaseHandler.getDetails(id)!= null)
{
List<Details> list = databaseHandler.getDetails(id);
return list;
}
return null;
}
getDate方法:
public String getDate()
{
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE,dd-MMM-yyyy");
String date = simpleDateFormat.format(calendar.getTime());
return date;
}
我的适配器类
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.Collections;
import java.util.List;
/**
* Created by mrudu on 08-05-2016.
*/
public class AddDetails_adapter extends RecyclerView.Adapter<AddDetails_adapter.View_holder>
{
List<Details> list = Collections.emptyList();
Context context;
public AddDetails_adapter(List<Details> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public AddDetails_adapter.View_holder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.add_detail_row,parent,false);
View_holder view_holder = new View_holder(view);
return view_holder;
}
@Override
public void onBindViewHolder(AddDetails_adapter.View_holder holder, int position)
{
holder.nametext.setText(list.get(position).getName());
holder.amounttext.setText(list.get(position).getAmount());
holder.dateText.setText(list.get(position).getDate());
}
@Override
public int getItemCount() {
return list.size();
}
public class View_holder extends RecyclerView.ViewHolder
{
TextView nametext,amounttext,dateText;
public View_holder(View itemView)
{
super(itemView);
nametext = (TextView)itemView.findViewById(R.id.adddetailRowname);
amounttext = (TextView)itemView.findViewById(R.id.adddetailRowAmount);
dateText = (TextView)itemView.findViewById(R.id.adddetailRowDate);
}
}
}
添加详细信息方法:
public void addDetail(Details details,long id)
{
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DetaiInfo_Name,details.getName());
values.put(DetailInfo_Amount,Double.parseDouble(details.getAmount()));
values.put(DetailInfo_Date,details.getDate());
values.put(DetailInfo_PId,id);
sqLiteDatabase.insert(Detailinfo_TableName,null,values);
sqLiteDatabase.close();
}
比你......