选择Recyclerview的特定行以共享意图

时间:2017-03-21 15:16:04

标签: android mysql android-recyclerview

有没有办法选择我Math.cos()的特定行(可能通过获取行的索引),然后使用它来创建意图。

我通过获取特定的行信息尝试了一些事情,但这甚至都不是很接近。我的回收器中填充了数据库中的数据。

2 个答案:

答案 0 :(得分:0)

要选择我的回收器的特定行,您可以像这样添加setOnItemClickListener代码到onCreate函数:

listView_search.setOnItemClickListener(
            new AdapterView.OnItemClickListener(){
                @Override
                public void onItemClick(AdapterView<?> parent, View v, int position, long id){
                    //Here I'm getting store_id and store_name
                    TextView store_search_id =(TextView) v.findViewById(R.id.store_search_id);
                    TextView store_search_name =(TextView) v.findViewById(R.id.store_search_name);
                    String store_name = store_search_name.getText().toString();
                    String store_id = store_search_id.getText().toString();

                    //Then I'm sending theses variables to a shared Intent
                    Intent myIntent = new Intent(getApplicationContext(), StoresShowActivity.class);
                    myIntent.putExtra("STORE_ID", store_id);
                    myIntent.putExtra("STORE_NAME", store_name);
                    startActivity(myIntent);
            }
            }
);

您可能还想按照以下步骤操作:

一个。为“回收”视图添加布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_stores_search"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ksa.ikp.activities.StoresSearchActivity">

<SearchView
    android:id="@+id/SearchView_store"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:iconifiedByDefault="false">

    <requestFocus />
</SearchView>

<ListView
    android:id="@+id/listView_search"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/SearchView_store" />
</RelativeLayout >

B中。添加recycleview项目的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="10dp">

<TextView
    android:id="@+id/store_search_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="invisible" />

<TextView
    android:id="@+id/store_search_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/store_search_category"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

℃。使用像这样的适配器,您可以根据需要进行修改

public class StoresSearchAdapter extends BaseAdapter {

// Declare Variables
Context mContext;
LayoutInflater inflater;
private List<Store> myList = null;
private ArrayList<Store> myArrayList;

public StoresSearchAdapter(Context context, List<Store> myList) {
    mContext = context;
    this.myList = myList;
    inflater = LayoutInflater.from(mContext);
    this.myArrayList = new ArrayList<Store>();
    this.myArrayList.addAll(myList);
}

public class ViewHolder {
    TextView store_search_id;
    TextView store_search_name;
    TextView store_search_category;
}

@Override
public int getCount() {
    return myList.size();
}

@Override
public Store getItem(int position) {
    return myList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

public View getView(final int position, View view, ViewGroup parent) {
    final ViewHolder holder;
    if (view == null) {
        holder = new ViewHolder();
        view = inflater.inflate(R.layout.activity_stores_search_item, null);
        // Locate the TextViews in activity_stores_search_item.xml
        holder.store_search_id = (TextView) view.findViewById(R.id.store_search_id);
        holder.store_search_name = (TextView) view.findViewById(R.id.store_search_name);
        holder.store_search_category = (TextView) view.findViewById(R.id.store_search_category);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }
    // Set the results into TextViews
    holder.store_search_id.setText(myList.get(position).getStr_id()+"");
    holder.store_search_name.setText(myList.get(position).getStr_name());
    String s = SplashActivity.db.getNameById(myList.get(position).getStr_cat_id()+"",SplashActivity.db.TABLE_CATEGORY,SplashActivity.db.COLUMN_CAT_NAME,SplashActivity.db.COLUMN_CAT_ID);
    s = " in ("+ s + ")";
    holder.store_search_category.setText(s);
    return view;
}

// Filter Class
public void filter(String s) {
    s = s.toLowerCase(Locale.getDefault());
    myList.clear();
    if (s.length() == 0) {
        myList.addAll(myArrayList);
    } else {
        myList.addAll(SplashActivity.db.getAllStoresFiltered(s));
    }
    notifyDataSetChanged();
}

}

最后你的最终输出将是这样的: screenshot

答案 1 :(得分:0)

这是我的recyclerbackgroundtask

public class RecycleBackgroundTask扩展了AsyncTask {

Context ctx;
Activity activity;
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<Post> arrayList = new ArrayList<>();

public RecycleBackgroundTask(Context ctx){
    this.ctx = ctx;
    activity = (Activity)ctx;
}

String json_string = "http://192.168.2.110/app/post.php";

@Override
public void onPreExecute() {
    recyclerView = (RecyclerView)activity.findViewById(R.id.recyclerView);
    layoutManager = new LinearLayoutManager(ctx);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);
    adapter = new RecyclerAdapter(arrayList);
    recyclerView.setAdapter(adapter);
}

@Override
public Void doInBackground(Void... params) {
    try {
        URL url = new URL(json_string);
        HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder stringBuilder = new StringBuilder();
        String line;

        while ((line=bufferedReader.readLine())!=null) {
            stringBuilder.append(line+"\n");
        }
        httpURLConnection.disconnect();
        String json_string = stringBuilder.toString().trim();

        JSONObject jsonObject = new JSONObject(json_string);
        JSONArray jsonArray = jsonObject.getJSONArray("server_response");

        int count = 0;
        while(count<jsonArray.length()){
            JSONObject JO = jsonArray.getJSONObject(count);
            count++;

            Post post = new Post(JO.getString("headline"),JO.getString("source"),JO.getString("url"));
            publishProgress(post);

        }

        Log.d("JSON STRING",json_string);

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}


@Override
public void onProgressUpdate(Post... values) {

    arrayList.add(values[0]);
    adapter.notifyDataSetChanged();
}

我的RecyclerAdapter

公共类RecyclerAdapter扩展了RecyclerView.Adapter {

private static final int TYPE_HEAD = 0;
private static final int TYPE_LIST = 1;



ArrayList<Post> arrayList = new ArrayList<>();

public RecyclerAdapter (ArrayList<Post> arrayList){

    this.arrayList = arrayList;
}



@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    if(viewType == TYPE_HEAD){
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_recycler,parent,false); // instead of row_recycler ,header_layout.
        RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view,viewType);
        return recyclerViewHolder;
    }
    if(viewType == TYPE_LIST){
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_recycler,parent,false);
        RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view,viewType);
        return recyclerViewHolder;
    }
    return null;

}



@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {


    if(holder.viewType == TYPE_LIST) {
        Post post = arrayList.get(position-1);
        holder.Headline.setText(post.getHeadline());
        holder.Source.setText(post.getSource());
        //holder.Url.setText(post.getUrl());


    }
}


@Override
public int getItemCount() {
    return arrayList.size()+1;
}

public  static class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    TextView Headline,Source;//,Url;
    int viewType;



    public RecyclerViewHolder(View view,int viewType){

        super(view);
        if(viewType == TYPE_LIST) {
            Headline = (TextView) view.findViewById(R.id.headline);
            Source = (TextView) view.findViewById(R.id.source);

            Headline.setOnClickListener(this);
            Source.setOnClickListener(this);
           // Url = (TextView) view.findViewById(R.id.url);


            this.viewType = TYPE_LIST;

        }else if (viewType == TYPE_HEAD){

            this.viewType = TYPE_HEAD;
        }
    }


    @Override
    public void onClick(View v) {
        Toast.makeText(v.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
    }
}
@Override
public  int getItemViewType(int position){
    if(position==0)
        return TYPE_HEAD;
        return TYPE_LIST;


}

public  void setFilter(ArrayList<Post> newList){
    arrayList = new ArrayList<>();
    arrayList.addAll(newList);
    notifyDataSetChanged();
}

}

也许这有帮助。我已经设置了一个简单的onclicklistener与吐司btw