我有一个回收站视图,该视图显示json api中的数据。我将其作为多项选择,因此用户可以从列表中选择多个数据。当用户单击按钮时,我想使用截击功能将所选数据存储到数据库表的不同列中。
我已经做到了,但是存储在数据库中的数据是相同的,我的意思是每一列存储了相同的数据。我想让每一列获取不同的数据。
例如:用户选择3个数据,例如data1,data2和data3。当用户点击按钮时,存储在db上的数据为:
第1列|第2栏|第3栏
data1 | data1 |数据1
data2 |数据2 |数据2
data3 | data3 |数据3
我要实现的是当用户点击按钮存储数据时,列将:
第1列|第2列| column3
data1 |数据2 |数据3
有人有解决办法吗?
在我的代码下面:
Activity.java
public class AddTagActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ArrayList<TagItem> tagItemArrayList = new ArrayList<>();
private TagAdapter adapter;
private Button save;
private String url = "http://agenproduk.dianarthaselaras.com/user/tag";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_tag);
save = findViewById(R.id.save);
recyclerView = findViewById(R.id.tag_rview);
int numberOfColumns = 2;
//recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns));
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
adapter = new TagAdapter(this, tagItemArrayList);
recyclerView.setAdapter(adapter);
getData();
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (adapter.getSelected().size() > 0) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < adapter.getSelected().size(); i++) {
stringBuilder.append(adapter.getSelected().get(i).getTagList());
stringBuilder.append("\n");
}
//showToast(stringBuilder.toString().trim());
postTag(stringBuilder.toString().trim(), stringBuilder1.toString().trim(), stringBuilder2.toString().trim());
} else {
//showToast("No Selection");
}
}
});
}
private void getData(){
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.show();
RequestQueue requestQueue = Volley.newRequestQueue(this);
String URL_READ = url;
System.out.print(URL_READ);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, URL_READ,null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
progressDialog.dismiss();
System.out.println(response);
try{
JSONArray jsonArray = response.getJSONArray("tag");
Gson gson = new Gson();
tagItemArrayList.clear();
for (int k = 0; k < jsonArray.length(); k++) {
JSONObject jsonObject = jsonArray.getJSONObject(k);
TagItem data = gson.fromJson(String.valueOf(jsonObject), TagItem.class);
tagItemArrayList.add(data);
}
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
progressDialog.dismiss();
} catch (JSONException e) {
e.printStackTrace();
progressDialog.dismiss();
Toast.makeText(AddTagActivity.this, "Error "+e.toString(), LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(AddTagActivity.this, "Error "+error.toString(), LENGTH_SHORT).show();
}
})
{
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Accept", "application/json");
return params;
}
};
requestQueue.add(jsonObjectRequest);
}
private void showToast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
private void postTag(final String tag1, final String tag2, final String tag3){
Intent intent = getIntent();
final String idd = intent.getStringExtra("id");
final String name = intent.getStringExtra("nama");
String URL_REGISTER = "http://agenproduk.dianarthaselaras.com/user/addtag";
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_REGISTER,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONObject jsonObject = new JSONObject(response);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
}
}, 3000);
} catch (JSONException e){
e.printStackTrace();
Toast.makeText(AddTagActivity.this, "Failed" + e.toString(), Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(AddTagActivity.this, "Failed" + error.toString(), Toast.LENGTH_SHORT).show();
}
})
{
@Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=UTF-8";
}
@Override
public Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("userid", idd);
params.put("user_name", name);
params.put("tag1", tag1);
params.put("tag2", tag2);
params.put("tag3", tag3);
params.put("Content-Type","application/x-www-form-urlencoded");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
adapter.java
public class TagAdapter extends RecyclerView.Adapter<TagAdapter.TagViewHolder> {
private Context context;
private ArrayList<TagItem> tag;
public TagAdapter(Context context, ArrayList<TagItem> tag) {
this.context = context;
this.tag = tag;
}
public void setTag(ArrayList<TagItem> tag) {
this.tag = new ArrayList<>();
this.tag = tag;
notifyDataSetChanged();
}
@NonNull
@Override
public TagViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.item_tag, viewGroup, false);
return new TagViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull TagViewHolder tagViewHolder, int position) {
tagViewHolder.bind(tag.get(position));
}
@Override
public int getItemCount() {
return tag.size();
}
class TagViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
private ImageView imageView;
TagViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView);
imageView = itemView.findViewById(R.id.imageView);
}
void bind(final TagItem tag) {
imageView.setVisibility(tag.isChecked() ? View.VISIBLE : View.GONE);
textView.setText(tag.getTagList());
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tag.setChecked(!tag.isChecked());
imageView.setVisibility(tag.isChecked() ? View.VISIBLE : View.GONE);
}
});
}
}
public ArrayList<TagItem> getAll() {
return tag;
}
public ArrayList<TagItem> getSelected() {
ArrayList<TagItem> selected = new ArrayList<>();
for (int i = 0; i < tag.size(); i++) {
if (tag.get(i).isChecked()) {
selected.add(tag.get(i));
}
}
return selected;
}
答案 0 :(得分:1)
如果选择了3个以上的项目,我不知道要发布哪些项目
if (adapter.getSelected().size() == 1) {
postTag(adapter.getSelected().get(0).getTagList(), null, null);
} else if (adapter.getSelected().size() == 2) {
postTag(adapter.getSelected().get(0).getTagList(),
adapter.getSelected().get(1).getTagList(),
null);
} else if (adapter.getSelected().size() > 2) {
postTag(adapter.getSelected().get(0).getTagList(),
adapter.getSelected().get(1).getTagList(),
adapter.getSelected().get(2).getTagList());
}